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 | bool read_only; |
| 352 | }; |
| 353 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 354 | /* |
| 355 | * a single device |
| 356 | */ |
| 357 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 358 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 359 | |
| 360 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 361 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 362 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 363 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 364 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 365 | struct rbd_client *rbd_client; |
| 366 | |
| 367 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 368 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 369 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 370 | |
| 371 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 372 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 373 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 374 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 375 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 376 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 377 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 378 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 379 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 380 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 381 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 382 | struct mutex watch_mutex; |
| 383 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 384 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 385 | u64 watch_cookie; |
| 386 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 387 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 388 | struct rw_semaphore lock_rwsem; |
| 389 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 390 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 391 | struct rbd_client_id owner_cid; |
| 392 | struct work_struct acquired_lock_work; |
| 393 | struct work_struct released_lock_work; |
| 394 | struct delayed_work lock_dwork; |
| 395 | struct work_struct unlock_work; |
| 396 | wait_queue_head_t lock_waitq; |
| 397 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 398 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 399 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 400 | struct rbd_spec *parent_spec; |
| 401 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 402 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 403 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 404 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 405 | /* Block layer tags. */ |
| 406 | struct blk_mq_tag_set tag_set; |
| 407 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 408 | /* protects updating the header */ |
| 409 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 410 | |
| 411 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 412 | |
| 413 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 414 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 415 | /* sysfs related */ |
| 416 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 417 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 418 | }; |
| 419 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 420 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 421 | * Flag bits for rbd_dev->flags: |
| 422 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 423 | * by rbd_dev->lock |
| 424 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 425 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 426 | enum rbd_dev_flags { |
| 427 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 428 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 429 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 430 | }; |
| 431 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 432 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 433 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 434 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 435 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 436 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 437 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 438 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 439 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 440 | /* Slab caches for frequently-allocated structures */ |
| 441 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 442 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 443 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 444 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 445 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 446 | static DEFINE_IDA(rbd_dev_id_ida); |
| 447 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 448 | static struct workqueue_struct *rbd_wq; |
| 449 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 450 | /* |
| 451 | * Default to false for now, as single-major requires >= 0.75 version of |
| 452 | * userspace rbd utility. |
| 453 | */ |
| 454 | static bool single_major = false; |
| 455 | module_param(single_major, bool, S_IRUGO); |
| 456 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 457 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 458 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 459 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 460 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 461 | size_t count); |
| 462 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 463 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 464 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 465 | size_t count); |
| 466 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 467 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 468 | 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] | 469 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 470 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 471 | static int rbd_dev_id_to_minor(int dev_id) |
| 472 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 473 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int minor_to_rbd_dev_id(int minor) |
| 477 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 478 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 481 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 482 | { |
| 483 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 484 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 485 | } |
| 486 | |
| 487 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 488 | { |
| 489 | bool is_lock_owner; |
| 490 | |
| 491 | down_read(&rbd_dev->lock_rwsem); |
| 492 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 493 | up_read(&rbd_dev->lock_rwsem); |
| 494 | return is_lock_owner; |
| 495 | } |
| 496 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 497 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 498 | { |
| 499 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 500 | } |
| 501 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 502 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 503 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 504 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 505 | 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] | 506 | 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] | 507 | |
| 508 | static struct attribute *rbd_bus_attrs[] = { |
| 509 | &bus_attr_add.attr, |
| 510 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 511 | &bus_attr_add_single_major.attr, |
| 512 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 513 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 514 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 515 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 516 | |
| 517 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 518 | struct attribute *attr, int index) |
| 519 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 520 | if (!single_major && |
| 521 | (attr == &bus_attr_add_single_major.attr || |
| 522 | attr == &bus_attr_remove_single_major.attr)) |
| 523 | return 0; |
| 524 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 525 | return attr->mode; |
| 526 | } |
| 527 | |
| 528 | static const struct attribute_group rbd_bus_group = { |
| 529 | .attrs = rbd_bus_attrs, |
| 530 | .is_visible = rbd_bus_is_visible, |
| 531 | }; |
| 532 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 533 | |
| 534 | static struct bus_type rbd_bus_type = { |
| 535 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 536 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | static void rbd_root_dev_release(struct device *dev) |
| 540 | { |
| 541 | } |
| 542 | |
| 543 | static struct device rbd_root_dev = { |
| 544 | .init_name = "rbd", |
| 545 | .release = rbd_root_dev_release, |
| 546 | }; |
| 547 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 548 | static __printf(2, 3) |
| 549 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 550 | { |
| 551 | struct va_format vaf; |
| 552 | va_list args; |
| 553 | |
| 554 | va_start(args, fmt); |
| 555 | vaf.fmt = fmt; |
| 556 | vaf.va = &args; |
| 557 | |
| 558 | if (!rbd_dev) |
| 559 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 560 | else if (rbd_dev->disk) |
| 561 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 562 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 563 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 564 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 565 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 566 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 567 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 568 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 569 | else /* punt */ |
| 570 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 571 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 572 | va_end(args); |
| 573 | } |
| 574 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 575 | #ifdef RBD_DEBUG |
| 576 | #define rbd_assert(expr) \ |
| 577 | if (unlikely(!(expr))) { \ |
| 578 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 579 | "at line %d:\n\n" \ |
| 580 | "\trbd_assert(%s);\n\n", \ |
| 581 | __func__, __LINE__, #expr); \ |
| 582 | BUG(); \ |
| 583 | } |
| 584 | #else /* !RBD_DEBUG */ |
| 585 | # define rbd_assert(expr) ((void) 0) |
| 586 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 587 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 588 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 589 | 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] | 590 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 591 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 592 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 593 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 594 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 595 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 596 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 597 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 598 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 599 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 600 | u8 *order, u64 *snap_size); |
| 601 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 602 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 603 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 604 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 605 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 606 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 607 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 608 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 609 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 610 | return -EROFS; |
| 611 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 612 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 613 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 614 | removing = true; |
| 615 | else |
| 616 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 617 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 618 | if (removing) |
| 619 | return -ENOENT; |
| 620 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 621 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 622 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 626 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 627 | { |
| 628 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 629 | unsigned long open_count_before; |
| 630 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 633 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 634 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 636 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 639 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 640 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 641 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | int val; |
| 643 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 644 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 646 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 647 | if (get_user(val, (int __user *)(arg))) |
| 648 | return -EFAULT; |
| 649 | |
| 650 | ro = val ? true : false; |
| 651 | /* Snapshot doesn't allow to write*/ |
| 652 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 653 | return -EROFS; |
| 654 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 655 | spin_lock_irq(&rbd_dev->lock); |
| 656 | /* prevent others open this device */ |
| 657 | if (rbd_dev->open_count > 1) { |
| 658 | ret = -EBUSY; |
| 659 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 660 | } |
| 661 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 662 | if (rbd_dev->mapping.read_only != ro) { |
| 663 | rbd_dev->mapping.read_only = ro; |
| 664 | ro_changed = true; |
| 665 | } |
| 666 | |
| 667 | out: |
| 668 | spin_unlock_irq(&rbd_dev->lock); |
| 669 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 670 | if (ret == 0 && ro_changed) |
| 671 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 672 | |
| 673 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 677 | unsigned int cmd, unsigned long arg) |
| 678 | { |
| 679 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 680 | int ret = 0; |
| 681 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 682 | switch (cmd) { |
| 683 | case BLKROSET: |
| 684 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 685 | break; |
| 686 | default: |
| 687 | ret = -ENOTTY; |
| 688 | } |
| 689 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | #ifdef CONFIG_COMPAT |
| 694 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 695 | unsigned int cmd, unsigned long arg) |
| 696 | { |
| 697 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 698 | } |
| 699 | #endif /* CONFIG_COMPAT */ |
| 700 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 701 | static const struct block_device_operations rbd_bd_ops = { |
| 702 | .owner = THIS_MODULE, |
| 703 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 704 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 705 | .ioctl = rbd_ioctl, |
| 706 | #ifdef CONFIG_COMPAT |
| 707 | .compat_ioctl = rbd_compat_ioctl, |
| 708 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 712 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 713 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 715 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 716 | { |
| 717 | struct rbd_client *rbdc; |
| 718 | int ret = -ENOMEM; |
| 719 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 720 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 722 | if (!rbdc) |
| 723 | goto out_opt; |
| 724 | |
| 725 | kref_init(&rbdc->kref); |
| 726 | INIT_LIST_HEAD(&rbdc->node); |
| 727 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 728 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 729 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 730 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 731 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 732 | |
| 733 | ret = ceph_open_session(rbdc->client); |
| 734 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 735 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 736 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 737 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 738 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 739 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 740 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 741 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 742 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 743 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 744 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 745 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 746 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 747 | kfree(rbdc); |
| 748 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 749 | if (ceph_opts) |
| 750 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 751 | dout("%s: error %d\n", __func__, ret); |
| 752 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 753 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 756 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 757 | { |
| 758 | kref_get(&rbdc->kref); |
| 759 | |
| 760 | return rbdc; |
| 761 | } |
| 762 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 763 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 764 | * Find a ceph client with specific addr and configuration. If |
| 765 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 766 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 767 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 768 | { |
| 769 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 770 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 771 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 772 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 773 | return NULL; |
| 774 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 775 | spin_lock(&rbd_client_list_lock); |
| 776 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 777 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 778 | __rbd_get_client(client_node); |
| 779 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 780 | found = true; |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | spin_unlock(&rbd_client_list_lock); |
| 785 | |
| 786 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 790 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 791 | */ |
| 792 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 793 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | Opt_last_int, |
| 795 | /* int args above */ |
| 796 | Opt_last_string, |
| 797 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 798 | Opt_read_only, |
| 799 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 800 | Opt_lock_on_read, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 801 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 804 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 805 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 806 | /* int args above */ |
| 807 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 808 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 809 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 810 | {Opt_read_write, "read_write"}, |
| 811 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 812 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 813 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 814 | }; |
| 815 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 816 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 817 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 818 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 819 | bool lock_on_read; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 820 | }; |
| 821 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 822 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 823 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 824 | #define RBD_LOCK_ON_READ_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 825 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | static int parse_rbd_opts_token(char *c, void *private) |
| 827 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 828 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 829 | substring_t argstr[MAX_OPT_ARGS]; |
| 830 | int token, intval, ret; |
| 831 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 832 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 833 | if (token < Opt_last_int) { |
| 834 | ret = match_int(&argstr[0], &intval); |
| 835 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 836 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | dout("got int token %d val %d\n", token, intval); |
| 840 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 841 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 842 | } else { |
| 843 | dout("got token %d\n", token); |
| 844 | } |
| 845 | |
| 846 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 847 | case Opt_queue_depth: |
| 848 | if (intval < 1) { |
| 849 | pr_err("queue_depth out of range\n"); |
| 850 | return -EINVAL; |
| 851 | } |
| 852 | rbd_opts->queue_depth = intval; |
| 853 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 854 | case Opt_read_only: |
| 855 | rbd_opts->read_only = true; |
| 856 | break; |
| 857 | case Opt_read_write: |
| 858 | rbd_opts->read_only = false; |
| 859 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 860 | case Opt_lock_on_read: |
| 861 | rbd_opts->lock_on_read = true; |
| 862 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 863 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 864 | /* libceph prints "bad option" msg */ |
| 865 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 866 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 867 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 871 | static char* obj_op_name(enum obj_operation_type op_type) |
| 872 | { |
| 873 | switch (op_type) { |
| 874 | case OBJ_OP_READ: |
| 875 | return "read"; |
| 876 | case OBJ_OP_WRITE: |
| 877 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 878 | case OBJ_OP_DISCARD: |
| 879 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 880 | default: |
| 881 | return "???"; |
| 882 | } |
| 883 | } |
| 884 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 885 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 886 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 887 | * not exist create it. Either way, ceph_opts is consumed by this |
| 888 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 890 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 891 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 892 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 893 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 894 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 895 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 896 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 897 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 898 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 899 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 900 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 902 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 907 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 908 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 909 | */ |
| 910 | static void rbd_client_release(struct kref *kref) |
| 911 | { |
| 912 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 913 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 914 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 915 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 917 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 918 | |
| 919 | ceph_destroy_client(rbdc->client); |
| 920 | kfree(rbdc); |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 925 | * it. |
| 926 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 927 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 928 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 929 | if (rbdc) |
| 930 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 933 | static bool rbd_image_format_valid(u32 image_format) |
| 934 | { |
| 935 | return image_format == 1 || image_format == 2; |
| 936 | } |
| 937 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 938 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 939 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 940 | size_t size; |
| 941 | u32 snap_count; |
| 942 | |
| 943 | /* The header has to start with the magic rbd header text */ |
| 944 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 945 | return false; |
| 946 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 947 | /* The bio layer requires at least sector-sized I/O */ |
| 948 | |
| 949 | if (ondisk->options.order < SECTOR_SHIFT) |
| 950 | return false; |
| 951 | |
| 952 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 953 | |
| 954 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 955 | return false; |
| 956 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 957 | /* |
| 958 | * The size of a snapshot header has to fit in a size_t, and |
| 959 | * that limits the number of snapshots. |
| 960 | */ |
| 961 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 962 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 963 | if (snap_count > size / sizeof (__le64)) |
| 964 | return false; |
| 965 | |
| 966 | /* |
| 967 | * Not only that, but the size of the entire the snapshot |
| 968 | * header must also be representable in a size_t. |
| 969 | */ |
| 970 | size -= snap_count * sizeof (__le64); |
| 971 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 972 | return false; |
| 973 | |
| 974 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 975 | } |
| 976 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 977 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 978 | * returns the size of an object in the image |
| 979 | */ |
| 980 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 981 | { |
| 982 | return 1U << header->obj_order; |
| 983 | } |
| 984 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 985 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 986 | { |
| 987 | if (rbd_dev->header.stripe_unit == 0 || |
| 988 | rbd_dev->header.stripe_count == 0) { |
| 989 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 990 | rbd_dev->header.stripe_count = 1; |
| 991 | } |
| 992 | |
| 993 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 994 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 995 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 996 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 997 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 998 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 999 | } |
| 1000 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1001 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1002 | * Fill an rbd image header with information from the given format 1 |
| 1003 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1004 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1005 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1006 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1007 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1008 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | bool first_time = header->object_prefix == NULL; |
| 1010 | struct ceph_snap_context *snapc; |
| 1011 | char *object_prefix = NULL; |
| 1012 | char *snap_names = NULL; |
| 1013 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1014 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1015 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1016 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1017 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1018 | /* Allocate this now to avoid having to handle failure below */ |
| 1019 | |
| 1020 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1021 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1022 | sizeof(ondisk->object_prefix), |
| 1023 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | if (!object_prefix) |
| 1025 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1029 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1030 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1031 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1032 | if (!snapc) |
| 1033 | goto out_err; |
| 1034 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1035 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1036 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1037 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1038 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1039 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1040 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1041 | if (snap_names_len > (u64)SIZE_MAX) |
| 1042 | goto out_2big; |
| 1043 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1044 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1045 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | |
| 1047 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1048 | snap_sizes = kmalloc_array(snap_count, |
| 1049 | sizeof(*header->snap_sizes), |
| 1050 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1051 | if (!snap_sizes) |
| 1052 | goto out_err; |
| 1053 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1054 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1055 | * Copy the names, and fill in each snapshot's id |
| 1056 | * and size. |
| 1057 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1058 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1060 | * snap_names_len bytes beyond the end of the |
| 1061 | * snapshot id array, this memcpy() is safe. |
| 1062 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1063 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1064 | snaps = ondisk->snaps; |
| 1065 | for (i = 0; i < snap_count; i++) { |
| 1066 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1067 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1068 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1069 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1070 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1071 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1072 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1073 | if (first_time) { |
| 1074 | header->object_prefix = object_prefix; |
| 1075 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1076 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1077 | } else { |
| 1078 | ceph_put_snap_context(header->snapc); |
| 1079 | kfree(header->snap_names); |
| 1080 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1084 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1085 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1086 | header->snapc = snapc; |
| 1087 | header->snap_names = snap_names; |
| 1088 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1089 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1090 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1091 | out_2big: |
| 1092 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1093 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1094 | kfree(snap_sizes); |
| 1095 | kfree(snap_names); |
| 1096 | ceph_put_snap_context(snapc); |
| 1097 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1098 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1099 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1102 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1103 | { |
| 1104 | const char *snap_name; |
| 1105 | |
| 1106 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1107 | |
| 1108 | /* Skip over names until we find the one we are looking for */ |
| 1109 | |
| 1110 | snap_name = rbd_dev->header.snap_names; |
| 1111 | while (which--) |
| 1112 | snap_name += strlen(snap_name) + 1; |
| 1113 | |
| 1114 | return kstrdup(snap_name, GFP_KERNEL); |
| 1115 | } |
| 1116 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1117 | /* |
| 1118 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1119 | * Note that result is for snapshots in *descending* order. |
| 1120 | */ |
| 1121 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1122 | { |
| 1123 | u64 snap_id1 = *(u64 *)s1; |
| 1124 | u64 snap_id2 = *(u64 *)s2; |
| 1125 | |
| 1126 | if (snap_id1 < snap_id2) |
| 1127 | return 1; |
| 1128 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Search a snapshot context to see if the given snapshot id is |
| 1133 | * present. |
| 1134 | * |
| 1135 | * Returns the position of the snapshot id in the array if it's found, |
| 1136 | * or BAD_SNAP_INDEX otherwise. |
| 1137 | * |
| 1138 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1139 | * reverse order, highest snapshot id first. |
| 1140 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1141 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1142 | { |
| 1143 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1144 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1145 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1146 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1147 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1148 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1149 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1152 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1153 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1154 | { |
| 1155 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1156 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1157 | |
| 1158 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1159 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1160 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1161 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1162 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1163 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1166 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1167 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1168 | if (snap_id == CEPH_NOSNAP) |
| 1169 | return RBD_SNAP_HEAD_NAME; |
| 1170 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1171 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1172 | if (rbd_dev->image_format == 1) |
| 1173 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1174 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1175 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1178 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1179 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1180 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1181 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1182 | if (snap_id == CEPH_NOSNAP) { |
| 1183 | *snap_size = rbd_dev->header.image_size; |
| 1184 | } else if (rbd_dev->image_format == 1) { |
| 1185 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1186 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1187 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1188 | if (which == BAD_SNAP_INDEX) |
| 1189 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1190 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1191 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1192 | } else { |
| 1193 | u64 size = 0; |
| 1194 | int ret; |
| 1195 | |
| 1196 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1197 | if (ret) |
| 1198 | return ret; |
| 1199 | |
| 1200 | *snap_size = size; |
| 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1206 | u64 *snap_features) |
| 1207 | { |
| 1208 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1209 | if (snap_id == CEPH_NOSNAP) { |
| 1210 | *snap_features = rbd_dev->header.features; |
| 1211 | } else if (rbd_dev->image_format == 1) { |
| 1212 | *snap_features = 0; /* No features for format 1 */ |
| 1213 | } else { |
| 1214 | u64 features = 0; |
| 1215 | int ret; |
| 1216 | |
| 1217 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1218 | if (ret) |
| 1219 | return ret; |
| 1220 | |
| 1221 | *snap_features = features; |
| 1222 | } |
| 1223 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1226 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1227 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1228 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1229 | u64 size = 0; |
| 1230 | u64 features = 0; |
| 1231 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1232 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1233 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1234 | if (ret) |
| 1235 | return ret; |
| 1236 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1237 | if (ret) |
| 1238 | return ret; |
| 1239 | |
| 1240 | rbd_dev->mapping.size = size; |
| 1241 | rbd_dev->mapping.features = features; |
| 1242 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1243 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1246 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1247 | { |
| 1248 | rbd_dev->mapping.size = 0; |
| 1249 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1250 | } |
| 1251 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1252 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1253 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1254 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1255 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1256 | return offset & (segment_size - 1); |
| 1257 | } |
| 1258 | |
| 1259 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1260 | u64 offset, u64 length) |
| 1261 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1262 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1263 | |
| 1264 | offset &= segment_size - 1; |
| 1265 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1266 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1267 | if (offset + length > segment_size) |
| 1268 | length = segment_size - offset; |
| 1269 | |
| 1270 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * bio helpers |
| 1275 | */ |
| 1276 | |
| 1277 | static void bio_chain_put(struct bio *chain) |
| 1278 | { |
| 1279 | struct bio *tmp; |
| 1280 | |
| 1281 | while (chain) { |
| 1282 | tmp = chain; |
| 1283 | chain = chain->bi_next; |
| 1284 | bio_put(tmp); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * zeros a bio chain, starting at specific offset |
| 1290 | */ |
| 1291 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1292 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1293 | struct bio_vec bv; |
| 1294 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1295 | unsigned long flags; |
| 1296 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1297 | int pos = 0; |
| 1298 | |
| 1299 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1300 | bio_for_each_segment(bv, chain, iter) { |
| 1301 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1302 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1303 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1304 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1305 | bv.bv_len - remainder); |
| 1306 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1307 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1308 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1309 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | chain = chain->bi_next; |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1317 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1318 | * starting at the given byte offset from the start of the array and |
| 1319 | * continuing up to the given end offset. The pages array is |
| 1320 | * assumed to be big enough to hold all bytes up to the end. |
| 1321 | */ |
| 1322 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1323 | { |
| 1324 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1325 | |
| 1326 | rbd_assert(end > offset); |
| 1327 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1328 | while (offset < end) { |
| 1329 | size_t page_offset; |
| 1330 | size_t length; |
| 1331 | unsigned long flags; |
| 1332 | void *kaddr; |
| 1333 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1334 | page_offset = offset & ~PAGE_MASK; |
| 1335 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1336 | local_irq_save(flags); |
| 1337 | kaddr = kmap_atomic(*page); |
| 1338 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1339 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1340 | kunmap_atomic(kaddr); |
| 1341 | local_irq_restore(flags); |
| 1342 | |
| 1343 | offset += length; |
| 1344 | page++; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1349 | * Clone a portion of a bio, starting at the given byte offset |
| 1350 | * and continuing for the number of bytes indicated. |
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 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1353 | unsigned int offset, |
| 1354 | unsigned int len, |
| 1355 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1356 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1357 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1358 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1359 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1360 | if (!bio) |
| 1361 | return NULL; /* ENOMEM */ |
| 1362 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1363 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1364 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1365 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1366 | return bio; |
| 1367 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1368 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1369 | /* |
| 1370 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1371 | * into the first bio in the source chain and continuing for the |
| 1372 | * number of bytes indicated. The result is another bio chain of |
| 1373 | * exactly the given length, or a null pointer on error. |
| 1374 | * |
| 1375 | * The bio_src and offset parameters are both in-out. On entry they |
| 1376 | * refer to the first source bio and the offset into that bio where |
| 1377 | * the start of data to be cloned is located. |
| 1378 | * |
| 1379 | * On return, bio_src is updated to refer to the bio in the source |
| 1380 | * chain that contains first un-cloned byte, and *offset will |
| 1381 | * contain the offset of that byte within that bio. |
| 1382 | */ |
| 1383 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1384 | unsigned int *offset, |
| 1385 | unsigned int len, |
| 1386 | gfp_t gfpmask) |
| 1387 | { |
| 1388 | struct bio *bi = *bio_src; |
| 1389 | unsigned int off = *offset; |
| 1390 | struct bio *chain = NULL; |
| 1391 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1392 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1393 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1394 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1395 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1396 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1397 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1398 | end = &chain; |
| 1399 | while (len) { |
| 1400 | unsigned int bi_size; |
| 1401 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1402 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1403 | if (!bi) { |
| 1404 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1405 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1406 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1407 | 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] | 1408 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1409 | if (!bio) |
| 1410 | goto out_err; /* ENOMEM */ |
| 1411 | |
| 1412 | *end = bio; |
| 1413 | end = &bio->bi_next; |
| 1414 | |
| 1415 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1416 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1417 | bi = bi->bi_next; |
| 1418 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1419 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1420 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1421 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1422 | *bio_src = bi; |
| 1423 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1424 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1425 | return chain; |
| 1426 | out_err: |
| 1427 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1428 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1429 | return NULL; |
| 1430 | } |
| 1431 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1432 | /* |
| 1433 | * The default/initial value for all object request flags is 0. For |
| 1434 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1435 | * again. |
| 1436 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1437 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1438 | { |
| 1439 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1440 | struct rbd_device *rbd_dev; |
| 1441 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1442 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1443 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1444 | obj_request); |
| 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1449 | { |
| 1450 | smp_mb(); |
| 1451 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1452 | } |
| 1453 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1454 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1455 | { |
| 1456 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1457 | struct rbd_device *rbd_dev = NULL; |
| 1458 | |
| 1459 | if (obj_request_img_data_test(obj_request)) |
| 1460 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1461 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1462 | obj_request); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1467 | { |
| 1468 | smp_mb(); |
| 1469 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1470 | } |
| 1471 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1472 | /* |
| 1473 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1474 | * flag. The latter is set based on the "exists" value provided. |
| 1475 | * |
| 1476 | * Note that for our purposes once an object exists it never goes |
| 1477 | * away again. It's possible that the response from two existence |
| 1478 | * checks are separated by the creation of the target object, and |
| 1479 | * the first ("doesn't exist") response arrives *after* the second |
| 1480 | * ("does exist"). In that case we ignore the second one. |
| 1481 | */ |
| 1482 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1483 | bool exists) |
| 1484 | { |
| 1485 | if (exists) |
| 1486 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1487 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1488 | smp_mb(); |
| 1489 | } |
| 1490 | |
| 1491 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1492 | { |
| 1493 | smp_mb(); |
| 1494 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1495 | } |
| 1496 | |
| 1497 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1498 | { |
| 1499 | smp_mb(); |
| 1500 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1501 | } |
| 1502 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1503 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1504 | { |
| 1505 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1506 | |
| 1507 | return obj_request->img_offset < |
| 1508 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1509 | } |
| 1510 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1511 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1512 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1513 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1514 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1515 | kref_get(&obj_request->kref); |
| 1516 | } |
| 1517 | |
| 1518 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1519 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1520 | { |
| 1521 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1522 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1523 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1524 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1525 | } |
| 1526 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1527 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1528 | { |
| 1529 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1530 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1531 | kref_get(&img_request->kref); |
| 1532 | } |
| 1533 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1534 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1535 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1536 | static void rbd_img_request_destroy(struct kref *kref); |
| 1537 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1538 | { |
| 1539 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1540 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1541 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1542 | if (img_request_child_test(img_request)) |
| 1543 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1544 | else |
| 1545 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1549 | struct rbd_obj_request *obj_request) |
| 1550 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1551 | rbd_assert(obj_request->img_request == NULL); |
| 1552 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1553 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1554 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1555 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1556 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1557 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1558 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1559 | img_request->obj_request_count++; |
| 1560 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1561 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1562 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1566 | struct rbd_obj_request *obj_request) |
| 1567 | { |
| 1568 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1569 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1570 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1571 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1572 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1573 | rbd_assert(img_request->obj_request_count > 0); |
| 1574 | img_request->obj_request_count--; |
| 1575 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1576 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1577 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1578 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1579 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1580 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1581 | rbd_obj_request_put(obj_request); |
| 1582 | } |
| 1583 | |
| 1584 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1585 | { |
| 1586 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1587 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1588 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1589 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1590 | return true; |
| 1591 | default: |
| 1592 | return false; |
| 1593 | } |
| 1594 | } |
| 1595 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1596 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1597 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1598 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1599 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1600 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1601 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1602 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1603 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1604 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1605 | if (obj_request_img_data_test(obj_request)) { |
| 1606 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1607 | rbd_img_request_get(obj_request->img_request); |
| 1608 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1609 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1613 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1614 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1615 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1616 | |
| 1617 | /* |
| 1618 | * If no error occurred, compute the aggregate transfer |
| 1619 | * count for the image request. We could instead use |
| 1620 | * atomic64_cmpxchg() to update it as each object request |
| 1621 | * completes; not clear which way is better off hand. |
| 1622 | */ |
| 1623 | if (!img_request->result) { |
| 1624 | struct rbd_obj_request *obj_request; |
| 1625 | u64 xferred = 0; |
| 1626 | |
| 1627 | for_each_obj_request(img_request, obj_request) |
| 1628 | xferred += obj_request->xferred; |
| 1629 | img_request->xferred = xferred; |
| 1630 | } |
| 1631 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1632 | if (img_request->callback) |
| 1633 | img_request->callback(img_request); |
| 1634 | else |
| 1635 | rbd_img_request_put(img_request); |
| 1636 | } |
| 1637 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1638 | /* |
| 1639 | * The default/initial value for all image request flags is 0. Each |
| 1640 | * is conditionally set to 1 at image request initialization time |
| 1641 | * and currently never change thereafter. |
| 1642 | */ |
| 1643 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1644 | { |
| 1645 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1646 | smp_mb(); |
| 1647 | } |
| 1648 | |
| 1649 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1650 | { |
| 1651 | smp_mb(); |
| 1652 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1653 | } |
| 1654 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1655 | /* |
| 1656 | * Set the discard flag when the img_request is an discard request |
| 1657 | */ |
| 1658 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1659 | { |
| 1660 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1661 | smp_mb(); |
| 1662 | } |
| 1663 | |
| 1664 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1665 | { |
| 1666 | smp_mb(); |
| 1667 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1668 | } |
| 1669 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1670 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1671 | { |
| 1672 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1673 | smp_mb(); |
| 1674 | } |
| 1675 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1676 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1677 | { |
| 1678 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1679 | smp_mb(); |
| 1680 | } |
| 1681 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1682 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1683 | { |
| 1684 | smp_mb(); |
| 1685 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1686 | } |
| 1687 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1688 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1689 | { |
| 1690 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1691 | smp_mb(); |
| 1692 | } |
| 1693 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1694 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1695 | { |
| 1696 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1697 | smp_mb(); |
| 1698 | } |
| 1699 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1700 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1701 | { |
| 1702 | smp_mb(); |
| 1703 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1704 | } |
| 1705 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1706 | static enum obj_operation_type |
| 1707 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1708 | { |
| 1709 | if (img_request_write_test(img_request)) |
| 1710 | return OBJ_OP_WRITE; |
| 1711 | else if (img_request_discard_test(img_request)) |
| 1712 | return OBJ_OP_DISCARD; |
| 1713 | else |
| 1714 | return OBJ_OP_READ; |
| 1715 | } |
| 1716 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1717 | static void |
| 1718 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1719 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1720 | u64 xferred = obj_request->xferred; |
| 1721 | u64 length = obj_request->length; |
| 1722 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1723 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1724 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1725 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1726 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1727 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1728 | * length of the request. A short read also implies zero-fill |
| 1729 | * to the end of the request. An error requires the whole |
| 1730 | * length of the request to be reported finished with an error |
| 1731 | * to the block layer. In each case we update the xferred |
| 1732 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1733 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1734 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1735 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1736 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1737 | zero_bio_chain(obj_request->bio_list, 0); |
| 1738 | else |
| 1739 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1740 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1741 | } else if (xferred < length && !obj_request->result) { |
| 1742 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1743 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1744 | else |
| 1745 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1746 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1747 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1748 | obj_request_done_set(obj_request); |
| 1749 | } |
| 1750 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1751 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1752 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1753 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1754 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1755 | if (obj_request->callback) |
| 1756 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1757 | else |
| 1758 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1759 | } |
| 1760 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1761 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1762 | { |
| 1763 | obj_request->result = err; |
| 1764 | obj_request->xferred = 0; |
| 1765 | /* |
| 1766 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1767 | * rbd_img_obj_callback() |
| 1768 | */ |
| 1769 | if (obj_request_img_data_test(obj_request)) { |
| 1770 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1771 | rbd_img_request_get(obj_request->img_request); |
| 1772 | } |
| 1773 | obj_request_done_set(obj_request); |
| 1774 | rbd_obj_request_complete(obj_request); |
| 1775 | } |
| 1776 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1777 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1778 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1779 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1780 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1781 | bool layered = false; |
| 1782 | |
| 1783 | if (obj_request_img_data_test(obj_request)) { |
| 1784 | img_request = obj_request->img_request; |
| 1785 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1786 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1787 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1788 | |
| 1789 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1790 | obj_request, img_request, obj_request->result, |
| 1791 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1792 | if (layered && obj_request->result == -ENOENT && |
| 1793 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1794 | rbd_img_parent_read(obj_request); |
| 1795 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1796 | rbd_img_obj_request_read_callback(obj_request); |
| 1797 | else |
| 1798 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1799 | } |
| 1800 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1801 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1802 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1803 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1804 | obj_request->result, obj_request->length); |
| 1805 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1806 | * There is no such thing as a successful short write. Set |
| 1807 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1808 | */ |
| 1809 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1810 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1811 | } |
| 1812 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1813 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1814 | { |
| 1815 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1816 | obj_request->result, obj_request->length); |
| 1817 | /* |
| 1818 | * There is no such thing as a successful short discard. Set |
| 1819 | * it to our originally-requested length. |
| 1820 | */ |
| 1821 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1822 | /* discarding a non-existent object is not a problem */ |
| 1823 | if (obj_request->result == -ENOENT) |
| 1824 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1825 | obj_request_done_set(obj_request); |
| 1826 | } |
| 1827 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1828 | /* |
| 1829 | * For a simple stat call there's nothing to do. We'll do more if |
| 1830 | * this is part of a write sequence for a layered image. |
| 1831 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1832 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1833 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1834 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1835 | obj_request_done_set(obj_request); |
| 1836 | } |
| 1837 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1838 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1839 | { |
| 1840 | dout("%s: obj %p\n", __func__, obj_request); |
| 1841 | |
| 1842 | if (obj_request_img_data_test(obj_request)) |
| 1843 | rbd_osd_copyup_callback(obj_request); |
| 1844 | else |
| 1845 | obj_request_done_set(obj_request); |
| 1846 | } |
| 1847 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1848 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1849 | { |
| 1850 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1851 | u16 opcode; |
| 1852 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1853 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1854 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1855 | if (obj_request_img_data_test(obj_request)) { |
| 1856 | rbd_assert(obj_request->img_request); |
| 1857 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1858 | } else { |
| 1859 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1860 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1861 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1862 | if (osd_req->r_result < 0) |
| 1863 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1864 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1865 | /* |
| 1866 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1867 | * passed to the block layer, which just supports a 32-bit |
| 1868 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1869 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1870 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1871 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1872 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1873 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1874 | switch (opcode) { |
| 1875 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1876 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1877 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1878 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1879 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1880 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1881 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1882 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1883 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1884 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1885 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1886 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1887 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1888 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1889 | case CEPH_OSD_OP_DELETE: |
| 1890 | case CEPH_OSD_OP_TRUNCATE: |
| 1891 | case CEPH_OSD_OP_ZERO: |
| 1892 | rbd_osd_discard_callback(obj_request); |
| 1893 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1894 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1895 | rbd_osd_call_callback(obj_request); |
| 1896 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1897 | default: |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1898 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
| 1899 | obj_request->object_no, opcode); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1900 | break; |
| 1901 | } |
| 1902 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1903 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1904 | rbd_obj_request_complete(obj_request); |
| 1905 | } |
| 1906 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1907 | 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] | 1908 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1909 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1910 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1911 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1912 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1916 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1917 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1918 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1919 | osd_req->r_mtime = CURRENT_TIME; |
| 1920 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1921 | } |
| 1922 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1923 | static struct ceph_osd_request * |
| 1924 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1925 | struct ceph_snap_context *snapc, |
| 1926 | int num_ops, unsigned int flags, |
| 1927 | struct rbd_obj_request *obj_request) |
| 1928 | { |
| 1929 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1930 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1931 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1932 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1933 | |
| 1934 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1935 | if (!req) |
| 1936 | return NULL; |
| 1937 | |
| 1938 | req->r_flags = flags; |
| 1939 | req->r_callback = rbd_osd_req_callback; |
| 1940 | req->r_priv = obj_request; |
| 1941 | |
| 1942 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1943 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1944 | rbd_dev->header.object_prefix, obj_request->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1945 | goto err_req; |
| 1946 | |
| 1947 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1948 | goto err_req; |
| 1949 | |
| 1950 | return req; |
| 1951 | |
| 1952 | err_req: |
| 1953 | ceph_osdc_put_request(req); |
| 1954 | return NULL; |
| 1955 | } |
| 1956 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1957 | /* |
| 1958 | * Create an osd request. A read request has one osd op (read). |
| 1959 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1960 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1961 | * technically osd watch is a write request, hence this distinction.) |
| 1962 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1963 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1964 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1965 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1966 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1967 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1968 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1969 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1970 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1971 | if (obj_request_img_data_test(obj_request) && |
| 1972 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1973 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1974 | if (op_type == OBJ_OP_WRITE) { |
| 1975 | rbd_assert(img_request_write_test(img_request)); |
| 1976 | } else { |
| 1977 | rbd_assert(img_request_discard_test(img_request)); |
| 1978 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1979 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1980 | } |
| 1981 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1982 | 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] | 1983 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1984 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 1985 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 1986 | CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1987 | } |
| 1988 | |
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 | * Create a copyup osd request based on the information in the object |
| 1991 | * request supplied. A copyup request has two or three osd ops, a |
| 1992 | * copyup method call, potentially a hint op, and a write or truncate |
| 1993 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1994 | */ |
| 1995 | static struct ceph_osd_request * |
| 1996 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 1997 | { |
| 1998 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1999 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2000 | |
| 2001 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2002 | img_request = obj_request->img_request; |
| 2003 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2004 | rbd_assert(img_request_write_test(img_request) || |
| 2005 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2006 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2007 | if (img_request_discard_test(img_request)) |
| 2008 | num_osd_ops = 2; |
| 2009 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2010 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 2011 | img_request->snapc, num_osd_ops, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 2012 | CEPH_OSD_FLAG_WRITE, obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2013 | } |
| 2014 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2015 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2016 | { |
| 2017 | ceph_osdc_put_request(osd_req); |
| 2018 | } |
| 2019 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2020 | static struct rbd_obj_request * |
| 2021 | rbd_obj_request_create(enum obj_request_type type) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2022 | { |
| 2023 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2024 | |
| 2025 | rbd_assert(obj_request_type_valid(type)); |
| 2026 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2027 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2028 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2029 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2030 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2031 | obj_request->which = BAD_WHICH; |
| 2032 | obj_request->type = type; |
| 2033 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2034 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2035 | kref_init(&obj_request->kref); |
| 2036 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2037 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2038 | return obj_request; |
| 2039 | } |
| 2040 | |
| 2041 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2042 | { |
| 2043 | struct rbd_obj_request *obj_request; |
| 2044 | |
| 2045 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2046 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2047 | dout("%s: obj %p\n", __func__, obj_request); |
| 2048 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2049 | rbd_assert(obj_request->img_request == NULL); |
| 2050 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2051 | |
| 2052 | if (obj_request->osd_req) |
| 2053 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2054 | |
| 2055 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2056 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2057 | case OBJ_REQUEST_NODATA: |
| 2058 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2059 | case OBJ_REQUEST_BIO: |
| 2060 | if (obj_request->bio_list) |
| 2061 | bio_chain_put(obj_request->bio_list); |
| 2062 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2063 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2064 | /* img_data requests don't own their page array */ |
| 2065 | if (obj_request->pages && |
| 2066 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2067 | ceph_release_page_vector(obj_request->pages, |
| 2068 | obj_request->page_count); |
| 2069 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2070 | } |
| 2071 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2072 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2073 | } |
| 2074 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2075 | /* It's OK to call this for a device with no parent */ |
| 2076 | |
| 2077 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2078 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2079 | { |
| 2080 | rbd_dev_remove_parent(rbd_dev); |
| 2081 | rbd_spec_put(rbd_dev->parent_spec); |
| 2082 | rbd_dev->parent_spec = NULL; |
| 2083 | rbd_dev->parent_overlap = 0; |
| 2084 | } |
| 2085 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2086 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2087 | * Parent image reference counting is used to determine when an |
| 2088 | * image's parent fields can be safely torn down--after there are no |
| 2089 | * more in-flight requests to the parent image. When the last |
| 2090 | * reference is dropped, cleaning them up is safe. |
| 2091 | */ |
| 2092 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2093 | { |
| 2094 | int counter; |
| 2095 | |
| 2096 | if (!rbd_dev->parent_spec) |
| 2097 | return; |
| 2098 | |
| 2099 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2100 | if (counter > 0) |
| 2101 | return; |
| 2102 | |
| 2103 | /* Last reference; clean up parent data structures */ |
| 2104 | |
| 2105 | if (!counter) |
| 2106 | rbd_dev_unparent(rbd_dev); |
| 2107 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2108 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * If an image has a non-zero parent overlap, get a reference to its |
| 2113 | * parent. |
| 2114 | * |
| 2115 | * Returns true if the rbd device has a parent with a non-zero |
| 2116 | * overlap and a reference for it was successfully taken, or |
| 2117 | * false otherwise. |
| 2118 | */ |
| 2119 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2120 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2121 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2122 | |
| 2123 | if (!rbd_dev->parent_spec) |
| 2124 | return false; |
| 2125 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2126 | down_read(&rbd_dev->header_rwsem); |
| 2127 | if (rbd_dev->parent_overlap) |
| 2128 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2129 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2130 | |
| 2131 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2132 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2133 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2134 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2135 | } |
| 2136 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2137 | /* |
| 2138 | * Caller is responsible for filling in the list of object requests |
| 2139 | * that comprises the image request, and the Linux request pointer |
| 2140 | * (if there is one). |
| 2141 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2142 | static struct rbd_img_request *rbd_img_request_create( |
| 2143 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2144 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2145 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2146 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2147 | { |
| 2148 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2149 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2150 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2151 | if (!img_request) |
| 2152 | return NULL; |
| 2153 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2154 | img_request->rq = NULL; |
| 2155 | img_request->rbd_dev = rbd_dev; |
| 2156 | img_request->offset = offset; |
| 2157 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2158 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2159 | if (op_type == OBJ_OP_DISCARD) { |
| 2160 | img_request_discard_set(img_request); |
| 2161 | img_request->snapc = snapc; |
| 2162 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2163 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2164 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2165 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2166 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2167 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2168 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2169 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2170 | spin_lock_init(&img_request->completion_lock); |
| 2171 | img_request->next_completion = 0; |
| 2172 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2173 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2174 | img_request->obj_request_count = 0; |
| 2175 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2176 | kref_init(&img_request->kref); |
| 2177 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2178 | 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] | 2179 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2180 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2181 | return img_request; |
| 2182 | } |
| 2183 | |
| 2184 | static void rbd_img_request_destroy(struct kref *kref) |
| 2185 | { |
| 2186 | struct rbd_img_request *img_request; |
| 2187 | struct rbd_obj_request *obj_request; |
| 2188 | struct rbd_obj_request *next_obj_request; |
| 2189 | |
| 2190 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2191 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2192 | dout("%s: img %p\n", __func__, img_request); |
| 2193 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2194 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2195 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2196 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2197 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2198 | if (img_request_layered_test(img_request)) { |
| 2199 | img_request_layered_clear(img_request); |
| 2200 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2201 | } |
| 2202 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2203 | if (img_request_write_test(img_request) || |
| 2204 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2205 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2206 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2207 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2208 | } |
| 2209 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2210 | static struct rbd_img_request *rbd_parent_request_create( |
| 2211 | struct rbd_obj_request *obj_request, |
| 2212 | u64 img_offset, u64 length) |
| 2213 | { |
| 2214 | struct rbd_img_request *parent_request; |
| 2215 | struct rbd_device *rbd_dev; |
| 2216 | |
| 2217 | rbd_assert(obj_request->img_request); |
| 2218 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2219 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2220 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2221 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2222 | if (!parent_request) |
| 2223 | return NULL; |
| 2224 | |
| 2225 | img_request_child_set(parent_request); |
| 2226 | rbd_obj_request_get(obj_request); |
| 2227 | parent_request->obj_request = obj_request; |
| 2228 | |
| 2229 | return parent_request; |
| 2230 | } |
| 2231 | |
| 2232 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2233 | { |
| 2234 | struct rbd_img_request *parent_request; |
| 2235 | struct rbd_obj_request *orig_request; |
| 2236 | |
| 2237 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2238 | orig_request = parent_request->obj_request; |
| 2239 | |
| 2240 | parent_request->obj_request = NULL; |
| 2241 | rbd_obj_request_put(orig_request); |
| 2242 | img_request_child_clear(parent_request); |
| 2243 | |
| 2244 | rbd_img_request_destroy(kref); |
| 2245 | } |
| 2246 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2247 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2248 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2249 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2250 | unsigned int xferred; |
| 2251 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2252 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2253 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2254 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2255 | img_request = obj_request->img_request; |
| 2256 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2257 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2258 | xferred = (unsigned int)obj_request->xferred; |
| 2259 | result = obj_request->result; |
| 2260 | if (result) { |
| 2261 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2262 | enum obj_operation_type op_type; |
| 2263 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2264 | if (img_request_discard_test(img_request)) |
| 2265 | op_type = OBJ_OP_DISCARD; |
| 2266 | else if (img_request_write_test(img_request)) |
| 2267 | op_type = OBJ_OP_WRITE; |
| 2268 | else |
| 2269 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2270 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2271 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2272 | obj_op_name(op_type), obj_request->length, |
| 2273 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2274 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2275 | result, xferred); |
| 2276 | if (!img_request->result) |
| 2277 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2278 | /* |
| 2279 | * Need to end I/O on the entire obj_request worth of |
| 2280 | * bytes in case of error. |
| 2281 | */ |
| 2282 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2283 | } |
| 2284 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2285 | if (img_request_child_test(img_request)) { |
| 2286 | rbd_assert(img_request->obj_request != NULL); |
| 2287 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2288 | } else { |
| 2289 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2290 | |
| 2291 | more = blk_update_request(img_request->rq, result, xferred); |
| 2292 | if (!more) |
| 2293 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2297 | } |
| 2298 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2299 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2300 | { |
| 2301 | struct rbd_img_request *img_request; |
| 2302 | u32 which = obj_request->which; |
| 2303 | bool more = true; |
| 2304 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2305 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2306 | img_request = obj_request->img_request; |
| 2307 | |
| 2308 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2309 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2310 | rbd_assert(img_request->obj_request_count > 0); |
| 2311 | rbd_assert(which != BAD_WHICH); |
| 2312 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2313 | |
| 2314 | spin_lock_irq(&img_request->completion_lock); |
| 2315 | if (which != img_request->next_completion) |
| 2316 | goto out; |
| 2317 | |
| 2318 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2319 | rbd_assert(more); |
| 2320 | rbd_assert(which < img_request->obj_request_count); |
| 2321 | |
| 2322 | if (!obj_request_done_test(obj_request)) |
| 2323 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2324 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2325 | which++; |
| 2326 | } |
| 2327 | |
| 2328 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2329 | img_request->next_completion = which; |
| 2330 | out: |
| 2331 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2332 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2333 | |
| 2334 | if (!more) |
| 2335 | rbd_img_request_complete(img_request); |
| 2336 | } |
| 2337 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2338 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2339 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2340 | * them for submission. num_ops is the current number of |
| 2341 | * osd operations already to the object request. |
| 2342 | */ |
| 2343 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2344 | struct ceph_osd_request *osd_request, |
| 2345 | enum obj_operation_type op_type, |
| 2346 | unsigned int num_ops) |
| 2347 | { |
| 2348 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2349 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2350 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2351 | u64 offset = obj_request->offset; |
| 2352 | u64 length = obj_request->length; |
| 2353 | u64 img_end; |
| 2354 | u16 opcode; |
| 2355 | |
| 2356 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2357 | if (!offset && length == object_size && |
| 2358 | (!img_request_layered_test(img_request) || |
| 2359 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2360 | opcode = CEPH_OSD_OP_DELETE; |
| 2361 | } else if ((offset + length == object_size)) { |
| 2362 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2363 | } else { |
| 2364 | down_read(&rbd_dev->header_rwsem); |
| 2365 | img_end = rbd_dev->header.image_size; |
| 2366 | up_read(&rbd_dev->header_rwsem); |
| 2367 | |
| 2368 | if (obj_request->img_offset + length == img_end) |
| 2369 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2370 | else |
| 2371 | opcode = CEPH_OSD_OP_ZERO; |
| 2372 | } |
| 2373 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2374 | if (!offset && length == object_size) |
| 2375 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2376 | else |
| 2377 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2378 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2379 | object_size, object_size); |
| 2380 | num_ops++; |
| 2381 | } else { |
| 2382 | opcode = CEPH_OSD_OP_READ; |
| 2383 | } |
| 2384 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2385 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2386 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2387 | else |
| 2388 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2389 | offset, length, 0, 0); |
| 2390 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2391 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2392 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2393 | obj_request->bio_list, length); |
| 2394 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2395 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2396 | obj_request->pages, length, |
| 2397 | offset & ~PAGE_MASK, false, false); |
| 2398 | |
| 2399 | /* Discards are also writes */ |
| 2400 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2401 | rbd_osd_req_format_write(obj_request); |
| 2402 | else |
| 2403 | rbd_osd_req_format_read(obj_request); |
| 2404 | } |
| 2405 | |
| 2406 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2407 | * Split up an image request into one or more object requests, each |
| 2408 | * to a different object. The "type" parameter indicates whether |
| 2409 | * "data_desc" is the pointer to the head of a list of bio |
| 2410 | * structures, or the base of a page array. In either case this |
| 2411 | * function assumes data_desc describes memory sufficient to hold |
| 2412 | * all data described by the image request. |
| 2413 | */ |
| 2414 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2415 | enum obj_request_type type, |
| 2416 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2417 | { |
| 2418 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2419 | struct rbd_obj_request *obj_request = NULL; |
| 2420 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2421 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2422 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2423 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2424 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2425 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2426 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2427 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2428 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2429 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2430 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2431 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2432 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2433 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2434 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2435 | |
| 2436 | if (type == OBJ_REQUEST_BIO) { |
| 2437 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2438 | rbd_assert(img_offset == |
| 2439 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2440 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2441 | pages = data_desc; |
| 2442 | } |
| 2443 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2444 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2445 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2446 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2447 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2448 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2449 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2450 | obj_request = rbd_obj_request_create(type); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2451 | if (!obj_request) |
| 2452 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2453 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2454 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2455 | obj_request->offset = offset; |
| 2456 | obj_request->length = length; |
| 2457 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2458 | /* |
| 2459 | * set obj_request->img_request before creating the |
| 2460 | * osd_request so that it gets the right snapc |
| 2461 | */ |
| 2462 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2463 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2464 | if (type == OBJ_REQUEST_BIO) { |
| 2465 | unsigned int clone_size; |
| 2466 | |
| 2467 | rbd_assert(length <= (u64)UINT_MAX); |
| 2468 | clone_size = (unsigned int)length; |
| 2469 | obj_request->bio_list = |
| 2470 | bio_chain_clone_range(&bio_list, |
| 2471 | &bio_offset, |
| 2472 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2473 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2474 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2475 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2476 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2477 | unsigned int page_count; |
| 2478 | |
| 2479 | obj_request->pages = pages; |
| 2480 | page_count = (u32)calc_pages_for(offset, length); |
| 2481 | obj_request->page_count = page_count; |
| 2482 | if ((offset + length) & ~PAGE_MASK) |
| 2483 | page_count--; /* more on last page */ |
| 2484 | pages += page_count; |
| 2485 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2486 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2487 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2488 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2489 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2490 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2491 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2492 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2493 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2494 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2495 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2496 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2497 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2498 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2499 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2500 | resid -= length; |
| 2501 | } |
| 2502 | |
| 2503 | return 0; |
| 2504 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2505 | out_unwind: |
| 2506 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2507 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2508 | |
| 2509 | return -ENOMEM; |
| 2510 | } |
| 2511 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2512 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2513 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2514 | { |
| 2515 | struct rbd_img_request *img_request; |
| 2516 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2517 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2518 | u32 page_count; |
| 2519 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2520 | dout("%s: obj %p\n", __func__, obj_request); |
| 2521 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2522 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2523 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2524 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2525 | img_request = obj_request->img_request; |
| 2526 | rbd_assert(img_request); |
| 2527 | |
| 2528 | rbd_dev = img_request->rbd_dev; |
| 2529 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2530 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2531 | pages = obj_request->copyup_pages; |
| 2532 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2533 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2534 | page_count = obj_request->copyup_page_count; |
| 2535 | rbd_assert(page_count); |
| 2536 | obj_request->copyup_page_count = 0; |
| 2537 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2538 | |
| 2539 | /* |
| 2540 | * We want the transfer count to reflect the size of the |
| 2541 | * original write request. There is no such thing as a |
| 2542 | * successful short write, so if the request was successful |
| 2543 | * we can just set it to the originally-requested length. |
| 2544 | */ |
| 2545 | if (!obj_request->result) |
| 2546 | obj_request->xferred = obj_request->length; |
| 2547 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2548 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2552 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2553 | { |
| 2554 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2555 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2556 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2557 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2558 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2559 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2560 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2561 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2562 | |
| 2563 | rbd_assert(img_request_child_test(img_request)); |
| 2564 | |
| 2565 | /* First get what we need from the image request */ |
| 2566 | |
| 2567 | pages = img_request->copyup_pages; |
| 2568 | rbd_assert(pages != NULL); |
| 2569 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2570 | page_count = img_request->copyup_page_count; |
| 2571 | rbd_assert(page_count); |
| 2572 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2573 | |
| 2574 | orig_request = img_request->obj_request; |
| 2575 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2576 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2577 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2578 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2579 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2580 | rbd_img_request_put(img_request); |
| 2581 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2582 | rbd_assert(orig_request->img_request); |
| 2583 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2584 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2585 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2586 | /* |
| 2587 | * If the overlap has become 0 (most likely because the |
| 2588 | * image has been flattened) we need to free the pages |
| 2589 | * and re-submit the original write request. |
| 2590 | */ |
| 2591 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2592 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2593 | rbd_obj_request_submit(orig_request); |
| 2594 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2598 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2599 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2600 | /* |
| 2601 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2602 | * 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] | 2603 | * request. Allocate the new copyup osd request for the |
| 2604 | * original request, and release the old one. |
| 2605 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2606 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2607 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2608 | if (!osd_req) |
| 2609 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2610 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2611 | orig_request->osd_req = osd_req; |
| 2612 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2613 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2614 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2615 | /* Initialize the copyup op */ |
| 2616 | |
| 2617 | 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] | 2618 | 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] | 2619 | false, false); |
| 2620 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2621 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2622 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2623 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2624 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2625 | |
| 2626 | /* All set, send it off. */ |
| 2627 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2628 | rbd_obj_request_submit(orig_request); |
| 2629 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2630 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2631 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2632 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2633 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* |
| 2637 | * Read from the parent image the range of data that covers the |
| 2638 | * entire target of the given object request. This is used for |
| 2639 | * satisfying a layered image write request when the target of an |
| 2640 | * object request from the image request does not exist. |
| 2641 | * |
| 2642 | * A page array big enough to hold the returned data is allocated |
| 2643 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2644 | * When the read completes, this page array will be transferred to |
| 2645 | * the original object request for the copyup operation. |
| 2646 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2647 | * If an error occurs, it is recorded as the result of the original |
| 2648 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2649 | */ |
| 2650 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2651 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2652 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2653 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2654 | u64 img_offset; |
| 2655 | u64 length; |
| 2656 | struct page **pages = NULL; |
| 2657 | u32 page_count; |
| 2658 | int result; |
| 2659 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2660 | rbd_assert(rbd_dev->parent != NULL); |
| 2661 | |
| 2662 | /* |
| 2663 | * Determine the byte range covered by the object in the |
| 2664 | * child image to which the original request was to be sent. |
| 2665 | */ |
| 2666 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2667 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2668 | |
| 2669 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2670 | * There is no defined parent data beyond the parent |
| 2671 | * overlap, so limit what we read at that boundary if |
| 2672 | * necessary. |
| 2673 | */ |
| 2674 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2675 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2676 | length = rbd_dev->parent_overlap - img_offset; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2680 | * Allocate a page array big enough to receive the data read |
| 2681 | * from the parent. |
| 2682 | */ |
| 2683 | page_count = (u32)calc_pages_for(0, length); |
| 2684 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2685 | if (IS_ERR(pages)) { |
| 2686 | result = PTR_ERR(pages); |
| 2687 | pages = NULL; |
| 2688 | goto out_err; |
| 2689 | } |
| 2690 | |
| 2691 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2692 | parent_request = rbd_parent_request_create(obj_request, |
| 2693 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2694 | if (!parent_request) |
| 2695 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2696 | |
| 2697 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2698 | if (result) |
| 2699 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2700 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2701 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2702 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2703 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2704 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2705 | result = rbd_img_request_submit(parent_request); |
| 2706 | if (!result) |
| 2707 | return 0; |
| 2708 | |
| 2709 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2710 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2711 | parent_request->obj_request = NULL; |
| 2712 | rbd_obj_request_put(obj_request); |
| 2713 | out_err: |
| 2714 | if (pages) |
| 2715 | ceph_release_page_vector(pages, page_count); |
| 2716 | if (parent_request) |
| 2717 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2718 | return result; |
| 2719 | } |
| 2720 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2721 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2722 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2723 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2724 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2725 | int result; |
| 2726 | |
| 2727 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2728 | |
| 2729 | /* |
| 2730 | * All we need from the object request is the original |
| 2731 | * request and the result of the STAT op. Grab those, then |
| 2732 | * we're done with the request. |
| 2733 | */ |
| 2734 | orig_request = obj_request->obj_request; |
| 2735 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2736 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2737 | rbd_assert(orig_request); |
| 2738 | rbd_assert(orig_request->img_request); |
| 2739 | |
| 2740 | result = obj_request->result; |
| 2741 | obj_request->result = 0; |
| 2742 | |
| 2743 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2744 | obj_request, orig_request, result, |
| 2745 | obj_request->xferred, obj_request->length); |
| 2746 | rbd_obj_request_put(obj_request); |
| 2747 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2748 | /* |
| 2749 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2750 | * image has been flattened) we need to re-submit the |
| 2751 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2752 | */ |
| 2753 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2754 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2755 | rbd_obj_request_submit(orig_request); |
| 2756 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2757 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2758 | |
| 2759 | /* |
| 2760 | * Our only purpose here is to determine whether the object |
| 2761 | * exists, and we don't want to treat the non-existence as |
| 2762 | * an error. If something else comes back, transfer the |
| 2763 | * error to the original request and complete it now. |
| 2764 | */ |
| 2765 | if (!result) { |
| 2766 | obj_request_existence_set(orig_request, true); |
| 2767 | } else if (result == -ENOENT) { |
| 2768 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2769 | } else { |
| 2770 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | /* |
| 2774 | * Resubmit the original request now that we have recorded |
| 2775 | * whether the target object exists. |
| 2776 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2777 | result = rbd_img_obj_request_submit(orig_request); |
| 2778 | if (result) |
| 2779 | goto fail_orig_request; |
| 2780 | |
| 2781 | return; |
| 2782 | |
| 2783 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2784 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2785 | } |
| 2786 | |
| 2787 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2788 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2789 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2790 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2791 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2792 | u32 page_count; |
| 2793 | size_t size; |
| 2794 | int ret; |
| 2795 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2796 | stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2797 | if (!stat_request) |
| 2798 | return -ENOMEM; |
| 2799 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2800 | stat_request->object_no = obj_request->object_no; |
| 2801 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2802 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2803 | stat_request); |
| 2804 | if (!stat_request->osd_req) { |
| 2805 | ret = -ENOMEM; |
| 2806 | goto fail_stat_request; |
| 2807 | } |
| 2808 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2809 | /* |
| 2810 | * The response data for a STAT call consists of: |
| 2811 | * le64 length; |
| 2812 | * struct { |
| 2813 | * le32 tv_sec; |
| 2814 | * le32 tv_nsec; |
| 2815 | * } mtime; |
| 2816 | */ |
| 2817 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2818 | page_count = (u32)calc_pages_for(0, size); |
| 2819 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2820 | if (IS_ERR(pages)) { |
| 2821 | ret = PTR_ERR(pages); |
| 2822 | goto fail_stat_request; |
| 2823 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2824 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2825 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2826 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2827 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2828 | |
| 2829 | rbd_obj_request_get(obj_request); |
| 2830 | stat_request->obj_request = obj_request; |
| 2831 | stat_request->pages = pages; |
| 2832 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2833 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2834 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2835 | rbd_obj_request_submit(stat_request); |
| 2836 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2837 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2838 | fail_stat_request: |
| 2839 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2840 | return ret; |
| 2841 | } |
| 2842 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2843 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2844 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2845 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2846 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2847 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2848 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2849 | if (!img_request_write_test(img_request) && |
| 2850 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2851 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2852 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2853 | /* Non-layered writes */ |
| 2854 | if (!img_request_layered_test(img_request)) |
| 2855 | return true; |
| 2856 | |
| 2857 | /* |
| 2858 | * Layered writes outside of the parent overlap range don't |
| 2859 | * share any data with the parent. |
| 2860 | */ |
| 2861 | if (!obj_request_overlaps_parent(obj_request)) |
| 2862 | return true; |
| 2863 | |
| 2864 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2865 | * Entire-object layered writes - we will overwrite whatever |
| 2866 | * parent data there is anyway. |
| 2867 | */ |
| 2868 | if (!obj_request->offset && |
| 2869 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2870 | return true; |
| 2871 | |
| 2872 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2873 | * If the object is known to already exist, its parent data has |
| 2874 | * already been copied. |
| 2875 | */ |
| 2876 | if (obj_request_known_test(obj_request) && |
| 2877 | obj_request_exists_test(obj_request)) |
| 2878 | return true; |
| 2879 | |
| 2880 | return false; |
| 2881 | } |
| 2882 | |
| 2883 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2884 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2885 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2886 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2887 | rbd_assert(obj_request->img_request); |
| 2888 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2889 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2890 | rbd_obj_request_submit(obj_request); |
| 2891 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2895 | * It's a layered write. The target object might exist but |
| 2896 | * we may not know that yet. If we know it doesn't exist, |
| 2897 | * start by reading the data for the full target object from |
| 2898 | * 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] | 2899 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2900 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2901 | return rbd_img_obj_parent_read_full(obj_request); |
| 2902 | |
| 2903 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2904 | |
| 2905 | return rbd_img_obj_exists_submit(obj_request); |
| 2906 | } |
| 2907 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2908 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2909 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2910 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2911 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2912 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2913 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2914 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2915 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2916 | rbd_img_request_get(img_request); |
| 2917 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2918 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2919 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2920 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2921 | } |
| 2922 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2923 | out_put_ireq: |
| 2924 | rbd_img_request_put(img_request); |
| 2925 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2926 | } |
| 2927 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2928 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2929 | { |
| 2930 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2931 | struct rbd_device *rbd_dev; |
| 2932 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2933 | u64 img_xferred; |
| 2934 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2935 | |
| 2936 | rbd_assert(img_request_child_test(img_request)); |
| 2937 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2938 | /* First get what we need from the image request and release it */ |
| 2939 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2940 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2941 | img_xferred = img_request->xferred; |
| 2942 | img_result = img_request->result; |
| 2943 | rbd_img_request_put(img_request); |
| 2944 | |
| 2945 | /* |
| 2946 | * If the overlap has become 0 (most likely because the |
| 2947 | * image has been flattened) we need to re-submit the |
| 2948 | * original request. |
| 2949 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2950 | rbd_assert(obj_request); |
| 2951 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2952 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2953 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2954 | rbd_obj_request_submit(obj_request); |
| 2955 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2956 | } |
| 2957 | |
| 2958 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2959 | if (obj_request->result) |
| 2960 | goto out; |
| 2961 | |
| 2962 | /* |
| 2963 | * We need to zero anything beyond the parent overlap |
| 2964 | * boundary. Since rbd_img_obj_request_read_callback() |
| 2965 | * will zero anything beyond the end of a short read, an |
| 2966 | * easy way to do this is to pretend the data from the |
| 2967 | * parent came up short--ending at the overlap boundary. |
| 2968 | */ |
| 2969 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 2970 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2971 | if (obj_end > rbd_dev->parent_overlap) { |
| 2972 | u64 xferred = 0; |
| 2973 | |
| 2974 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 2975 | xferred = rbd_dev->parent_overlap - |
| 2976 | obj_request->img_offset; |
| 2977 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2978 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2979 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2980 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2981 | } |
| 2982 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2983 | rbd_img_obj_request_read_callback(obj_request); |
| 2984 | rbd_obj_request_complete(obj_request); |
| 2985 | } |
| 2986 | |
| 2987 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 2988 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2989 | struct rbd_img_request *img_request; |
| 2990 | int result; |
| 2991 | |
| 2992 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2993 | rbd_assert(obj_request->img_request != NULL); |
| 2994 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2995 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2996 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2997 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2998 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2999 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3000 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3001 | result = -ENOMEM; |
| 3002 | if (!img_request) |
| 3003 | goto out_err; |
| 3004 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3005 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3006 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3007 | obj_request->bio_list); |
| 3008 | else |
| 3009 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3010 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3011 | if (result) |
| 3012 | goto out_err; |
| 3013 | |
| 3014 | img_request->callback = rbd_img_parent_read_callback; |
| 3015 | result = rbd_img_request_submit(img_request); |
| 3016 | if (result) |
| 3017 | goto out_err; |
| 3018 | |
| 3019 | return; |
| 3020 | out_err: |
| 3021 | if (img_request) |
| 3022 | rbd_img_request_put(img_request); |
| 3023 | obj_request->result = result; |
| 3024 | obj_request->xferred = 0; |
| 3025 | obj_request_done_set(obj_request); |
| 3026 | } |
| 3027 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3028 | static const struct rbd_client_id rbd_empty_cid; |
| 3029 | |
| 3030 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3031 | const struct rbd_client_id *rhs) |
| 3032 | { |
| 3033 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3034 | } |
| 3035 | |
| 3036 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3037 | { |
| 3038 | struct rbd_client_id cid; |
| 3039 | |
| 3040 | mutex_lock(&rbd_dev->watch_mutex); |
| 3041 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3042 | cid.handle = rbd_dev->watch_cookie; |
| 3043 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3044 | return cid; |
| 3045 | } |
| 3046 | |
| 3047 | /* |
| 3048 | * lock_rwsem must be held for write |
| 3049 | */ |
| 3050 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3051 | const struct rbd_client_id *cid) |
| 3052 | { |
| 3053 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3054 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3055 | cid->gid, cid->handle); |
| 3056 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3057 | } |
| 3058 | |
| 3059 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3060 | { |
| 3061 | mutex_lock(&rbd_dev->watch_mutex); |
| 3062 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3063 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3064 | } |
| 3065 | |
| 3066 | /* |
| 3067 | * lock_rwsem must be held for write |
| 3068 | */ |
| 3069 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3070 | { |
| 3071 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3072 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3073 | char cookie[32]; |
| 3074 | int ret; |
| 3075 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3076 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 3077 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3078 | |
| 3079 | format_lock_cookie(rbd_dev, cookie); |
| 3080 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3081 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3082 | RBD_LOCK_TAG, "", 0); |
| 3083 | if (ret) |
| 3084 | return ret; |
| 3085 | |
| 3086 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3087 | strcpy(rbd_dev->lock_cookie, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3088 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3089 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3090 | return 0; |
| 3091 | } |
| 3092 | |
| 3093 | /* |
| 3094 | * lock_rwsem must be held for write |
| 3095 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3096 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3097 | { |
| 3098 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3099 | int ret; |
| 3100 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3101 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 3102 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3103 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3104 | 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] | 3105 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3106 | if (ret && ret != -ENOENT) |
| 3107 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3108 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3109 | /* treat errors as the image is unlocked */ |
| 3110 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3111 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3112 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3113 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3117 | enum rbd_notify_op notify_op, |
| 3118 | struct page ***preply_pages, |
| 3119 | size_t *preply_len) |
| 3120 | { |
| 3121 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3122 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3123 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3124 | char buf[buf_size]; |
| 3125 | void *p = buf; |
| 3126 | |
| 3127 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3128 | |
| 3129 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3130 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3131 | ceph_encode_32(&p, notify_op); |
| 3132 | ceph_encode_64(&p, cid.gid); |
| 3133 | ceph_encode_64(&p, cid.handle); |
| 3134 | |
| 3135 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3136 | &rbd_dev->header_oloc, buf, buf_size, |
| 3137 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3138 | } |
| 3139 | |
| 3140 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3141 | enum rbd_notify_op notify_op) |
| 3142 | { |
| 3143 | struct page **reply_pages; |
| 3144 | size_t reply_len; |
| 3145 | |
| 3146 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3147 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3148 | } |
| 3149 | |
| 3150 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3151 | { |
| 3152 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3153 | acquired_lock_work); |
| 3154 | |
| 3155 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3156 | } |
| 3157 | |
| 3158 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3159 | { |
| 3160 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3161 | released_lock_work); |
| 3162 | |
| 3163 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3164 | } |
| 3165 | |
| 3166 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3167 | { |
| 3168 | struct page **reply_pages; |
| 3169 | size_t reply_len; |
| 3170 | bool lock_owner_responded = false; |
| 3171 | int ret; |
| 3172 | |
| 3173 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3174 | |
| 3175 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3176 | &reply_pages, &reply_len); |
| 3177 | if (ret && ret != -ETIMEDOUT) { |
| 3178 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3179 | goto out; |
| 3180 | } |
| 3181 | |
| 3182 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3183 | void *p = page_address(reply_pages[0]); |
| 3184 | void *const end = p + reply_len; |
| 3185 | u32 n; |
| 3186 | |
| 3187 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3188 | while (n--) { |
| 3189 | u8 struct_v; |
| 3190 | u32 len; |
| 3191 | |
| 3192 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3193 | p += 8 + 8; /* skip gid and cookie */ |
| 3194 | |
| 3195 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3196 | if (!len) |
| 3197 | continue; |
| 3198 | |
| 3199 | if (lock_owner_responded) { |
| 3200 | rbd_warn(rbd_dev, |
| 3201 | "duplicate lock owners detected"); |
| 3202 | ret = -EIO; |
| 3203 | goto out; |
| 3204 | } |
| 3205 | |
| 3206 | lock_owner_responded = true; |
| 3207 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3208 | &struct_v, &len); |
| 3209 | if (ret) { |
| 3210 | rbd_warn(rbd_dev, |
| 3211 | "failed to decode ResponseMessage: %d", |
| 3212 | ret); |
| 3213 | goto e_inval; |
| 3214 | } |
| 3215 | |
| 3216 | ret = ceph_decode_32(&p); |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | if (!lock_owner_responded) { |
| 3221 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3222 | ret = -ETIMEDOUT; |
| 3223 | } |
| 3224 | |
| 3225 | out: |
| 3226 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3227 | return ret; |
| 3228 | |
| 3229 | e_inval: |
| 3230 | ret = -EINVAL; |
| 3231 | goto out; |
| 3232 | } |
| 3233 | |
| 3234 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3235 | { |
| 3236 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3237 | |
| 3238 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3239 | if (wake_all) |
| 3240 | wake_up_all(&rbd_dev->lock_waitq); |
| 3241 | else |
| 3242 | wake_up(&rbd_dev->lock_waitq); |
| 3243 | } |
| 3244 | |
| 3245 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3246 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3247 | { |
| 3248 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3249 | u8 lock_type; |
| 3250 | char *lock_tag; |
| 3251 | int ret; |
| 3252 | |
| 3253 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3254 | |
| 3255 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3256 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3257 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3258 | if (ret) |
| 3259 | return ret; |
| 3260 | |
| 3261 | if (*num_lockers == 0) { |
| 3262 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3263 | goto out; |
| 3264 | } |
| 3265 | |
| 3266 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3267 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3268 | lock_tag); |
| 3269 | ret = -EBUSY; |
| 3270 | goto out; |
| 3271 | } |
| 3272 | |
| 3273 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3274 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3275 | ret = -EBUSY; |
| 3276 | goto out; |
| 3277 | } |
| 3278 | |
| 3279 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3280 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3281 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3282 | (*lockers)[0].id.cookie); |
| 3283 | ret = -EBUSY; |
| 3284 | goto out; |
| 3285 | } |
| 3286 | |
| 3287 | out: |
| 3288 | kfree(lock_tag); |
| 3289 | return ret; |
| 3290 | } |
| 3291 | |
| 3292 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3293 | const struct ceph_locker *locker) |
| 3294 | { |
| 3295 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3296 | struct ceph_watch_item *watchers; |
| 3297 | u32 num_watchers; |
| 3298 | u64 cookie; |
| 3299 | int i; |
| 3300 | int ret; |
| 3301 | |
| 3302 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3303 | &rbd_dev->header_oloc, &watchers, |
| 3304 | &num_watchers); |
| 3305 | if (ret) |
| 3306 | return ret; |
| 3307 | |
| 3308 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3309 | for (i = 0; i < num_watchers; i++) { |
| 3310 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3311 | sizeof(locker->info.addr)) && |
| 3312 | watchers[i].cookie == cookie) { |
| 3313 | struct rbd_client_id cid = { |
| 3314 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3315 | .handle = cookie, |
| 3316 | }; |
| 3317 | |
| 3318 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3319 | rbd_dev, cid.gid, cid.handle); |
| 3320 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3321 | ret = 1; |
| 3322 | goto out; |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3327 | ret = 0; |
| 3328 | out: |
| 3329 | kfree(watchers); |
| 3330 | return ret; |
| 3331 | } |
| 3332 | |
| 3333 | /* |
| 3334 | * lock_rwsem must be held for write |
| 3335 | */ |
| 3336 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3337 | { |
| 3338 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3339 | struct ceph_locker *lockers; |
| 3340 | u32 num_lockers; |
| 3341 | int ret; |
| 3342 | |
| 3343 | for (;;) { |
| 3344 | ret = rbd_lock(rbd_dev); |
| 3345 | if (ret != -EBUSY) |
| 3346 | return ret; |
| 3347 | |
| 3348 | /* determine if the current lock holder is still alive */ |
| 3349 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3350 | if (ret) |
| 3351 | return ret; |
| 3352 | |
| 3353 | if (num_lockers == 0) |
| 3354 | goto again; |
| 3355 | |
| 3356 | ret = find_watcher(rbd_dev, lockers); |
| 3357 | if (ret) { |
| 3358 | if (ret > 0) |
| 3359 | ret = 0; /* have to request lock */ |
| 3360 | goto out; |
| 3361 | } |
| 3362 | |
| 3363 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3364 | ENTITY_NAME(lockers[0].id.name)); |
| 3365 | |
| 3366 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3367 | &lockers[0].info.addr); |
| 3368 | if (ret) { |
| 3369 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3370 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3371 | goto out; |
| 3372 | } |
| 3373 | |
| 3374 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3375 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3376 | lockers[0].id.cookie, |
| 3377 | &lockers[0].id.name); |
| 3378 | if (ret && ret != -ENOENT) |
| 3379 | goto out; |
| 3380 | |
| 3381 | again: |
| 3382 | ceph_free_lockers(lockers, num_lockers); |
| 3383 | } |
| 3384 | |
| 3385 | out: |
| 3386 | ceph_free_lockers(lockers, num_lockers); |
| 3387 | return ret; |
| 3388 | } |
| 3389 | |
| 3390 | /* |
| 3391 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3392 | */ |
| 3393 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3394 | int *pret) |
| 3395 | { |
| 3396 | enum rbd_lock_state lock_state; |
| 3397 | |
| 3398 | down_read(&rbd_dev->lock_rwsem); |
| 3399 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3400 | rbd_dev->lock_state); |
| 3401 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3402 | lock_state = rbd_dev->lock_state; |
| 3403 | up_read(&rbd_dev->lock_rwsem); |
| 3404 | return lock_state; |
| 3405 | } |
| 3406 | |
| 3407 | up_read(&rbd_dev->lock_rwsem); |
| 3408 | down_write(&rbd_dev->lock_rwsem); |
| 3409 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3410 | rbd_dev->lock_state); |
| 3411 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3412 | *pret = rbd_try_lock(rbd_dev); |
| 3413 | if (*pret) |
| 3414 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3415 | } |
| 3416 | |
| 3417 | lock_state = rbd_dev->lock_state; |
| 3418 | up_write(&rbd_dev->lock_rwsem); |
| 3419 | return lock_state; |
| 3420 | } |
| 3421 | |
| 3422 | static void rbd_acquire_lock(struct work_struct *work) |
| 3423 | { |
| 3424 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3425 | struct rbd_device, lock_dwork); |
| 3426 | enum rbd_lock_state lock_state; |
| 3427 | int ret; |
| 3428 | |
| 3429 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3430 | again: |
| 3431 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3432 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3433 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3434 | wake_requests(rbd_dev, true); |
| 3435 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3436 | rbd_dev, lock_state, ret); |
| 3437 | return; |
| 3438 | } |
| 3439 | |
| 3440 | ret = rbd_request_lock(rbd_dev); |
| 3441 | if (ret == -ETIMEDOUT) { |
| 3442 | goto again; /* treat this as a dead client */ |
| 3443 | } else if (ret < 0) { |
| 3444 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3445 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3446 | RBD_RETRY_DELAY); |
| 3447 | } else { |
| 3448 | /* |
| 3449 | * lock owner acked, but resend if we don't see them |
| 3450 | * release the lock |
| 3451 | */ |
| 3452 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3453 | rbd_dev); |
| 3454 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3455 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3456 | } |
| 3457 | } |
| 3458 | |
| 3459 | /* |
| 3460 | * lock_rwsem must be held for write |
| 3461 | */ |
| 3462 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3463 | { |
| 3464 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3465 | rbd_dev->lock_state); |
| 3466 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3467 | return false; |
| 3468 | |
| 3469 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3470 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3471 | /* |
| 3472 | * Ensure that all in-flight IO is flushed. |
| 3473 | * |
| 3474 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3475 | * may be shared with other devices. |
| 3476 | */ |
| 3477 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3478 | up_read(&rbd_dev->lock_rwsem); |
| 3479 | |
| 3480 | down_write(&rbd_dev->lock_rwsem); |
| 3481 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3482 | rbd_dev->lock_state); |
| 3483 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3484 | return false; |
| 3485 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3486 | rbd_unlock(rbd_dev); |
| 3487 | /* |
| 3488 | * Give others a chance to grab the lock - we would re-acquire |
| 3489 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3490 | * otherwise. We need to ack our own notifications, so this |
| 3491 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3492 | * after wake_requests() in rbd_handle_released_lock(). |
| 3493 | */ |
| 3494 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3495 | return true; |
| 3496 | } |
| 3497 | |
| 3498 | static void rbd_release_lock_work(struct work_struct *work) |
| 3499 | { |
| 3500 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3501 | unlock_work); |
| 3502 | |
| 3503 | down_write(&rbd_dev->lock_rwsem); |
| 3504 | rbd_release_lock(rbd_dev); |
| 3505 | up_write(&rbd_dev->lock_rwsem); |
| 3506 | } |
| 3507 | |
| 3508 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3509 | void **p) |
| 3510 | { |
| 3511 | struct rbd_client_id cid = { 0 }; |
| 3512 | |
| 3513 | if (struct_v >= 2) { |
| 3514 | cid.gid = ceph_decode_64(p); |
| 3515 | cid.handle = ceph_decode_64(p); |
| 3516 | } |
| 3517 | |
| 3518 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3519 | cid.handle); |
| 3520 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3521 | down_write(&rbd_dev->lock_rwsem); |
| 3522 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3523 | /* |
| 3524 | * we already know that the remote client is |
| 3525 | * the owner |
| 3526 | */ |
| 3527 | up_write(&rbd_dev->lock_rwsem); |
| 3528 | return; |
| 3529 | } |
| 3530 | |
| 3531 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3532 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3533 | } else { |
| 3534 | down_read(&rbd_dev->lock_rwsem); |
| 3535 | } |
| 3536 | |
| 3537 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3538 | wake_requests(rbd_dev, false); |
| 3539 | up_read(&rbd_dev->lock_rwsem); |
| 3540 | } |
| 3541 | |
| 3542 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3543 | void **p) |
| 3544 | { |
| 3545 | struct rbd_client_id cid = { 0 }; |
| 3546 | |
| 3547 | if (struct_v >= 2) { |
| 3548 | cid.gid = ceph_decode_64(p); |
| 3549 | cid.handle = ceph_decode_64(p); |
| 3550 | } |
| 3551 | |
| 3552 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3553 | cid.handle); |
| 3554 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3555 | down_write(&rbd_dev->lock_rwsem); |
| 3556 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3557 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3558 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3559 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3560 | up_write(&rbd_dev->lock_rwsem); |
| 3561 | return; |
| 3562 | } |
| 3563 | |
| 3564 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3565 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3566 | } else { |
| 3567 | down_read(&rbd_dev->lock_rwsem); |
| 3568 | } |
| 3569 | |
| 3570 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3571 | wake_requests(rbd_dev, false); |
| 3572 | up_read(&rbd_dev->lock_rwsem); |
| 3573 | } |
| 3574 | |
| 3575 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3576 | void **p) |
| 3577 | { |
| 3578 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3579 | struct rbd_client_id cid = { 0 }; |
| 3580 | bool need_to_send; |
| 3581 | |
| 3582 | if (struct_v >= 2) { |
| 3583 | cid.gid = ceph_decode_64(p); |
| 3584 | cid.handle = ceph_decode_64(p); |
| 3585 | } |
| 3586 | |
| 3587 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3588 | cid.handle); |
| 3589 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3590 | return false; |
| 3591 | |
| 3592 | down_read(&rbd_dev->lock_rwsem); |
| 3593 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3594 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3595 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3596 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3597 | rbd_dev); |
| 3598 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3599 | } |
| 3600 | } |
| 3601 | up_read(&rbd_dev->lock_rwsem); |
| 3602 | return need_to_send; |
| 3603 | } |
| 3604 | |
| 3605 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3606 | u64 notify_id, u64 cookie, s32 *result) |
| 3607 | { |
| 3608 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3609 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3610 | char buf[buf_size]; |
| 3611 | int ret; |
| 3612 | |
| 3613 | if (result) { |
| 3614 | void *p = buf; |
| 3615 | |
| 3616 | /* encode ResponseMessage */ |
| 3617 | ceph_start_encoding(&p, 1, 1, |
| 3618 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3619 | ceph_encode_32(&p, *result); |
| 3620 | } else { |
| 3621 | buf_size = 0; |
| 3622 | } |
| 3623 | |
| 3624 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3625 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3626 | buf, buf_size); |
| 3627 | if (ret) |
| 3628 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3629 | } |
| 3630 | |
| 3631 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3632 | u64 cookie) |
| 3633 | { |
| 3634 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3635 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3636 | } |
| 3637 | |
| 3638 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3639 | u64 notify_id, u64 cookie, s32 result) |
| 3640 | { |
| 3641 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3642 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3643 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3644 | |
| 3645 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3646 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3647 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3648 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3649 | void *p = data; |
| 3650 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3651 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3652 | u32 len; |
| 3653 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3654 | int ret; |
| 3655 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3656 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3657 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3658 | if (data_len) { |
| 3659 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3660 | &struct_v, &len); |
| 3661 | if (ret) { |
| 3662 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3663 | ret); |
| 3664 | return; |
| 3665 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3666 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3667 | notify_op = ceph_decode_32(&p); |
| 3668 | } else { |
| 3669 | /* legacy notification for header updates */ |
| 3670 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3671 | len = 0; |
| 3672 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3673 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3674 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3675 | switch (notify_op) { |
| 3676 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3677 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3678 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3679 | break; |
| 3680 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3681 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3682 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3683 | break; |
| 3684 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3685 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3686 | /* |
| 3687 | * send ResponseMessage(0) back so the client |
| 3688 | * can detect a missing owner |
| 3689 | */ |
| 3690 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3691 | cookie, 0); |
| 3692 | else |
| 3693 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3694 | break; |
| 3695 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3696 | ret = rbd_dev_refresh(rbd_dev); |
| 3697 | if (ret) |
| 3698 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3699 | |
| 3700 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3701 | break; |
| 3702 | default: |
| 3703 | if (rbd_is_lock_owner(rbd_dev)) |
| 3704 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3705 | cookie, -EOPNOTSUPP); |
| 3706 | else |
| 3707 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3708 | break; |
| 3709 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3710 | } |
| 3711 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3712 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3713 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3714 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3715 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3716 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3717 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3718 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3719 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3720 | down_write(&rbd_dev->lock_rwsem); |
| 3721 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3722 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3723 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3724 | mutex_lock(&rbd_dev->watch_mutex); |
| 3725 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3726 | __rbd_unregister_watch(rbd_dev); |
| 3727 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3728 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3729 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3730 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3731 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3735 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3736 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3737 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3738 | { |
| 3739 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3740 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3741 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3742 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3743 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3744 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3745 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3746 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3747 | rbd_watch_errcb, rbd_dev); |
| 3748 | if (IS_ERR(handle)) |
| 3749 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3750 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3751 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3752 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3753 | } |
| 3754 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3755 | /* |
| 3756 | * watch_mutex must be locked |
| 3757 | */ |
| 3758 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3759 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3760 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3761 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3762 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3763 | rbd_assert(rbd_dev->watch_handle); |
| 3764 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3765 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3766 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3767 | if (ret) |
| 3768 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3769 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3770 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3771 | } |
| 3772 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3773 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3774 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3775 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3776 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3777 | mutex_lock(&rbd_dev->watch_mutex); |
| 3778 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3779 | ret = __rbd_register_watch(rbd_dev); |
| 3780 | if (ret) |
| 3781 | goto out; |
| 3782 | |
| 3783 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3784 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3785 | |
| 3786 | out: |
| 3787 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3788 | return ret; |
| 3789 | } |
| 3790 | |
| 3791 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3792 | { |
| 3793 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3794 | |
| 3795 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3796 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3797 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3798 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3799 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3803 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3804 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3805 | cancel_tasks_sync(rbd_dev); |
| 3806 | |
| 3807 | mutex_lock(&rbd_dev->watch_mutex); |
| 3808 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3809 | __rbd_unregister_watch(rbd_dev); |
| 3810 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3811 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3812 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3813 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3814 | } |
| 3815 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3816 | /* |
| 3817 | * lock_rwsem must be held for write |
| 3818 | */ |
| 3819 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3820 | { |
| 3821 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3822 | char cookie[32]; |
| 3823 | int ret; |
| 3824 | |
| 3825 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3826 | |
| 3827 | format_lock_cookie(rbd_dev, cookie); |
| 3828 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3829 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3830 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3831 | RBD_LOCK_TAG, cookie); |
| 3832 | if (ret) { |
| 3833 | if (ret != -EOPNOTSUPP) |
| 3834 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3835 | ret); |
| 3836 | |
| 3837 | /* |
| 3838 | * Lock cookie cannot be updated on older OSDs, so do |
| 3839 | * a manual release and queue an acquire. |
| 3840 | */ |
| 3841 | if (rbd_release_lock(rbd_dev)) |
| 3842 | queue_delayed_work(rbd_dev->task_wq, |
| 3843 | &rbd_dev->lock_dwork, 0); |
| 3844 | } else { |
| 3845 | strcpy(rbd_dev->lock_cookie, cookie); |
| 3846 | } |
| 3847 | } |
| 3848 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3849 | static void rbd_reregister_watch(struct work_struct *work) |
| 3850 | { |
| 3851 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3852 | struct rbd_device, watch_dwork); |
| 3853 | int ret; |
| 3854 | |
| 3855 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3856 | |
| 3857 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3858 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3859 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3860 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3861 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3862 | |
| 3863 | ret = __rbd_register_watch(rbd_dev); |
| 3864 | if (ret) { |
| 3865 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3866 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3867 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3868 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3869 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3870 | queue_delayed_work(rbd_dev->task_wq, |
| 3871 | &rbd_dev->watch_dwork, |
| 3872 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3873 | } |
| 3874 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3875 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3879 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3880 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3881 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3882 | down_write(&rbd_dev->lock_rwsem); |
| 3883 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3884 | rbd_reacquire_lock(rbd_dev); |
| 3885 | up_write(&rbd_dev->lock_rwsem); |
| 3886 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3887 | ret = rbd_dev_refresh(rbd_dev); |
| 3888 | if (ret) |
| 3889 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3892 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3893 | * Synchronous osd object method call. Returns the number of bytes |
| 3894 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3895 | */ |
| 3896 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3897 | struct ceph_object_id *oid, |
| 3898 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3899 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3900 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3901 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3902 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3903 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3904 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3905 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3906 | struct page *req_page = NULL; |
| 3907 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3908 | int ret; |
| 3909 | |
| 3910 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3911 | * Method calls are ultimately read operations. The result |
| 3912 | * should placed into the inbound buffer provided. They |
| 3913 | * also supply outbound data--parameters for the object |
| 3914 | * method. Currently if this is present it will be a |
| 3915 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3916 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3917 | if (outbound) { |
| 3918 | if (outbound_size > PAGE_SIZE) |
| 3919 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3920 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3921 | req_page = alloc_page(GFP_KERNEL); |
| 3922 | if (!req_page) |
| 3923 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3924 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3925 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3926 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3927 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3928 | reply_page = alloc_page(GFP_KERNEL); |
| 3929 | if (!reply_page) { |
| 3930 | if (req_page) |
| 3931 | __free_page(req_page); |
| 3932 | return -ENOMEM; |
| 3933 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3934 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3935 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3936 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3937 | reply_page, &inbound_size); |
| 3938 | if (!ret) { |
| 3939 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3940 | ret = inbound_size; |
| 3941 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3942 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3943 | if (req_page) |
| 3944 | __free_page(req_page); |
| 3945 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3946 | return ret; |
| 3947 | } |
| 3948 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3949 | /* |
| 3950 | * lock_rwsem must be held for read |
| 3951 | */ |
| 3952 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3953 | { |
| 3954 | DEFINE_WAIT(wait); |
| 3955 | |
| 3956 | do { |
| 3957 | /* |
| 3958 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3959 | * and cancel_delayed_work() in wake_requests(). |
| 3960 | */ |
| 3961 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3962 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3963 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3964 | TASK_UNINTERRUPTIBLE); |
| 3965 | up_read(&rbd_dev->lock_rwsem); |
| 3966 | schedule(); |
| 3967 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3968 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3969 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3970 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3971 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3972 | } |
| 3973 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3974 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3975 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3976 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3977 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3978 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3979 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3980 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3981 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3982 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3983 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3984 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3985 | int result; |
| 3986 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3987 | switch (req_op(rq)) { |
| 3988 | case REQ_OP_DISCARD: |
| 3989 | op_type = OBJ_OP_DISCARD; |
| 3990 | break; |
| 3991 | case REQ_OP_WRITE: |
| 3992 | op_type = OBJ_OP_WRITE; |
| 3993 | break; |
| 3994 | case REQ_OP_READ: |
| 3995 | op_type = OBJ_OP_READ; |
| 3996 | break; |
| 3997 | default: |
| 3998 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3999 | result = -EIO; |
| 4000 | goto err; |
| 4001 | } |
| 4002 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4003 | /* Ignore/skip any zero-length requests */ |
| 4004 | |
| 4005 | if (!length) { |
| 4006 | dout("%s: zero-length request\n", __func__); |
| 4007 | result = 0; |
| 4008 | goto err_rq; |
| 4009 | } |
| 4010 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4011 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4012 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4013 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4014 | if (rbd_dev->mapping.read_only) { |
| 4015 | result = -EROFS; |
| 4016 | goto err_rq; |
| 4017 | } |
| 4018 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4019 | } |
| 4020 | |
| 4021 | /* |
| 4022 | * Quit early if the mapped snapshot no longer exists. It's |
| 4023 | * still possible the snapshot will have disappeared by the |
| 4024 | * time our request arrives at the osd, but there's no sense in |
| 4025 | * sending it if we already know. |
| 4026 | */ |
| 4027 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4028 | dout("request for non-existent snapshot"); |
| 4029 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4030 | result = -ENXIO; |
| 4031 | goto err_rq; |
| 4032 | } |
| 4033 | |
| 4034 | if (offset && length > U64_MAX - offset + 1) { |
| 4035 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4036 | length); |
| 4037 | result = -EINVAL; |
| 4038 | goto err_rq; /* Shouldn't happen */ |
| 4039 | } |
| 4040 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4041 | blk_mq_start_request(rq); |
| 4042 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4043 | down_read(&rbd_dev->header_rwsem); |
| 4044 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4045 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4046 | snapc = rbd_dev->header.snapc; |
| 4047 | ceph_get_snap_context(snapc); |
| 4048 | } |
| 4049 | up_read(&rbd_dev->header_rwsem); |
| 4050 | |
| 4051 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4052 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4053 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4054 | result = -EIO; |
| 4055 | goto err_rq; |
| 4056 | } |
| 4057 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame^] | 4058 | must_be_locked = |
| 4059 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 4060 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4061 | if (must_be_locked) { |
| 4062 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4063 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4064 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4065 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4066 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4067 | result = -EBLACKLISTED; |
| 4068 | goto err_unlock; |
| 4069 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4070 | } |
| 4071 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4072 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4073 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4074 | if (!img_request) { |
| 4075 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4076 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4077 | } |
| 4078 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4079 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4080 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4081 | if (op_type == OBJ_OP_DISCARD) |
| 4082 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4083 | NULL); |
| 4084 | else |
| 4085 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4086 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4087 | if (result) |
| 4088 | goto err_img_request; |
| 4089 | |
| 4090 | result = rbd_img_request_submit(img_request); |
| 4091 | if (result) |
| 4092 | goto err_img_request; |
| 4093 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4094 | if (must_be_locked) |
| 4095 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4096 | return; |
| 4097 | |
| 4098 | err_img_request: |
| 4099 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4100 | err_unlock: |
| 4101 | if (must_be_locked) |
| 4102 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4103 | err_rq: |
| 4104 | if (result) |
| 4105 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4106 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4107 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4108 | err: |
| 4109 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4110 | } |
| 4111 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4112 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4113 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4114 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4115 | struct request *rq = bd->rq; |
| 4116 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4117 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4118 | queue_work(rbd_wq, work); |
| 4119 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4120 | } |
| 4121 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4122 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4123 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4124 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 4125 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 4126 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4127 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4130 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4131 | struct ceph_object_id *oid, |
| 4132 | struct ceph_object_locator *oloc, |
| 4133 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4134 | |
| 4135 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4136 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4137 | struct ceph_osd_request *req; |
| 4138 | struct page **pages; |
| 4139 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4140 | int ret; |
| 4141 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4142 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4143 | if (!req) |
| 4144 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4145 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4146 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4147 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4148 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4149 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4150 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4151 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4152 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4153 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4154 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4155 | if (IS_ERR(pages)) { |
| 4156 | ret = PTR_ERR(pages); |
| 4157 | goto out_req; |
| 4158 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4159 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4160 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4161 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4162 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4163 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4164 | ceph_osdc_start_request(osdc, req, false); |
| 4165 | ret = ceph_osdc_wait_request(osdc, req); |
| 4166 | if (ret >= 0) |
| 4167 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4168 | |
| 4169 | out_req: |
| 4170 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4171 | return ret; |
| 4172 | } |
| 4173 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4174 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4175 | * Read the complete header for the given rbd device. On successful |
| 4176 | * return, the rbd_dev->header field will contain up-to-date |
| 4177 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4178 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4179 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4180 | { |
| 4181 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4182 | u32 snap_count = 0; |
| 4183 | u64 names_size = 0; |
| 4184 | u32 want_count; |
| 4185 | int ret; |
| 4186 | |
| 4187 | /* |
| 4188 | * The complete header will include an array of its 64-bit |
| 4189 | * snapshot ids, followed by the names of those snapshots as |
| 4190 | * a contiguous block of NUL-terminated strings. Note that |
| 4191 | * the number of snapshots could change by the time we read |
| 4192 | * it in, in which case we re-read it. |
| 4193 | */ |
| 4194 | do { |
| 4195 | size_t size; |
| 4196 | |
| 4197 | kfree(ondisk); |
| 4198 | |
| 4199 | size = sizeof (*ondisk); |
| 4200 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4201 | size += names_size; |
| 4202 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4203 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4204 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4205 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4206 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4207 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4208 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4209 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4210 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4211 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4212 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4213 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4214 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4215 | } |
| 4216 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4217 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4218 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4219 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4223 | want_count = snap_count; |
| 4224 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4225 | } while (snap_count != want_count); |
| 4226 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4227 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4228 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4229 | kfree(ondisk); |
| 4230 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4231 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4234 | /* |
| 4235 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4236 | * has disappeared from the (just updated) snapshot context. |
| 4237 | */ |
| 4238 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4239 | { |
| 4240 | u64 snap_id; |
| 4241 | |
| 4242 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4243 | return; |
| 4244 | |
| 4245 | snap_id = rbd_dev->spec->snap_id; |
| 4246 | if (snap_id == CEPH_NOSNAP) |
| 4247 | return; |
| 4248 | |
| 4249 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4250 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4251 | } |
| 4252 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4253 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4254 | { |
| 4255 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4256 | |
| 4257 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4258 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4259 | * try to update its size. If REMOVING is set, updating size |
| 4260 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4261 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4262 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4263 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4264 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4265 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4266 | set_capacity(rbd_dev->disk, size); |
| 4267 | revalidate_disk(rbd_dev->disk); |
| 4268 | } |
| 4269 | } |
| 4270 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4271 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4272 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4273 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4274 | int ret; |
| 4275 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4276 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4277 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4278 | |
| 4279 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4280 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4281 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4282 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4283 | /* |
| 4284 | * If there is a parent, see if it has disappeared due to the |
| 4285 | * mapped image getting flattened. |
| 4286 | */ |
| 4287 | if (rbd_dev->parent) { |
| 4288 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4289 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4290 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4291 | } |
| 4292 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4293 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4294 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4295 | } else { |
| 4296 | /* validate mapped snapshot's EXISTS flag */ |
| 4297 | rbd_exists_validate(rbd_dev); |
| 4298 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4299 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4300 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4301 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4302 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4303 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4304 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4305 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4306 | } |
| 4307 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4308 | static int rbd_init_request(void *data, struct request *rq, |
| 4309 | unsigned int hctx_idx, unsigned int request_idx, |
| 4310 | unsigned int numa_node) |
| 4311 | { |
| 4312 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4313 | |
| 4314 | INIT_WORK(work, rbd_queue_workfn); |
| 4315 | return 0; |
| 4316 | } |
| 4317 | |
| 4318 | static struct blk_mq_ops rbd_mq_ops = { |
| 4319 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4320 | .init_request = rbd_init_request, |
| 4321 | }; |
| 4322 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4323 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4324 | { |
| 4325 | struct gendisk *disk; |
| 4326 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4327 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4328 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4329 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4330 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4331 | disk = alloc_disk(single_major ? |
| 4332 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4333 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4334 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4335 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4336 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4337 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4338 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4339 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4340 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4341 | if (single_major) |
| 4342 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4343 | disk->fops = &rbd_bd_ops; |
| 4344 | disk->private_data = rbd_dev; |
| 4345 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4346 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4347 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4348 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4349 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4350 | 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] | 4351 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4352 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4353 | |
| 4354 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4355 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4356 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4357 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4358 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4359 | if (IS_ERR(q)) { |
| 4360 | err = PTR_ERR(q); |
| 4361 | goto out_tag_set; |
| 4362 | } |
| 4363 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4364 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4365 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4366 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4367 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4368 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4369 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4370 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4371 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4372 | blk_queue_max_segment_size(q, segment_size); |
| 4373 | blk_queue_io_min(q, segment_size); |
| 4374 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4375 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4376 | /* enable the discard support */ |
| 4377 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4378 | q->limits.discard_granularity = segment_size; |
| 4379 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4380 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4381 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4382 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4383 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4384 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4385 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4386 | /* |
| 4387 | * disk_release() expects a queue ref from add_disk() and will |
| 4388 | * put it. Hold an extra ref until add_disk() is called. |
| 4389 | */ |
| 4390 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4391 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4392 | q->queuedata = rbd_dev; |
| 4393 | |
| 4394 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4395 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4396 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4397 | out_tag_set: |
| 4398 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4399 | out_disk: |
| 4400 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4401 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4402 | } |
| 4403 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4404 | /* |
| 4405 | sysfs |
| 4406 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4407 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4408 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4409 | { |
| 4410 | return container_of(dev, struct rbd_device, dev); |
| 4411 | } |
| 4412 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4413 | static ssize_t rbd_size_show(struct device *dev, |
| 4414 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4415 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4416 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4417 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4418 | return sprintf(buf, "%llu\n", |
| 4419 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4422 | /* |
| 4423 | * Note this shows the features for whatever's mapped, which is not |
| 4424 | * necessarily the base image. |
| 4425 | */ |
| 4426 | static ssize_t rbd_features_show(struct device *dev, |
| 4427 | struct device_attribute *attr, char *buf) |
| 4428 | { |
| 4429 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4430 | |
| 4431 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4432 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4433 | } |
| 4434 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4435 | static ssize_t rbd_major_show(struct device *dev, |
| 4436 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4437 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4438 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4439 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4440 | if (rbd_dev->major) |
| 4441 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4442 | |
| 4443 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4444 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4445 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4446 | static ssize_t rbd_minor_show(struct device *dev, |
| 4447 | struct device_attribute *attr, char *buf) |
| 4448 | { |
| 4449 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4450 | |
| 4451 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4452 | } |
| 4453 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4454 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4455 | struct device_attribute *attr, char *buf) |
| 4456 | { |
| 4457 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4458 | struct ceph_entity_addr *client_addr = |
| 4459 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4460 | |
| 4461 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4462 | le32_to_cpu(client_addr->nonce)); |
| 4463 | } |
| 4464 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4465 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4466 | struct device_attribute *attr, char *buf) |
| 4467 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4468 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4469 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4470 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4471 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4472 | } |
| 4473 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4474 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4475 | struct device_attribute *attr, char *buf) |
| 4476 | { |
| 4477 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4478 | |
| 4479 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4480 | } |
| 4481 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4482 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4483 | struct device_attribute *attr, char *buf) |
| 4484 | { |
| 4485 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4486 | |
| 4487 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4488 | } |
| 4489 | |
| 4490 | static ssize_t rbd_pool_show(struct device *dev, |
| 4491 | struct device_attribute *attr, char *buf) |
| 4492 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4493 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4494 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4495 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4496 | } |
| 4497 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4498 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4499 | struct device_attribute *attr, char *buf) |
| 4500 | { |
| 4501 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4502 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4503 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4504 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4505 | } |
| 4506 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4507 | static ssize_t rbd_name_show(struct device *dev, |
| 4508 | struct device_attribute *attr, char *buf) |
| 4509 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4510 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4511 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4512 | if (rbd_dev->spec->image_name) |
| 4513 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4514 | |
| 4515 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4516 | } |
| 4517 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4518 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4519 | struct device_attribute *attr, char *buf) |
| 4520 | { |
| 4521 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4522 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4523 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4524 | } |
| 4525 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4526 | /* |
| 4527 | * Shows the name of the currently-mapped snapshot (or |
| 4528 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4529 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4530 | static ssize_t rbd_snap_show(struct device *dev, |
| 4531 | struct device_attribute *attr, |
| 4532 | char *buf) |
| 4533 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4534 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4535 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4536 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4537 | } |
| 4538 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4539 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4540 | struct device_attribute *attr, char *buf) |
| 4541 | { |
| 4542 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4543 | |
| 4544 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4545 | } |
| 4546 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4547 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4548 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4549 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4550 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4551 | */ |
| 4552 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4553 | struct device_attribute *attr, |
| 4554 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4555 | { |
| 4556 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4557 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4558 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4559 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4560 | return sprintf(buf, "(no parent image)\n"); |
| 4561 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4562 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4563 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4564 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4565 | count += sprintf(&buf[count], "%s" |
| 4566 | "pool_id %llu\npool_name %s\n" |
| 4567 | "image_id %s\nimage_name %s\n" |
| 4568 | "snap_id %llu\nsnap_name %s\n" |
| 4569 | "overlap %llu\n", |
| 4570 | !count ? "" : "\n", /* first? */ |
| 4571 | spec->pool_id, spec->pool_name, |
| 4572 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4573 | spec->snap_id, spec->snap_name, |
| 4574 | rbd_dev->parent_overlap); |
| 4575 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4576 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4577 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4578 | } |
| 4579 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4580 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4581 | struct device_attribute *attr, |
| 4582 | const char *buf, |
| 4583 | size_t size) |
| 4584 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4585 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4586 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4587 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4588 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4589 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4590 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4591 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4592 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4593 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4594 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4595 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4596 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4597 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4598 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4599 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4600 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4601 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4602 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4603 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4604 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4605 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4606 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4607 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4608 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4609 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4610 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4611 | |
| 4612 | static struct attribute *rbd_attrs[] = { |
| 4613 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4614 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4615 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4616 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4617 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4618 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4619 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4620 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4621 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4622 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4623 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4624 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4625 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4626 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4627 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4628 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4629 | NULL |
| 4630 | }; |
| 4631 | |
| 4632 | static struct attribute_group rbd_attr_group = { |
| 4633 | .attrs = rbd_attrs, |
| 4634 | }; |
| 4635 | |
| 4636 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4637 | &rbd_attr_group, |
| 4638 | NULL |
| 4639 | }; |
| 4640 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4641 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4642 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4643 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4644 | .name = "rbd", |
| 4645 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4646 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4647 | }; |
| 4648 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4649 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4650 | { |
| 4651 | kref_get(&spec->kref); |
| 4652 | |
| 4653 | return spec; |
| 4654 | } |
| 4655 | |
| 4656 | static void rbd_spec_free(struct kref *kref); |
| 4657 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4658 | { |
| 4659 | if (spec) |
| 4660 | kref_put(&spec->kref, rbd_spec_free); |
| 4661 | } |
| 4662 | |
| 4663 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4664 | { |
| 4665 | struct rbd_spec *spec; |
| 4666 | |
| 4667 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4668 | if (!spec) |
| 4669 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4670 | |
| 4671 | spec->pool_id = CEPH_NOPOOL; |
| 4672 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4673 | kref_init(&spec->kref); |
| 4674 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4675 | return spec; |
| 4676 | } |
| 4677 | |
| 4678 | static void rbd_spec_free(struct kref *kref) |
| 4679 | { |
| 4680 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4681 | |
| 4682 | kfree(spec->pool_name); |
| 4683 | kfree(spec->image_id); |
| 4684 | kfree(spec->image_name); |
| 4685 | kfree(spec->snap_name); |
| 4686 | kfree(spec); |
| 4687 | } |
| 4688 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4689 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4690 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4691 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4692 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4693 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4694 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4695 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4696 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4697 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4698 | rbd_put_client(rbd_dev->rbd_client); |
| 4699 | rbd_spec_put(rbd_dev->spec); |
| 4700 | kfree(rbd_dev->opts); |
| 4701 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4702 | } |
| 4703 | |
| 4704 | static void rbd_dev_release(struct device *dev) |
| 4705 | { |
| 4706 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4707 | bool need_put = !!rbd_dev->opts; |
| 4708 | |
| 4709 | if (need_put) { |
| 4710 | destroy_workqueue(rbd_dev->task_wq); |
| 4711 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4712 | } |
| 4713 | |
| 4714 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4715 | |
| 4716 | /* |
| 4717 | * This is racy, but way better than putting module outside of |
| 4718 | * the release callback. The race window is pretty small, so |
| 4719 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4720 | */ |
| 4721 | if (need_put) |
| 4722 | module_put(THIS_MODULE); |
| 4723 | } |
| 4724 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4725 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4726 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4727 | { |
| 4728 | struct rbd_device *rbd_dev; |
| 4729 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4730 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4731 | if (!rbd_dev) |
| 4732 | return NULL; |
| 4733 | |
| 4734 | spin_lock_init(&rbd_dev->lock); |
| 4735 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4736 | init_rwsem(&rbd_dev->header_rwsem); |
| 4737 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4738 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4739 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4740 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4741 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4742 | mutex_init(&rbd_dev->watch_mutex); |
| 4743 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4744 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4745 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4746 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4747 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4748 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4749 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4750 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4751 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4752 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4753 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4754 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4755 | rbd_dev->dev.type = &rbd_device_type; |
| 4756 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4757 | device_initialize(&rbd_dev->dev); |
| 4758 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4759 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4760 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4761 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4762 | return rbd_dev; |
| 4763 | } |
| 4764 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4765 | /* |
| 4766 | * Create a mapping rbd_dev. |
| 4767 | */ |
| 4768 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4769 | struct rbd_spec *spec, |
| 4770 | struct rbd_options *opts) |
| 4771 | { |
| 4772 | struct rbd_device *rbd_dev; |
| 4773 | |
| 4774 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4775 | if (!rbd_dev) |
| 4776 | return NULL; |
| 4777 | |
| 4778 | rbd_dev->opts = opts; |
| 4779 | |
| 4780 | /* get an id and fill in device name */ |
| 4781 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4782 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4783 | GFP_KERNEL); |
| 4784 | if (rbd_dev->dev_id < 0) |
| 4785 | goto fail_rbd_dev; |
| 4786 | |
| 4787 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4788 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4789 | rbd_dev->name); |
| 4790 | if (!rbd_dev->task_wq) |
| 4791 | goto fail_dev_id; |
| 4792 | |
| 4793 | /* we have a ref from do_rbd_add() */ |
| 4794 | __module_get(THIS_MODULE); |
| 4795 | |
| 4796 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4797 | return rbd_dev; |
| 4798 | |
| 4799 | fail_dev_id: |
| 4800 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4801 | fail_rbd_dev: |
| 4802 | rbd_dev_free(rbd_dev); |
| 4803 | return NULL; |
| 4804 | } |
| 4805 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4806 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4807 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4808 | if (rbd_dev) |
| 4809 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4810 | } |
| 4811 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4812 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4813 | * Get the size and object order for an image snapshot, or if |
| 4814 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4815 | * image. |
| 4816 | */ |
| 4817 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4818 | u8 *order, u64 *snap_size) |
| 4819 | { |
| 4820 | __le64 snapid = cpu_to_le64(snap_id); |
| 4821 | int ret; |
| 4822 | struct { |
| 4823 | u8 order; |
| 4824 | __le64 size; |
| 4825 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4826 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4827 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4828 | &rbd_dev->header_oloc, "get_size", |
| 4829 | &snapid, sizeof(snapid), |
| 4830 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4831 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4832 | if (ret < 0) |
| 4833 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4834 | if (ret < sizeof (size_buf)) |
| 4835 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4836 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4837 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4838 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4839 | dout(" order %u", (unsigned int)*order); |
| 4840 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4841 | *snap_size = le64_to_cpu(size_buf.size); |
| 4842 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4843 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4844 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4845 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4846 | |
| 4847 | return 0; |
| 4848 | } |
| 4849 | |
| 4850 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4851 | { |
| 4852 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4853 | &rbd_dev->header.obj_order, |
| 4854 | &rbd_dev->header.image_size); |
| 4855 | } |
| 4856 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4857 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4858 | { |
| 4859 | void *reply_buf; |
| 4860 | int ret; |
| 4861 | void *p; |
| 4862 | |
| 4863 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4864 | if (!reply_buf) |
| 4865 | return -ENOMEM; |
| 4866 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4867 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4868 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4869 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4870 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4871 | if (ret < 0) |
| 4872 | goto out; |
| 4873 | |
| 4874 | p = reply_buf; |
| 4875 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4876 | p + ret, NULL, GFP_NOIO); |
| 4877 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4878 | |
| 4879 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4880 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4881 | rbd_dev->header.object_prefix = NULL; |
| 4882 | } else { |
| 4883 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4884 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4885 | out: |
| 4886 | kfree(reply_buf); |
| 4887 | |
| 4888 | return ret; |
| 4889 | } |
| 4890 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4891 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4892 | u64 *snap_features) |
| 4893 | { |
| 4894 | __le64 snapid = cpu_to_le64(snap_id); |
| 4895 | struct { |
| 4896 | __le64 features; |
| 4897 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4898 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4899 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4900 | int ret; |
| 4901 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4902 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4903 | &rbd_dev->header_oloc, "get_features", |
| 4904 | &snapid, sizeof(snapid), |
| 4905 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4906 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4907 | if (ret < 0) |
| 4908 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4909 | if (ret < sizeof (features_buf)) |
| 4910 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4911 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4912 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4913 | if (unsup) { |
| 4914 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4915 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4916 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4917 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4918 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4919 | *snap_features = le64_to_cpu(features_buf.features); |
| 4920 | |
| 4921 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4922 | (unsigned long long)snap_id, |
| 4923 | (unsigned long long)*snap_features, |
| 4924 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4925 | |
| 4926 | return 0; |
| 4927 | } |
| 4928 | |
| 4929 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4930 | { |
| 4931 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4932 | &rbd_dev->header.features); |
| 4933 | } |
| 4934 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4935 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4936 | { |
| 4937 | struct rbd_spec *parent_spec; |
| 4938 | size_t size; |
| 4939 | void *reply_buf = NULL; |
| 4940 | __le64 snapid; |
| 4941 | void *p; |
| 4942 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4943 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4944 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4945 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4946 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4947 | int ret; |
| 4948 | |
| 4949 | parent_spec = rbd_spec_alloc(); |
| 4950 | if (!parent_spec) |
| 4951 | return -ENOMEM; |
| 4952 | |
| 4953 | size = sizeof (__le64) + /* pool_id */ |
| 4954 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4955 | sizeof (__le64) + /* snap_id */ |
| 4956 | sizeof (__le64); /* overlap */ |
| 4957 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4958 | if (!reply_buf) { |
| 4959 | ret = -ENOMEM; |
| 4960 | goto out_err; |
| 4961 | } |
| 4962 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4963 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4964 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4965 | &rbd_dev->header_oloc, "get_parent", |
| 4966 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4967 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4968 | if (ret < 0) |
| 4969 | goto out_err; |
| 4970 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4971 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4972 | end = reply_buf + ret; |
| 4973 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4974 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4975 | if (pool_id == CEPH_NOPOOL) { |
| 4976 | /* |
| 4977 | * Either the parent never existed, or we have |
| 4978 | * record of it but the image got flattened so it no |
| 4979 | * longer has a parent. When the parent of a |
| 4980 | * layered image disappears we immediately set the |
| 4981 | * overlap to 0. The effect of this is that all new |
| 4982 | * requests will be treated as if the image had no |
| 4983 | * parent. |
| 4984 | */ |
| 4985 | if (rbd_dev->parent_overlap) { |
| 4986 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4987 | rbd_dev_parent_put(rbd_dev); |
| 4988 | pr_info("%s: clone image has been flattened\n", |
| 4989 | rbd_dev->disk->disk_name); |
| 4990 | } |
| 4991 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4992 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4993 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4994 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4995 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4996 | |
| 4997 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4998 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4999 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5000 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5001 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5002 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5003 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5004 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5005 | if (IS_ERR(image_id)) { |
| 5006 | ret = PTR_ERR(image_id); |
| 5007 | goto out_err; |
| 5008 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5009 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5010 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5011 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5012 | /* |
| 5013 | * The parent won't change (except when the clone is |
| 5014 | * flattened, already handled that). So we only need to |
| 5015 | * record the parent spec we have not already done so. |
| 5016 | */ |
| 5017 | if (!rbd_dev->parent_spec) { |
| 5018 | parent_spec->pool_id = pool_id; |
| 5019 | parent_spec->image_id = image_id; |
| 5020 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5021 | rbd_dev->parent_spec = parent_spec; |
| 5022 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5023 | } else { |
| 5024 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5028 | * We always update the parent overlap. If it's zero we issue |
| 5029 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5030 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5031 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5032 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5033 | /* refresh, careful to warn just once */ |
| 5034 | if (rbd_dev->parent_overlap) |
| 5035 | rbd_warn(rbd_dev, |
| 5036 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5037 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5038 | /* initial probe */ |
| 5039 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5040 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5041 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5042 | rbd_dev->parent_overlap = overlap; |
| 5043 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5044 | out: |
| 5045 | ret = 0; |
| 5046 | out_err: |
| 5047 | kfree(reply_buf); |
| 5048 | rbd_spec_put(parent_spec); |
| 5049 | |
| 5050 | return ret; |
| 5051 | } |
| 5052 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5053 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5054 | { |
| 5055 | struct { |
| 5056 | __le64 stripe_unit; |
| 5057 | __le64 stripe_count; |
| 5058 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5059 | size_t size = sizeof (striping_info_buf); |
| 5060 | void *p; |
| 5061 | u64 obj_size; |
| 5062 | u64 stripe_unit; |
| 5063 | u64 stripe_count; |
| 5064 | int ret; |
| 5065 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5066 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5067 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5068 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5069 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5070 | if (ret < 0) |
| 5071 | return ret; |
| 5072 | if (ret < size) |
| 5073 | return -ERANGE; |
| 5074 | |
| 5075 | /* |
| 5076 | * We don't actually support the "fancy striping" feature |
| 5077 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5078 | * defaults the behavior is the same as before. So find |
| 5079 | * out, and only fail if the image has non-default values. |
| 5080 | */ |
| 5081 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5082 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5083 | p = &striping_info_buf; |
| 5084 | stripe_unit = ceph_decode_64(&p); |
| 5085 | if (stripe_unit != obj_size) { |
| 5086 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5087 | "(got %llu want %llu)", |
| 5088 | stripe_unit, obj_size); |
| 5089 | return -EINVAL; |
| 5090 | } |
| 5091 | stripe_count = ceph_decode_64(&p); |
| 5092 | if (stripe_count != 1) { |
| 5093 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5094 | "(got %llu want 1)", stripe_count); |
| 5095 | return -EINVAL; |
| 5096 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5097 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5098 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5099 | |
| 5100 | return 0; |
| 5101 | } |
| 5102 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5103 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5104 | { |
| 5105 | __le64 data_pool_id; |
| 5106 | int ret; |
| 5107 | |
| 5108 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5109 | &rbd_dev->header_oloc, "get_data_pool", |
| 5110 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5111 | if (ret < 0) |
| 5112 | return ret; |
| 5113 | if (ret < sizeof(data_pool_id)) |
| 5114 | return -EBADMSG; |
| 5115 | |
| 5116 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5117 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5121 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5122 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5123 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5124 | size_t image_id_size; |
| 5125 | char *image_id; |
| 5126 | void *p; |
| 5127 | void *end; |
| 5128 | size_t size; |
| 5129 | void *reply_buf = NULL; |
| 5130 | size_t len = 0; |
| 5131 | char *image_name = NULL; |
| 5132 | int ret; |
| 5133 | |
| 5134 | rbd_assert(!rbd_dev->spec->image_name); |
| 5135 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5136 | len = strlen(rbd_dev->spec->image_id); |
| 5137 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5138 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5139 | if (!image_id) |
| 5140 | return NULL; |
| 5141 | |
| 5142 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5143 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5144 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5145 | |
| 5146 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5147 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5148 | if (!reply_buf) |
| 5149 | goto out; |
| 5150 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5151 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5152 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5153 | "dir_get_name", image_id, image_id_size, |
| 5154 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5155 | if (ret < 0) |
| 5156 | goto out; |
| 5157 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5158 | end = reply_buf + ret; |
| 5159 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5160 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5161 | if (IS_ERR(image_name)) |
| 5162 | image_name = NULL; |
| 5163 | else |
| 5164 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5165 | out: |
| 5166 | kfree(reply_buf); |
| 5167 | kfree(image_id); |
| 5168 | |
| 5169 | return image_name; |
| 5170 | } |
| 5171 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5172 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5173 | { |
| 5174 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5175 | const char *snap_name; |
| 5176 | u32 which = 0; |
| 5177 | |
| 5178 | /* Skip over names until we find the one we are looking for */ |
| 5179 | |
| 5180 | snap_name = rbd_dev->header.snap_names; |
| 5181 | while (which < snapc->num_snaps) { |
| 5182 | if (!strcmp(name, snap_name)) |
| 5183 | return snapc->snaps[which]; |
| 5184 | snap_name += strlen(snap_name) + 1; |
| 5185 | which++; |
| 5186 | } |
| 5187 | return CEPH_NOSNAP; |
| 5188 | } |
| 5189 | |
| 5190 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5191 | { |
| 5192 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5193 | u32 which; |
| 5194 | bool found = false; |
| 5195 | u64 snap_id; |
| 5196 | |
| 5197 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5198 | const char *snap_name; |
| 5199 | |
| 5200 | snap_id = snapc->snaps[which]; |
| 5201 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5202 | if (IS_ERR(snap_name)) { |
| 5203 | /* ignore no-longer existing snapshots */ |
| 5204 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5205 | continue; |
| 5206 | else |
| 5207 | break; |
| 5208 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5209 | found = !strcmp(name, snap_name); |
| 5210 | kfree(snap_name); |
| 5211 | } |
| 5212 | return found ? snap_id : CEPH_NOSNAP; |
| 5213 | } |
| 5214 | |
| 5215 | /* |
| 5216 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5217 | * no snapshot by that name is found, or if an error occurs. |
| 5218 | */ |
| 5219 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5220 | { |
| 5221 | if (rbd_dev->image_format == 1) |
| 5222 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5223 | |
| 5224 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5225 | } |
| 5226 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5227 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5228 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5229 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5230 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5231 | { |
| 5232 | struct rbd_spec *spec = rbd_dev->spec; |
| 5233 | |
| 5234 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5235 | rbd_assert(spec->image_id && spec->image_name); |
| 5236 | rbd_assert(spec->snap_name); |
| 5237 | |
| 5238 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5239 | u64 snap_id; |
| 5240 | |
| 5241 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5242 | if (snap_id == CEPH_NOSNAP) |
| 5243 | return -ENOENT; |
| 5244 | |
| 5245 | spec->snap_id = snap_id; |
| 5246 | } else { |
| 5247 | spec->snap_id = CEPH_NOSNAP; |
| 5248 | } |
| 5249 | |
| 5250 | return 0; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * A parent image will have all ids but none of the names. |
| 5255 | * |
| 5256 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5257 | * can't figure out the name for an image id. |
| 5258 | */ |
| 5259 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5260 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5261 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5262 | struct rbd_spec *spec = rbd_dev->spec; |
| 5263 | const char *pool_name; |
| 5264 | const char *image_name; |
| 5265 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5266 | int ret; |
| 5267 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5268 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5269 | rbd_assert(spec->image_id); |
| 5270 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5271 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5272 | /* 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] | 5273 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5274 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5275 | if (!pool_name) { |
| 5276 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5277 | return -EIO; |
| 5278 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5279 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5280 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5281 | return -ENOMEM; |
| 5282 | |
| 5283 | /* Fetch the image name; tolerate failure here */ |
| 5284 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5285 | image_name = rbd_dev_image_name(rbd_dev); |
| 5286 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5287 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5288 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5289 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5290 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5291 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5292 | if (IS_ERR(snap_name)) { |
| 5293 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5294 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | spec->pool_name = pool_name; |
| 5298 | spec->image_name = image_name; |
| 5299 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5300 | |
| 5301 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5302 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5303 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5304 | kfree(image_name); |
| 5305 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5306 | return ret; |
| 5307 | } |
| 5308 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5309 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5310 | { |
| 5311 | size_t size; |
| 5312 | int ret; |
| 5313 | void *reply_buf; |
| 5314 | void *p; |
| 5315 | void *end; |
| 5316 | u64 seq; |
| 5317 | u32 snap_count; |
| 5318 | struct ceph_snap_context *snapc; |
| 5319 | u32 i; |
| 5320 | |
| 5321 | /* |
| 5322 | * We'll need room for the seq value (maximum snapshot id), |
| 5323 | * snapshot count, and array of that many snapshot ids. |
| 5324 | * For now we have a fixed upper limit on the number we're |
| 5325 | * prepared to receive. |
| 5326 | */ |
| 5327 | size = sizeof (__le64) + sizeof (__le32) + |
| 5328 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5329 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5330 | if (!reply_buf) |
| 5331 | return -ENOMEM; |
| 5332 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5333 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5334 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5335 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5336 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5337 | if (ret < 0) |
| 5338 | goto out; |
| 5339 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5340 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5341 | end = reply_buf + ret; |
| 5342 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5343 | ceph_decode_64_safe(&p, end, seq, out); |
| 5344 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5345 | |
| 5346 | /* |
| 5347 | * Make sure the reported number of snapshot ids wouldn't go |
| 5348 | * beyond the end of our buffer. But before checking that, |
| 5349 | * make sure the computed size of the snapshot context we |
| 5350 | * allocate is representable in a size_t. |
| 5351 | */ |
| 5352 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5353 | / sizeof (u64)) { |
| 5354 | ret = -EINVAL; |
| 5355 | goto out; |
| 5356 | } |
| 5357 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5358 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5359 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5360 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5361 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5362 | if (!snapc) { |
| 5363 | ret = -ENOMEM; |
| 5364 | goto out; |
| 5365 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5366 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5367 | for (i = 0; i < snap_count; i++) |
| 5368 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5369 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5370 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5371 | rbd_dev->header.snapc = snapc; |
| 5372 | |
| 5373 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5374 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5375 | out: |
| 5376 | kfree(reply_buf); |
| 5377 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5378 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5379 | } |
| 5380 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5381 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5382 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5383 | { |
| 5384 | size_t size; |
| 5385 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5386 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5387 | int ret; |
| 5388 | void *p; |
| 5389 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5390 | char *snap_name; |
| 5391 | |
| 5392 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5393 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5394 | if (!reply_buf) |
| 5395 | return ERR_PTR(-ENOMEM); |
| 5396 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5397 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5398 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5399 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5400 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5401 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5402 | if (ret < 0) { |
| 5403 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5404 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5405 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5406 | |
| 5407 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5408 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5409 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5410 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5411 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5412 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5413 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5414 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5415 | out: |
| 5416 | kfree(reply_buf); |
| 5417 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5418 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5419 | } |
| 5420 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5421 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5422 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5423 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5424 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5425 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5426 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5427 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5428 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5429 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5430 | if (first_time) { |
| 5431 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5432 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5433 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5434 | } |
| 5435 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5436 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5437 | if (ret && first_time) { |
| 5438 | kfree(rbd_dev->header.object_prefix); |
| 5439 | rbd_dev->header.object_prefix = NULL; |
| 5440 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5441 | |
| 5442 | return ret; |
| 5443 | } |
| 5444 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5445 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5446 | { |
| 5447 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5448 | |
| 5449 | if (rbd_dev->image_format == 1) |
| 5450 | return rbd_dev_v1_header_info(rbd_dev); |
| 5451 | |
| 5452 | return rbd_dev_v2_header_info(rbd_dev); |
| 5453 | } |
| 5454 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5455 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5456 | * Skips over white space at *buf, and updates *buf to point to the |
| 5457 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5458 | * the token (string of non-white space characters) found. Note |
| 5459 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5460 | */ |
| 5461 | static inline size_t next_token(const char **buf) |
| 5462 | { |
| 5463 | /* |
| 5464 | * These are the characters that produce nonzero for |
| 5465 | * isspace() in the "C" and "POSIX" locales. |
| 5466 | */ |
| 5467 | const char *spaces = " \f\n\r\t\v"; |
| 5468 | |
| 5469 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5470 | |
| 5471 | return strcspn(*buf, spaces); /* Return token length */ |
| 5472 | } |
| 5473 | |
| 5474 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5475 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5476 | * enough to hold a copy of it, and copies the token into the new |
| 5477 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5478 | * that a duplicate buffer is created even for a zero-length token. |
| 5479 | * |
| 5480 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5481 | * pointer if memory for the duplicate was not available. If |
| 5482 | * the lenp argument is a non-null pointer, the length of the token |
| 5483 | * (not including the '\0') is returned in *lenp. |
| 5484 | * |
| 5485 | * If successful, the *buf pointer will be updated to point beyond |
| 5486 | * the end of the found token. |
| 5487 | * |
| 5488 | * Note: uses GFP_KERNEL for allocation. |
| 5489 | */ |
| 5490 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5491 | { |
| 5492 | char *dup; |
| 5493 | size_t len; |
| 5494 | |
| 5495 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5496 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5497 | if (!dup) |
| 5498 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5499 | *(dup + len) = '\0'; |
| 5500 | *buf += len; |
| 5501 | |
| 5502 | if (lenp) |
| 5503 | *lenp = len; |
| 5504 | |
| 5505 | return dup; |
| 5506 | } |
| 5507 | |
| 5508 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5509 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5510 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5511 | * and the data written is passed here via a NUL-terminated buffer. |
| 5512 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5513 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5514 | * The information extracted from these options is recorded in |
| 5515 | * the other parameters which return dynamically-allocated |
| 5516 | * structures: |
| 5517 | * ceph_opts |
| 5518 | * The address of a pointer that will refer to a ceph options |
| 5519 | * structure. Caller must release the returned pointer using |
| 5520 | * ceph_destroy_options() when it is no longer needed. |
| 5521 | * rbd_opts |
| 5522 | * Address of an rbd options pointer. Fully initialized by |
| 5523 | * this function; caller must release with kfree(). |
| 5524 | * spec |
| 5525 | * Address of an rbd image specification pointer. Fully |
| 5526 | * initialized by this function based on parsed options. |
| 5527 | * Caller must release with rbd_spec_put(). |
| 5528 | * |
| 5529 | * The options passed take this form: |
| 5530 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5531 | * where: |
| 5532 | * <mon_addrs> |
| 5533 | * A comma-separated list of one or more monitor addresses. |
| 5534 | * A monitor address is an ip address, optionally followed |
| 5535 | * by a port number (separated by a colon). |
| 5536 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5537 | * <options> |
| 5538 | * A comma-separated list of ceph and/or rbd options. |
| 5539 | * <pool_name> |
| 5540 | * The name of the rados pool containing the rbd image. |
| 5541 | * <image_name> |
| 5542 | * The name of the image in that pool to map. |
| 5543 | * <snap_id> |
| 5544 | * An optional snapshot id. If provided, the mapping will |
| 5545 | * present data from the image at the time that snapshot was |
| 5546 | * created. The image head is used if no snapshot id is |
| 5547 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5548 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5549 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5550 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5551 | struct rbd_options **opts, |
| 5552 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5553 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5554 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5555 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5556 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5557 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5558 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5559 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5560 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5561 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5562 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5563 | |
| 5564 | /* The first four tokens are required */ |
| 5565 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5566 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5567 | if (!len) { |
| 5568 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5569 | return -EINVAL; |
| 5570 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5571 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5572 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5573 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5574 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5575 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5576 | options = dup_token(&buf, NULL); |
| 5577 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5578 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5579 | if (!*options) { |
| 5580 | rbd_warn(NULL, "no options provided"); |
| 5581 | goto out_err; |
| 5582 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5583 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5584 | spec = rbd_spec_alloc(); |
| 5585 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5586 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5587 | |
| 5588 | spec->pool_name = dup_token(&buf, NULL); |
| 5589 | if (!spec->pool_name) |
| 5590 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5591 | if (!*spec->pool_name) { |
| 5592 | rbd_warn(NULL, "no pool name provided"); |
| 5593 | goto out_err; |
| 5594 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5595 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5596 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5597 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5598 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5599 | if (!*spec->image_name) { |
| 5600 | rbd_warn(NULL, "no image name provided"); |
| 5601 | goto out_err; |
| 5602 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5603 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5604 | /* |
| 5605 | * Snapshot name is optional; default is to use "-" |
| 5606 | * (indicating the head/no snapshot). |
| 5607 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5608 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5609 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5610 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5611 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5612 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5613 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5614 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5615 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5616 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5617 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5618 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5619 | *(snap_name + len) = '\0'; |
| 5620 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5621 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5622 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5623 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5624 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5625 | if (!rbd_opts) |
| 5626 | goto out_mem; |
| 5627 | |
| 5628 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5629 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5630 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5631 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5632 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5633 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5634 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5635 | if (IS_ERR(copts)) { |
| 5636 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5637 | goto out_err; |
| 5638 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5639 | kfree(options); |
| 5640 | |
| 5641 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5642 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5643 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5644 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5645 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5646 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5647 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5648 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5649 | kfree(rbd_opts); |
| 5650 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5651 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5652 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5653 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5654 | } |
| 5655 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5656 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5657 | * Return pool id (>= 0) or a negative error code. |
| 5658 | */ |
| 5659 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5660 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5661 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5662 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5663 | int tries = 0; |
| 5664 | int ret; |
| 5665 | |
| 5666 | again: |
| 5667 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5668 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5669 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5670 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5671 | if (ret < 0) |
| 5672 | return ret; |
| 5673 | |
| 5674 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5675 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5676 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5677 | newest_epoch, |
| 5678 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5679 | goto again; |
| 5680 | } else { |
| 5681 | /* the osdmap we have is new enough */ |
| 5682 | return -ENOENT; |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | return ret; |
| 5687 | } |
| 5688 | |
| 5689 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5690 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5691 | * name given to it by the user. Internally, that identifier is |
| 5692 | * what's used to specify the names of objects related to the image. |
| 5693 | * |
| 5694 | * A special "rbd id" object is used to map an rbd image name to its |
| 5695 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5696 | * with the supplied name. |
| 5697 | * |
| 5698 | * This function will record the given rbd_dev's image_id field if |
| 5699 | * it can be determined, and in that case will return 0. If any |
| 5700 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5701 | * image_id field will be unchanged (and should be NULL). |
| 5702 | */ |
| 5703 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5704 | { |
| 5705 | int ret; |
| 5706 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5707 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5708 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5709 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5710 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5711 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5712 | * When probing a parent image, the image id is already |
| 5713 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5714 | * need to fetch the image id again in this case. We |
| 5715 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5716 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5717 | if (rbd_dev->spec->image_id) { |
| 5718 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5719 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5720 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5721 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5722 | |
| 5723 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5724 | * First, see if the format 2 image id file exists, and if |
| 5725 | * so, get the image's persistent id from it. |
| 5726 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5727 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5728 | rbd_dev->spec->image_name); |
| 5729 | if (ret) |
| 5730 | return ret; |
| 5731 | |
| 5732 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5733 | |
| 5734 | /* Response will be an encoded string, which includes a length */ |
| 5735 | |
| 5736 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5737 | response = kzalloc(size, GFP_NOIO); |
| 5738 | if (!response) { |
| 5739 | ret = -ENOMEM; |
| 5740 | goto out; |
| 5741 | } |
| 5742 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5743 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5744 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5745 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5746 | "get_id", NULL, 0, |
| 5747 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5748 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5749 | if (ret == -ENOENT) { |
| 5750 | image_id = kstrdup("", GFP_KERNEL); |
| 5751 | ret = image_id ? 0 : -ENOMEM; |
| 5752 | if (!ret) |
| 5753 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5754 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5755 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5756 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5757 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5758 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5759 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5760 | if (!ret) |
| 5761 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | if (!ret) { |
| 5765 | rbd_dev->spec->image_id = image_id; |
| 5766 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5767 | } |
| 5768 | out: |
| 5769 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5770 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5771 | return ret; |
| 5772 | } |
| 5773 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5774 | /* |
| 5775 | * Undo whatever state changes are made by v1 or v2 header info |
| 5776 | * call. |
| 5777 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5778 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5779 | { |
| 5780 | struct rbd_image_header *header; |
| 5781 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5782 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5783 | |
| 5784 | /* Free dynamic fields from the header, then zero it out */ |
| 5785 | |
| 5786 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5787 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5788 | kfree(header->snap_sizes); |
| 5789 | kfree(header->snap_names); |
| 5790 | kfree(header->object_prefix); |
| 5791 | memset(header, 0, sizeof (*header)); |
| 5792 | } |
| 5793 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5794 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5795 | { |
| 5796 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5797 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5798 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5799 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5800 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5801 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5802 | /* |
| 5803 | * Get the and check features for the image. Currently the |
| 5804 | * features are assumed to never change. |
| 5805 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5806 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5807 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5808 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5809 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5810 | /* If the image supports fancy striping, get its parameters */ |
| 5811 | |
| 5812 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5813 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5814 | if (ret < 0) |
| 5815 | goto out_err; |
| 5816 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5817 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5818 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5819 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5820 | if (ret) |
| 5821 | goto out_err; |
| 5822 | } |
| 5823 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5824 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5825 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5826 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5827 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5828 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5829 | kfree(rbd_dev->header.object_prefix); |
| 5830 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5831 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5832 | } |
| 5833 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5834 | /* |
| 5835 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5836 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5837 | * length of the already discovered part of the parent chain. |
| 5838 | */ |
| 5839 | 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] | 5840 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5841 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5842 | int ret; |
| 5843 | |
| 5844 | if (!rbd_dev->parent_spec) |
| 5845 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5846 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5847 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5848 | pr_info("parent chain is too long (%d)\n", depth); |
| 5849 | ret = -EINVAL; |
| 5850 | goto out_err; |
| 5851 | } |
| 5852 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5853 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5854 | if (!parent) { |
| 5855 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5856 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5857 | } |
| 5858 | |
| 5859 | /* |
| 5860 | * Images related by parent/child relationships always share |
| 5861 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5862 | */ |
| 5863 | __rbd_get_client(rbd_dev->rbd_client); |
| 5864 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5865 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5866 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5867 | if (ret < 0) |
| 5868 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5869 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5870 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5871 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5872 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5873 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5874 | out_err: |
| 5875 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5876 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5877 | return ret; |
| 5878 | } |
| 5879 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5880 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5881 | { |
| 5882 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5883 | rbd_dev_mapping_clear(rbd_dev); |
| 5884 | rbd_free_disk(rbd_dev); |
| 5885 | if (!single_major) |
| 5886 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5887 | } |
| 5888 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5889 | /* |
| 5890 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5891 | * upon return. |
| 5892 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5893 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5894 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5895 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5896 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5897 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5898 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5899 | if (!single_major) { |
| 5900 | ret = register_blkdev(0, rbd_dev->name); |
| 5901 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5902 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5903 | |
| 5904 | rbd_dev->major = ret; |
| 5905 | rbd_dev->minor = 0; |
| 5906 | } else { |
| 5907 | rbd_dev->major = rbd_major; |
| 5908 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5909 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5910 | |
| 5911 | /* Set up the blkdev mapping. */ |
| 5912 | |
| 5913 | ret = rbd_init_disk(rbd_dev); |
| 5914 | if (ret) |
| 5915 | goto err_out_blkdev; |
| 5916 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5917 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5918 | if (ret) |
| 5919 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5920 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5921 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 5922 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5923 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5924 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5925 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5926 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5927 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5928 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5929 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5930 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5931 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5932 | err_out_mapping: |
| 5933 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5934 | err_out_disk: |
| 5935 | rbd_free_disk(rbd_dev); |
| 5936 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5937 | if (!single_major) |
| 5938 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5939 | err_out_unlock: |
| 5940 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5941 | return ret; |
| 5942 | } |
| 5943 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5944 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5945 | { |
| 5946 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5947 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5948 | |
| 5949 | /* Record the header object name for this rbd image. */ |
| 5950 | |
| 5951 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5952 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5953 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5954 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5955 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5956 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5957 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5958 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5959 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5960 | } |
| 5961 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5962 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5963 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5964 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5965 | if (rbd_dev->opts) |
| 5966 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5967 | rbd_dev->image_format = 0; |
| 5968 | kfree(rbd_dev->spec->image_id); |
| 5969 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5970 | } |
| 5971 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5972 | /* |
| 5973 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5974 | * device. If this image is the one being mapped (i.e., not a |
| 5975 | * parent), initiate a watch on its header object before using that |
| 5976 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5977 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5978 | 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] | 5979 | { |
| 5980 | int ret; |
| 5981 | |
| 5982 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5983 | * Get the id from the image id object. Unless there's an |
| 5984 | * error, rbd_dev->spec->image_id will be filled in with |
| 5985 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5986 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5987 | */ |
| 5988 | ret = rbd_dev_image_id(rbd_dev); |
| 5989 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5990 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5991 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5992 | ret = rbd_dev_header_name(rbd_dev); |
| 5993 | if (ret) |
| 5994 | goto err_out_format; |
| 5995 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5996 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5997 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5998 | if (ret) { |
| 5999 | if (ret == -ENOENT) |
| 6000 | pr_info("image %s/%s does not exist\n", |
| 6001 | rbd_dev->spec->pool_name, |
| 6002 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6003 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6004 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6005 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6006 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6007 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6008 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6009 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6010 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6011 | /* |
| 6012 | * If this image is the one being mapped, we have pool name and |
| 6013 | * id, image name and id, and snap name - need to fill snap id. |
| 6014 | * Otherwise this is a parent image, identified by pool, image |
| 6015 | * and snap ids - need to fill in names for those ids. |
| 6016 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6017 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6018 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6019 | else |
| 6020 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6021 | if (ret) { |
| 6022 | if (ret == -ENOENT) |
| 6023 | pr_info("snap %s/%s@%s does not exist\n", |
| 6024 | rbd_dev->spec->pool_name, |
| 6025 | rbd_dev->spec->image_name, |
| 6026 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6027 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6028 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6029 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6030 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6031 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6032 | if (ret) |
| 6033 | goto err_out_probe; |
| 6034 | |
| 6035 | /* |
| 6036 | * Need to warn users if this image is the one being |
| 6037 | * mapped and has a parent. |
| 6038 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6039 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6040 | rbd_warn(rbd_dev, |
| 6041 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6042 | } |
| 6043 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6044 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6045 | if (ret) |
| 6046 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6047 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6048 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6049 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6050 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6051 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6052 | err_out_probe: |
| 6053 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6054 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6055 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6056 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6057 | err_out_format: |
| 6058 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6059 | kfree(rbd_dev->spec->image_id); |
| 6060 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6061 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6062 | } |
| 6063 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6064 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6065 | const char *buf, |
| 6066 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6067 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6068 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6069 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6070 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6071 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6072 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6073 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6074 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6075 | |
| 6076 | if (!try_module_get(THIS_MODULE)) |
| 6077 | return -ENODEV; |
| 6078 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6079 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6080 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6081 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6082 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6083 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6084 | rbdc = rbd_get_client(ceph_opts); |
| 6085 | if (IS_ERR(rbdc)) { |
| 6086 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6087 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6088 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6089 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6090 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6091 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6092 | if (rc < 0) { |
| 6093 | if (rc == -ENOENT) |
| 6094 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6095 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6096 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6097 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6098 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6099 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6100 | if (!rbd_dev) { |
| 6101 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6102 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6103 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6104 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6105 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6106 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6107 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6108 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6109 | if (!rbd_dev->config_info) { |
| 6110 | rc = -ENOMEM; |
| 6111 | goto err_out_rbd_dev; |
| 6112 | } |
| 6113 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6114 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6115 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6116 | if (rc < 0) { |
| 6117 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6118 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6119 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6120 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6121 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6122 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6123 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6124 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6125 | read_only = true; |
| 6126 | rbd_dev->mapping.read_only = read_only; |
| 6127 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6128 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6129 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6130 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6131 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6132 | /* Everything's ready. Announce the disk to the world. */ |
| 6133 | |
| 6134 | rc = device_add(&rbd_dev->dev); |
| 6135 | if (rc) |
| 6136 | goto err_out_device_setup; |
| 6137 | |
| 6138 | add_disk(rbd_dev->disk); |
| 6139 | /* see rbd_init_disk() */ |
| 6140 | blk_put_queue(rbd_dev->disk->queue); |
| 6141 | |
| 6142 | spin_lock(&rbd_dev_list_lock); |
| 6143 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6144 | spin_unlock(&rbd_dev_list_lock); |
| 6145 | |
| 6146 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6147 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6148 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6149 | rc = count; |
| 6150 | out: |
| 6151 | module_put(THIS_MODULE); |
| 6152 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6153 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6154 | err_out_device_setup: |
| 6155 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6156 | err_out_image_probe: |
| 6157 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6158 | err_out_rbd_dev: |
| 6159 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6160 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6161 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6162 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6163 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6164 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6165 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6166 | } |
| 6167 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6168 | static ssize_t rbd_add(struct bus_type *bus, |
| 6169 | const char *buf, |
| 6170 | size_t count) |
| 6171 | { |
| 6172 | if (single_major) |
| 6173 | return -EINVAL; |
| 6174 | |
| 6175 | return do_rbd_add(bus, buf, count); |
| 6176 | } |
| 6177 | |
| 6178 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6179 | const char *buf, |
| 6180 | size_t count) |
| 6181 | { |
| 6182 | return do_rbd_add(bus, buf, count); |
| 6183 | } |
| 6184 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6185 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6186 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6187 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6188 | struct rbd_device *first = rbd_dev; |
| 6189 | struct rbd_device *second = first->parent; |
| 6190 | struct rbd_device *third; |
| 6191 | |
| 6192 | /* |
| 6193 | * Follow to the parent with no grandparent and |
| 6194 | * remove it. |
| 6195 | */ |
| 6196 | while (second && (third = second->parent)) { |
| 6197 | first = second; |
| 6198 | second = third; |
| 6199 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6200 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6201 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6202 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6203 | first->parent = NULL; |
| 6204 | first->parent_overlap = 0; |
| 6205 | |
| 6206 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6207 | rbd_spec_put(first->parent_spec); |
| 6208 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6212 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6213 | const char *buf, |
| 6214 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6215 | { |
| 6216 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6217 | struct list_head *tmp; |
| 6218 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6219 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6220 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6221 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6222 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6223 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6224 | dev_id = -1; |
| 6225 | opt_buf[0] = '\0'; |
| 6226 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6227 | if (dev_id < 0) { |
| 6228 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6229 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6230 | } |
| 6231 | if (opt_buf[0] != '\0') { |
| 6232 | if (!strcmp(opt_buf, "force")) { |
| 6233 | force = true; |
| 6234 | } else { |
| 6235 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6236 | return -EINVAL; |
| 6237 | } |
| 6238 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6239 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6240 | ret = -ENOENT; |
| 6241 | spin_lock(&rbd_dev_list_lock); |
| 6242 | list_for_each(tmp, &rbd_dev_list) { |
| 6243 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6244 | if (rbd_dev->dev_id == dev_id) { |
| 6245 | ret = 0; |
| 6246 | break; |
| 6247 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6248 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6249 | if (!ret) { |
| 6250 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6251 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6252 | ret = -EBUSY; |
| 6253 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6254 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6255 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6256 | spin_unlock_irq(&rbd_dev->lock); |
| 6257 | } |
| 6258 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6259 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6260 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6261 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6262 | if (force) { |
| 6263 | /* |
| 6264 | * Prevent new IO from being queued and wait for existing |
| 6265 | * IO to complete/fail. |
| 6266 | */ |
| 6267 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6268 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6269 | } |
| 6270 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6271 | del_gendisk(rbd_dev->disk); |
| 6272 | spin_lock(&rbd_dev_list_lock); |
| 6273 | list_del_init(&rbd_dev->node); |
| 6274 | spin_unlock(&rbd_dev_list_lock); |
| 6275 | device_del(&rbd_dev->dev); |
| 6276 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6277 | down_write(&rbd_dev->lock_rwsem); |
| 6278 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6279 | rbd_unlock(rbd_dev); |
| 6280 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6281 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6282 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6283 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6284 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6285 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6286 | } |
| 6287 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6288 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6289 | const char *buf, |
| 6290 | size_t count) |
| 6291 | { |
| 6292 | if (single_major) |
| 6293 | return -EINVAL; |
| 6294 | |
| 6295 | return do_rbd_remove(bus, buf, count); |
| 6296 | } |
| 6297 | |
| 6298 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6299 | const char *buf, |
| 6300 | size_t count) |
| 6301 | { |
| 6302 | return do_rbd_remove(bus, buf, count); |
| 6303 | } |
| 6304 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6305 | /* |
| 6306 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6307 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6308 | */ |
| 6309 | static int rbd_sysfs_init(void) |
| 6310 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6311 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6312 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6313 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6314 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6315 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6316 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6317 | ret = bus_register(&rbd_bus_type); |
| 6318 | if (ret < 0) |
| 6319 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6320 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6321 | return ret; |
| 6322 | } |
| 6323 | |
| 6324 | static void rbd_sysfs_cleanup(void) |
| 6325 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6326 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6327 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6328 | } |
| 6329 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6330 | static int rbd_slab_init(void) |
| 6331 | { |
| 6332 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6333 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6334 | if (!rbd_img_request_cache) |
| 6335 | return -ENOMEM; |
| 6336 | |
| 6337 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6338 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6339 | if (!rbd_obj_request_cache) |
| 6340 | goto out_err; |
| 6341 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6342 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6343 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6344 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6345 | kmem_cache_destroy(rbd_img_request_cache); |
| 6346 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6347 | return -ENOMEM; |
| 6348 | } |
| 6349 | |
| 6350 | static void rbd_slab_exit(void) |
| 6351 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6352 | rbd_assert(rbd_obj_request_cache); |
| 6353 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6354 | rbd_obj_request_cache = NULL; |
| 6355 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6356 | rbd_assert(rbd_img_request_cache); |
| 6357 | kmem_cache_destroy(rbd_img_request_cache); |
| 6358 | rbd_img_request_cache = NULL; |
| 6359 | } |
| 6360 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6361 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6362 | { |
| 6363 | int rc; |
| 6364 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6365 | if (!libceph_compatible(NULL)) { |
| 6366 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6367 | return -EINVAL; |
| 6368 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6369 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6370 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6371 | if (rc) |
| 6372 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6373 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6374 | /* |
| 6375 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6376 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6377 | */ |
| 6378 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6379 | if (!rbd_wq) { |
| 6380 | rc = -ENOMEM; |
| 6381 | goto err_out_slab; |
| 6382 | } |
| 6383 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6384 | if (single_major) { |
| 6385 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6386 | if (rbd_major < 0) { |
| 6387 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6388 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6389 | } |
| 6390 | } |
| 6391 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6392 | rc = rbd_sysfs_init(); |
| 6393 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6394 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6395 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6396 | if (single_major) |
| 6397 | pr_info("loaded (major %d)\n", rbd_major); |
| 6398 | else |
| 6399 | pr_info("loaded\n"); |
| 6400 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6401 | return 0; |
| 6402 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6403 | err_out_blkdev: |
| 6404 | if (single_major) |
| 6405 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6406 | err_out_wq: |
| 6407 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6408 | err_out_slab: |
| 6409 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6410 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6411 | } |
| 6412 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6413 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6414 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6415 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6416 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6417 | if (single_major) |
| 6418 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6419 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6420 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6421 | } |
| 6422 | |
| 6423 | module_init(rbd_init); |
| 6424 | module_exit(rbd_exit); |
| 6425 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6426 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6427 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6428 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6429 | /* following authorship retained from original osdblk.c */ |
| 6430 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6431 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6432 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6433 | MODULE_LICENSE("GPL"); |