Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | rbd.c -- Export ceph rados objects as a Linux block device |
| 4 | |
| 5 | |
| 6 | based on drivers/block/osdblk.c: |
| 7 | |
| 8 | Copyright 2009 Red Hat, Inc. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; see the file COPYING. If not, write to |
| 21 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | |
| 23 | |
| 24 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 25 | For usage instructions, please refer to: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 26 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 27 | Documentation/ABI/testing/sysfs-bus-rbd |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 28 | |
| 29 | */ |
| 30 | |
| 31 | #include <linux/ceph/libceph.h> |
| 32 | #include <linux/ceph/osd_client.h> |
| 33 | #include <linux/ceph/mon_client.h> |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 34 | #include <linux/ceph/cls_lock_client.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 35 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 36 | #include <linux/parser.h> |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 37 | #include <linux/bsearch.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/device.h> |
| 41 | #include <linux/module.h> |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 42 | #include <linux/blk-mq.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 43 | #include <linux/fs.h> |
| 44 | #include <linux/blkdev.h> |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 45 | #include <linux/slab.h> |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 46 | #include <linux/idr.h> |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 47 | #include <linux/workqueue.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 48 | |
| 49 | #include "rbd_types.h" |
| 50 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 51 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
| 52 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 53 | /* |
| 54 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 55 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 56 | * universally 512 bytes. These symbols are just slightly more |
| 57 | * meaningful than the bare numbers they represent. |
| 58 | */ |
| 59 | #define SECTOR_SHIFT 9 |
| 60 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 61 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Increment the given counter and return its updated value. |
| 64 | * If the counter is already 0 it will not be incremented. |
| 65 | * If the counter is already at its maximum value returns |
| 66 | * -EINVAL without updating it. |
| 67 | */ |
| 68 | static int atomic_inc_return_safe(atomic_t *v) |
| 69 | { |
| 70 | unsigned int counter; |
| 71 | |
| 72 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); |
| 73 | if (counter <= (unsigned int)INT_MAX) |
| 74 | return (int)counter; |
| 75 | |
| 76 | atomic_dec(v); |
| 77 | |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 82 | static int atomic_dec_return_safe(atomic_t *v) |
| 83 | { |
| 84 | int counter; |
| 85 | |
| 86 | counter = atomic_dec_return(v); |
| 87 | if (counter >= 0) |
| 88 | return counter; |
| 89 | |
| 90 | atomic_inc(v); |
| 91 | |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 95 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 96 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 97 | #define RBD_MINORS_PER_MAJOR 256 |
| 98 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 99 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 100 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 101 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 102 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 103 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 104 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 105 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 106 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 107 | |
| 108 | #define RBD_SNAP_HEAD_NAME "-" |
| 109 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 110 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 111 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 112 | /* This allows a single page to hold an image name sent by OSD */ |
| 113 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 114 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 115 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 116 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 117 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 118 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 119 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 120 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 121 | /* Feature bits */ |
| 122 | |
Alex Elder | 5cbf6f12 | 2013-04-11 09:29:48 -0500 | [diff] [blame] | 123 | #define RBD_FEATURE_LAYERING (1<<0) |
| 124 | #define RBD_FEATURE_STRIPINGV2 (1<<1) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2) |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 126 | #define RBD_FEATURE_DATA_POOL (1<<7) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 127 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 128 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 129 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
| 130 | RBD_FEATURE_DATA_POOL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 131 | |
| 132 | /* Features supported by this (client software) implementation. */ |
| 133 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 134 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 135 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 136 | /* |
| 137 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 138 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 139 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 140 | #define DEV_NAME_LEN 32 |
| 141 | |
| 142 | /* |
| 143 | * block device image metadata (in-memory version) |
| 144 | */ |
| 145 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 146 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 147 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 148 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 149 | u64 stripe_unit; |
| 150 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 151 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 152 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 153 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 154 | /* The remaining fields need to be updated occasionally */ |
| 155 | u64 image_size; |
| 156 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 157 | char *snap_names; /* format 1 only */ |
| 158 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 161 | /* |
| 162 | * An rbd image specification. |
| 163 | * |
| 164 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 165 | * identify an image. Each rbd_dev structure includes a pointer to |
| 166 | * an rbd_spec structure that encapsulates this identity. |
| 167 | * |
| 168 | * Each of the id's in an rbd_spec has an associated name. For a |
| 169 | * user-mapped image, the names are supplied and the id's associated |
| 170 | * with them are looked up. For a layered image, a parent image is |
| 171 | * defined by the tuple, and the names are looked up. |
| 172 | * |
| 173 | * An rbd_dev structure contains a parent_spec pointer which is |
| 174 | * non-null if the image it represents is a child in a layered |
| 175 | * image. This pointer will refer to the rbd_spec structure used |
| 176 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 177 | * is shared between the parent and child). |
| 178 | * |
| 179 | * Since these structures are populated once, during the discovery |
| 180 | * phase of image construction, they are effectively immutable so |
| 181 | * we make no effort to synchronize access to them. |
| 182 | * |
| 183 | * Note that code herein does not assume the image name is known (it |
| 184 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 185 | */ |
| 186 | struct rbd_spec { |
| 187 | u64 pool_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 188 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 189 | |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 190 | const char *image_id; |
| 191 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 192 | |
| 193 | u64 snap_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 194 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 195 | |
| 196 | struct kref kref; |
| 197 | }; |
| 198 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 199 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 200 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 201 | */ |
| 202 | struct rbd_client { |
| 203 | struct ceph_client *client; |
| 204 | struct kref kref; |
| 205 | struct list_head node; |
| 206 | }; |
| 207 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 208 | struct rbd_img_request; |
| 209 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); |
| 210 | |
| 211 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ |
| 212 | |
| 213 | struct rbd_obj_request; |
| 214 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); |
| 215 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 216 | enum obj_request_type { |
| 217 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES |
| 218 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 219 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 220 | enum obj_operation_type { |
| 221 | OBJ_OP_WRITE, |
| 222 | OBJ_OP_READ, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 223 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 224 | }; |
| 225 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 226 | enum obj_req_flags { |
| 227 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 228 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 229 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
| 230 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 231 | }; |
| 232 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 233 | struct rbd_obj_request { |
| 234 | const char *object_name; |
| 235 | u64 offset; /* object start byte */ |
| 236 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 237 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 238 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 239 | /* |
| 240 | * An object request associated with an image will have its |
| 241 | * img_data flag set; a standalone object request will not. |
| 242 | * |
| 243 | * A standalone object request will have which == BAD_WHICH |
| 244 | * and a null obj_request pointer. |
| 245 | * |
| 246 | * An object request initiated in support of a layered image |
| 247 | * object (to check for its existence before a write) will |
| 248 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 249 | * |
| 250 | * Finally, an object request for rbd image data will have |
| 251 | * which != BAD_WHICH, and will have a non-null img_request |
| 252 | * pointer. The value of which will be in the range |
| 253 | * 0..(img_request->obj_request_count-1). |
| 254 | */ |
| 255 | union { |
| 256 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 257 | struct { |
| 258 | struct rbd_img_request *img_request; |
| 259 | u64 img_offset; |
| 260 | /* links for img_request->obj_requests list */ |
| 261 | struct list_head links; |
| 262 | }; |
| 263 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 264 | u32 which; /* posn image request list */ |
| 265 | |
| 266 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 267 | union { |
| 268 | struct bio *bio_list; |
| 269 | struct { |
| 270 | struct page **pages; |
| 271 | u32 page_count; |
| 272 | }; |
| 273 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 274 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 275 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | |
| 277 | struct ceph_osd_request *osd_req; |
| 278 | |
| 279 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 280 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | |
| 282 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 283 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 284 | |
| 285 | struct kref kref; |
| 286 | }; |
| 287 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 288 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 289 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 290 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 291 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 292 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 293 | }; |
| 294 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 295 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | struct rbd_device *rbd_dev; |
| 297 | u64 offset; /* starting image byte offset */ |
| 298 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 299 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 300 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 301 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 303 | }; |
| 304 | union { |
| 305 | struct request *rq; /* block request */ |
| 306 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 307 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 308 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 309 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 310 | spinlock_t completion_lock;/* protects next_completion */ |
| 311 | u32 next_completion; |
| 312 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 313 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 314 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 315 | |
| 316 | u32 obj_request_count; |
| 317 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 318 | |
| 319 | struct kref kref; |
| 320 | }; |
| 321 | |
| 322 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 323 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 324 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 325 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 326 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 327 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 328 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 329 | enum rbd_watch_state { |
| 330 | RBD_WATCH_STATE_UNREGISTERED, |
| 331 | RBD_WATCH_STATE_REGISTERED, |
| 332 | RBD_WATCH_STATE_ERROR, |
| 333 | }; |
| 334 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 335 | enum rbd_lock_state { |
| 336 | RBD_LOCK_STATE_UNLOCKED, |
| 337 | RBD_LOCK_STATE_LOCKED, |
| 338 | RBD_LOCK_STATE_RELEASING, |
| 339 | }; |
| 340 | |
| 341 | /* WatchNotify::ClientId */ |
| 342 | struct rbd_client_id { |
| 343 | u64 gid; |
| 344 | u64 handle; |
| 345 | }; |
| 346 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 347 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 348 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 349 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 350 | bool read_only; |
| 351 | }; |
| 352 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 353 | /* |
| 354 | * a single device |
| 355 | */ |
| 356 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 357 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 358 | |
| 359 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 360 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 362 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 363 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 364 | struct rbd_client *rbd_client; |
| 365 | |
| 366 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 367 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 368 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 369 | |
| 370 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 371 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 372 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 373 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 374 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 375 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 376 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 377 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 378 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 379 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 380 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 381 | struct mutex watch_mutex; |
| 382 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 383 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 384 | u64 watch_cookie; |
| 385 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 386 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 387 | struct rw_semaphore lock_rwsem; |
| 388 | enum rbd_lock_state lock_state; |
| 389 | struct rbd_client_id owner_cid; |
| 390 | struct work_struct acquired_lock_work; |
| 391 | struct work_struct released_lock_work; |
| 392 | struct delayed_work lock_dwork; |
| 393 | struct work_struct unlock_work; |
| 394 | wait_queue_head_t lock_waitq; |
| 395 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 396 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 397 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 398 | struct rbd_spec *parent_spec; |
| 399 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 400 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 401 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 402 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 403 | /* Block layer tags. */ |
| 404 | struct blk_mq_tag_set tag_set; |
| 405 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 406 | /* protects updating the header */ |
| 407 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 408 | |
| 409 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 410 | |
| 411 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 412 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 413 | /* sysfs related */ |
| 414 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 415 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 416 | }; |
| 417 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 418 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 419 | * Flag bits for rbd_dev->flags: |
| 420 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 421 | * by rbd_dev->lock |
| 422 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 423 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 424 | enum rbd_dev_flags { |
| 425 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 426 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 427 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 428 | }; |
| 429 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 430 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 431 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 432 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 433 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 434 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 435 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 436 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 437 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 438 | /* Slab caches for frequently-allocated structures */ |
| 439 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 440 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 441 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 442 | static struct kmem_cache *rbd_segment_name_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 443 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 444 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 445 | static DEFINE_IDA(rbd_dev_id_ida); |
| 446 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 447 | static struct workqueue_struct *rbd_wq; |
| 448 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 449 | /* |
| 450 | * Default to false for now, as single-major requires >= 0.75 version of |
| 451 | * userspace rbd utility. |
| 452 | */ |
| 453 | static bool single_major = false; |
| 454 | module_param(single_major, bool, S_IRUGO); |
| 455 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 456 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 457 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 458 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 459 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 460 | size_t count); |
| 461 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 462 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 463 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 464 | size_t count); |
| 465 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 466 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 467 | 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] | 468 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 469 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 470 | static int rbd_dev_id_to_minor(int dev_id) |
| 471 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 472 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int minor_to_rbd_dev_id(int minor) |
| 476 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 477 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 478 | } |
| 479 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 480 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
| 481 | { |
| 482 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 483 | rbd_dev->spec->snap_id == CEPH_NOSNAP && |
| 484 | !rbd_dev->mapping.read_only; |
| 485 | } |
| 486 | |
| 487 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 488 | { |
| 489 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 490 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 491 | } |
| 492 | |
| 493 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 494 | { |
| 495 | bool is_lock_owner; |
| 496 | |
| 497 | down_read(&rbd_dev->lock_rwsem); |
| 498 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 499 | up_read(&rbd_dev->lock_rwsem); |
| 500 | return is_lock_owner; |
| 501 | } |
| 502 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 503 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 504 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 505 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 506 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
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, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 513 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 514 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 515 | |
| 516 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 517 | struct attribute *attr, int index) |
| 518 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 519 | if (!single_major && |
| 520 | (attr == &bus_attr_add_single_major.attr || |
| 521 | attr == &bus_attr_remove_single_major.attr)) |
| 522 | return 0; |
| 523 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 524 | return attr->mode; |
| 525 | } |
| 526 | |
| 527 | static const struct attribute_group rbd_bus_group = { |
| 528 | .attrs = rbd_bus_attrs, |
| 529 | .is_visible = rbd_bus_is_visible, |
| 530 | }; |
| 531 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 532 | |
| 533 | static struct bus_type rbd_bus_type = { |
| 534 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 535 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 536 | }; |
| 537 | |
| 538 | static void rbd_root_dev_release(struct device *dev) |
| 539 | { |
| 540 | } |
| 541 | |
| 542 | static struct device rbd_root_dev = { |
| 543 | .init_name = "rbd", |
| 544 | .release = rbd_root_dev_release, |
| 545 | }; |
| 546 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 547 | static __printf(2, 3) |
| 548 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 549 | { |
| 550 | struct va_format vaf; |
| 551 | va_list args; |
| 552 | |
| 553 | va_start(args, fmt); |
| 554 | vaf.fmt = fmt; |
| 555 | vaf.va = &args; |
| 556 | |
| 557 | if (!rbd_dev) |
| 558 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 559 | else if (rbd_dev->disk) |
| 560 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 561 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 562 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 563 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 564 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 565 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 566 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 567 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 568 | else /* punt */ |
| 569 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 570 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 571 | va_end(args); |
| 572 | } |
| 573 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 574 | #ifdef RBD_DEBUG |
| 575 | #define rbd_assert(expr) \ |
| 576 | if (unlikely(!(expr))) { \ |
| 577 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 578 | "at line %d:\n\n" \ |
| 579 | "\trbd_assert(%s);\n\n", \ |
| 580 | __func__, __LINE__, #expr); \ |
| 581 | BUG(); \ |
| 582 | } |
| 583 | #else /* !RBD_DEBUG */ |
| 584 | # define rbd_assert(expr) ((void) 0) |
| 585 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 586 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 587 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 588 | 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] | 589 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 590 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 591 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 592 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 593 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 594 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 595 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 596 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 597 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 598 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 599 | u8 *order, u64 *snap_size); |
| 600 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 601 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 602 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 603 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 604 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 605 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 606 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 607 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 608 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 609 | return -EROFS; |
| 610 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 611 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 612 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 613 | removing = true; |
| 614 | else |
| 615 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 616 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 617 | if (removing) |
| 618 | return -ENOENT; |
| 619 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 620 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 621 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 622 | return 0; |
| 623 | } |
| 624 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 625 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 626 | { |
| 627 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 628 | unsigned long open_count_before; |
| 629 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 630 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 631 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 632 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 633 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 634 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 635 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 638 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 639 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 640 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 641 | int val; |
| 642 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 643 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 644 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 645 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 646 | if (get_user(val, (int __user *)(arg))) |
| 647 | return -EFAULT; |
| 648 | |
| 649 | ro = val ? true : false; |
| 650 | /* Snapshot doesn't allow to write*/ |
| 651 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 652 | return -EROFS; |
| 653 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 654 | spin_lock_irq(&rbd_dev->lock); |
| 655 | /* prevent others open this device */ |
| 656 | if (rbd_dev->open_count > 1) { |
| 657 | ret = -EBUSY; |
| 658 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 659 | } |
| 660 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 661 | if (rbd_dev->mapping.read_only != ro) { |
| 662 | rbd_dev->mapping.read_only = ro; |
| 663 | ro_changed = true; |
| 664 | } |
| 665 | |
| 666 | out: |
| 667 | spin_unlock_irq(&rbd_dev->lock); |
| 668 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 669 | if (ret == 0 && ro_changed) |
| 670 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 671 | |
| 672 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 676 | unsigned int cmd, unsigned long arg) |
| 677 | { |
| 678 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 679 | int ret = 0; |
| 680 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 681 | switch (cmd) { |
| 682 | case BLKROSET: |
| 683 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 684 | break; |
| 685 | default: |
| 686 | ret = -ENOTTY; |
| 687 | } |
| 688 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 689 | return ret; |
| 690 | } |
| 691 | |
| 692 | #ifdef CONFIG_COMPAT |
| 693 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 694 | unsigned int cmd, unsigned long arg) |
| 695 | { |
| 696 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 697 | } |
| 698 | #endif /* CONFIG_COMPAT */ |
| 699 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 700 | static const struct block_device_operations rbd_bd_ops = { |
| 701 | .owner = THIS_MODULE, |
| 702 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 703 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 704 | .ioctl = rbd_ioctl, |
| 705 | #ifdef CONFIG_COMPAT |
| 706 | .compat_ioctl = rbd_compat_ioctl, |
| 707 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 708 | }; |
| 709 | |
| 710 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 711 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 712 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 713 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 714 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 715 | { |
| 716 | struct rbd_client *rbdc; |
| 717 | int ret = -ENOMEM; |
| 718 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 719 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 720 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 721 | if (!rbdc) |
| 722 | goto out_opt; |
| 723 | |
| 724 | kref_init(&rbdc->kref); |
| 725 | INIT_LIST_HEAD(&rbdc->node); |
| 726 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 727 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 728 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 729 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 730 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 731 | |
| 732 | ret = ceph_open_session(rbdc->client); |
| 733 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 734 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 735 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 736 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 737 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 738 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 739 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 740 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 741 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 742 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 743 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 744 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 745 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 746 | kfree(rbdc); |
| 747 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 748 | if (ceph_opts) |
| 749 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 750 | dout("%s: error %d\n", __func__, ret); |
| 751 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 752 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 753 | } |
| 754 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 755 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 756 | { |
| 757 | kref_get(&rbdc->kref); |
| 758 | |
| 759 | return rbdc; |
| 760 | } |
| 761 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 762 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 763 | * Find a ceph client with specific addr and configuration. If |
| 764 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 765 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 766 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 767 | { |
| 768 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 769 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 770 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 771 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 772 | return NULL; |
| 773 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 774 | spin_lock(&rbd_client_list_lock); |
| 775 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 776 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 777 | __rbd_get_client(client_node); |
| 778 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 779 | found = true; |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | spin_unlock(&rbd_client_list_lock); |
| 784 | |
| 785 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 789 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 790 | */ |
| 791 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 792 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 793 | Opt_last_int, |
| 794 | /* int args above */ |
| 795 | Opt_last_string, |
| 796 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 797 | Opt_read_only, |
| 798 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 799 | Opt_lock_on_read, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 800 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 801 | }; |
| 802 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 803 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 804 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 805 | /* int args above */ |
| 806 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 807 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 808 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 809 | {Opt_read_write, "read_write"}, |
| 810 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 811 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 812 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 813 | }; |
| 814 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 815 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 816 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 817 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 818 | bool lock_on_read; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 819 | }; |
| 820 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 821 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 822 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 823 | #define RBD_LOCK_ON_READ_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 824 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 825 | static int parse_rbd_opts_token(char *c, void *private) |
| 826 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 827 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 828 | substring_t argstr[MAX_OPT_ARGS]; |
| 829 | int token, intval, ret; |
| 830 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 831 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 832 | if (token < Opt_last_int) { |
| 833 | ret = match_int(&argstr[0], &intval); |
| 834 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 835 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 836 | return ret; |
| 837 | } |
| 838 | dout("got int token %d val %d\n", token, intval); |
| 839 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 840 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 841 | } else { |
| 842 | dout("got token %d\n", token); |
| 843 | } |
| 844 | |
| 845 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 846 | case Opt_queue_depth: |
| 847 | if (intval < 1) { |
| 848 | pr_err("queue_depth out of range\n"); |
| 849 | return -EINVAL; |
| 850 | } |
| 851 | rbd_opts->queue_depth = intval; |
| 852 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 853 | case Opt_read_only: |
| 854 | rbd_opts->read_only = true; |
| 855 | break; |
| 856 | case Opt_read_write: |
| 857 | rbd_opts->read_only = false; |
| 858 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 859 | case Opt_lock_on_read: |
| 860 | rbd_opts->lock_on_read = true; |
| 861 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 862 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 863 | /* libceph prints "bad option" msg */ |
| 864 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 865 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 866 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 870 | static char* obj_op_name(enum obj_operation_type op_type) |
| 871 | { |
| 872 | switch (op_type) { |
| 873 | case OBJ_OP_READ: |
| 874 | return "read"; |
| 875 | case OBJ_OP_WRITE: |
| 876 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 877 | case OBJ_OP_DISCARD: |
| 878 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 879 | default: |
| 880 | return "???"; |
| 881 | } |
| 882 | } |
| 883 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 884 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 885 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 886 | * not exist create it. Either way, ceph_opts is consumed by this |
| 887 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 888 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 889 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 890 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 891 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 892 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 893 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 894 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 895 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 896 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 897 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 898 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 899 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 900 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 901 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /* |
| 905 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 906 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 907 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 908 | */ |
| 909 | static void rbd_client_release(struct kref *kref) |
| 910 | { |
| 911 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 912 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 913 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 914 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 915 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 916 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 917 | |
| 918 | ceph_destroy_client(rbdc->client); |
| 919 | kfree(rbdc); |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 924 | * it. |
| 925 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 926 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 927 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 928 | if (rbdc) |
| 929 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 932 | static bool rbd_image_format_valid(u32 image_format) |
| 933 | { |
| 934 | return image_format == 1 || image_format == 2; |
| 935 | } |
| 936 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 937 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 938 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 939 | size_t size; |
| 940 | u32 snap_count; |
| 941 | |
| 942 | /* The header has to start with the magic rbd header text */ |
| 943 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 944 | return false; |
| 945 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 946 | /* The bio layer requires at least sector-sized I/O */ |
| 947 | |
| 948 | if (ondisk->options.order < SECTOR_SHIFT) |
| 949 | return false; |
| 950 | |
| 951 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 952 | |
| 953 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 954 | return false; |
| 955 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 956 | /* |
| 957 | * The size of a snapshot header has to fit in a size_t, and |
| 958 | * that limits the number of snapshots. |
| 959 | */ |
| 960 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 961 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 962 | if (snap_count > size / sizeof (__le64)) |
| 963 | return false; |
| 964 | |
| 965 | /* |
| 966 | * Not only that, but the size of the entire the snapshot |
| 967 | * header must also be representable in a size_t. |
| 968 | */ |
| 969 | size -= snap_count * sizeof (__le64); |
| 970 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 971 | return false; |
| 972 | |
| 973 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 974 | } |
| 975 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 976 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 977 | * returns the size of an object in the image |
| 978 | */ |
| 979 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 980 | { |
| 981 | return 1U << header->obj_order; |
| 982 | } |
| 983 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 984 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 985 | { |
| 986 | if (rbd_dev->header.stripe_unit == 0 || |
| 987 | rbd_dev->header.stripe_count == 0) { |
| 988 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 989 | rbd_dev->header.stripe_count = 1; |
| 990 | } |
| 991 | |
| 992 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 993 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 994 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 995 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 996 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 997 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 998 | } |
| 999 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1000 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1001 | * Fill an rbd image header with information from the given format 1 |
| 1002 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1003 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1004 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1005 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1006 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1007 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1008 | bool first_time = header->object_prefix == NULL; |
| 1009 | struct ceph_snap_context *snapc; |
| 1010 | char *object_prefix = NULL; |
| 1011 | char *snap_names = NULL; |
| 1012 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1013 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1014 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1015 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1016 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1017 | /* Allocate this now to avoid having to handle failure below */ |
| 1018 | |
| 1019 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1020 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1021 | sizeof(ondisk->object_prefix), |
| 1022 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1023 | if (!object_prefix) |
| 1024 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1028 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1029 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1030 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1031 | if (!snapc) |
| 1032 | goto out_err; |
| 1033 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1034 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1035 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1036 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1037 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1038 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1039 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1040 | if (snap_names_len > (u64)SIZE_MAX) |
| 1041 | goto out_2big; |
| 1042 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1043 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1044 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1045 | |
| 1046 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1047 | snap_sizes = kmalloc_array(snap_count, |
| 1048 | sizeof(*header->snap_sizes), |
| 1049 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1050 | if (!snap_sizes) |
| 1051 | goto out_err; |
| 1052 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1053 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1054 | * Copy the names, and fill in each snapshot's id |
| 1055 | * and size. |
| 1056 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1057 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1058 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1059 | * snap_names_len bytes beyond the end of the |
| 1060 | * snapshot id array, this memcpy() is safe. |
| 1061 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1062 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1063 | snaps = ondisk->snaps; |
| 1064 | for (i = 0; i < snap_count; i++) { |
| 1065 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1066 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1067 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1068 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1069 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1070 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1071 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1072 | if (first_time) { |
| 1073 | header->object_prefix = object_prefix; |
| 1074 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1075 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1076 | } else { |
| 1077 | ceph_put_snap_context(header->snapc); |
| 1078 | kfree(header->snap_names); |
| 1079 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1083 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1084 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1085 | header->snapc = snapc; |
| 1086 | header->snap_names = snap_names; |
| 1087 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1088 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1089 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1090 | out_2big: |
| 1091 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1092 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1093 | kfree(snap_sizes); |
| 1094 | kfree(snap_names); |
| 1095 | ceph_put_snap_context(snapc); |
| 1096 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1097 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1098 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1101 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1102 | { |
| 1103 | const char *snap_name; |
| 1104 | |
| 1105 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1106 | |
| 1107 | /* Skip over names until we find the one we are looking for */ |
| 1108 | |
| 1109 | snap_name = rbd_dev->header.snap_names; |
| 1110 | while (which--) |
| 1111 | snap_name += strlen(snap_name) + 1; |
| 1112 | |
| 1113 | return kstrdup(snap_name, GFP_KERNEL); |
| 1114 | } |
| 1115 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1116 | /* |
| 1117 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1118 | * Note that result is for snapshots in *descending* order. |
| 1119 | */ |
| 1120 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1121 | { |
| 1122 | u64 snap_id1 = *(u64 *)s1; |
| 1123 | u64 snap_id2 = *(u64 *)s2; |
| 1124 | |
| 1125 | if (snap_id1 < snap_id2) |
| 1126 | return 1; |
| 1127 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Search a snapshot context to see if the given snapshot id is |
| 1132 | * present. |
| 1133 | * |
| 1134 | * Returns the position of the snapshot id in the array if it's found, |
| 1135 | * or BAD_SNAP_INDEX otherwise. |
| 1136 | * |
| 1137 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1138 | * reverse order, highest snapshot id first. |
| 1139 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1140 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1141 | { |
| 1142 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1143 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1144 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1145 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1146 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1147 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1148 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1151 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1152 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1153 | { |
| 1154 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1155 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1156 | |
| 1157 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1158 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1159 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1160 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1161 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1162 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1163 | } |
| 1164 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1165 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1166 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1167 | if (snap_id == CEPH_NOSNAP) |
| 1168 | return RBD_SNAP_HEAD_NAME; |
| 1169 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1170 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1171 | if (rbd_dev->image_format == 1) |
| 1172 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1173 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1174 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1175 | } |
| 1176 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1177 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1178 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1179 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1180 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1181 | if (snap_id == CEPH_NOSNAP) { |
| 1182 | *snap_size = rbd_dev->header.image_size; |
| 1183 | } else if (rbd_dev->image_format == 1) { |
| 1184 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1185 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1186 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1187 | if (which == BAD_SNAP_INDEX) |
| 1188 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1189 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1190 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1191 | } else { |
| 1192 | u64 size = 0; |
| 1193 | int ret; |
| 1194 | |
| 1195 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1196 | if (ret) |
| 1197 | return ret; |
| 1198 | |
| 1199 | *snap_size = size; |
| 1200 | } |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
| 1204 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1205 | u64 *snap_features) |
| 1206 | { |
| 1207 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1208 | if (snap_id == CEPH_NOSNAP) { |
| 1209 | *snap_features = rbd_dev->header.features; |
| 1210 | } else if (rbd_dev->image_format == 1) { |
| 1211 | *snap_features = 0; /* No features for format 1 */ |
| 1212 | } else { |
| 1213 | u64 features = 0; |
| 1214 | int ret; |
| 1215 | |
| 1216 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1217 | if (ret) |
| 1218 | return ret; |
| 1219 | |
| 1220 | *snap_features = features; |
| 1221 | } |
| 1222 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1225 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1226 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1227 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1228 | u64 size = 0; |
| 1229 | u64 features = 0; |
| 1230 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1231 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1232 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1233 | if (ret) |
| 1234 | return ret; |
| 1235 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1236 | if (ret) |
| 1237 | return ret; |
| 1238 | |
| 1239 | rbd_dev->mapping.size = size; |
| 1240 | rbd_dev->mapping.features = features; |
| 1241 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1242 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1245 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1246 | { |
| 1247 | rbd_dev->mapping.size = 0; |
| 1248 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1249 | } |
| 1250 | |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1251 | static void rbd_segment_name_free(const char *name) |
| 1252 | { |
| 1253 | /* The explicit cast here is needed to drop the const qualifier */ |
| 1254 | |
| 1255 | kmem_cache_free(rbd_segment_name_cache, (void *)name); |
| 1256 | } |
| 1257 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 1258 | static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1259 | { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1260 | char *name; |
| 1261 | u64 segment; |
| 1262 | int ret; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1263 | char *name_format; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1264 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 1265 | name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1266 | if (!name) |
| 1267 | return NULL; |
| 1268 | segment = offset >> rbd_dev->header.obj_order; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1269 | name_format = "%s.%012llx"; |
| 1270 | if (rbd_dev->image_format == 2) |
| 1271 | name_format = "%s.%016llx"; |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1272 | ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format, |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1273 | rbd_dev->header.object_prefix, segment); |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1274 | if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1275 | pr_err("error formatting segment name for #%llu (%d)\n", |
| 1276 | segment, ret); |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1277 | rbd_segment_name_free(name); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1278 | name = NULL; |
| 1279 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1280 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1281 | return name; |
| 1282 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1283 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1284 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1285 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1286 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1287 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1288 | return offset & (segment_size - 1); |
| 1289 | } |
| 1290 | |
| 1291 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1292 | u64 offset, u64 length) |
| 1293 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1294 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1295 | |
| 1296 | offset &= segment_size - 1; |
| 1297 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1298 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1299 | if (offset + length > segment_size) |
| 1300 | length = segment_size - offset; |
| 1301 | |
| 1302 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * bio helpers |
| 1307 | */ |
| 1308 | |
| 1309 | static void bio_chain_put(struct bio *chain) |
| 1310 | { |
| 1311 | struct bio *tmp; |
| 1312 | |
| 1313 | while (chain) { |
| 1314 | tmp = chain; |
| 1315 | chain = chain->bi_next; |
| 1316 | bio_put(tmp); |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * zeros a bio chain, starting at specific offset |
| 1322 | */ |
| 1323 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1324 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1325 | struct bio_vec bv; |
| 1326 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1327 | unsigned long flags; |
| 1328 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1329 | int pos = 0; |
| 1330 | |
| 1331 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1332 | bio_for_each_segment(bv, chain, iter) { |
| 1333 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1334 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1335 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1336 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1337 | bv.bv_len - remainder); |
| 1338 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1339 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1340 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1341 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | chain = chain->bi_next; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1349 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1350 | * starting at the given byte offset from the start of the array and |
| 1351 | * continuing up to the given end offset. The pages array is |
| 1352 | * assumed to be big enough to hold all bytes up to the end. |
| 1353 | */ |
| 1354 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1355 | { |
| 1356 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1357 | |
| 1358 | rbd_assert(end > offset); |
| 1359 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1360 | while (offset < end) { |
| 1361 | size_t page_offset; |
| 1362 | size_t length; |
| 1363 | unsigned long flags; |
| 1364 | void *kaddr; |
| 1365 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1366 | page_offset = offset & ~PAGE_MASK; |
| 1367 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1368 | local_irq_save(flags); |
| 1369 | kaddr = kmap_atomic(*page); |
| 1370 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1371 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1372 | kunmap_atomic(kaddr); |
| 1373 | local_irq_restore(flags); |
| 1374 | |
| 1375 | offset += length; |
| 1376 | page++; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1381 | * Clone a portion of a bio, starting at the given byte offset |
| 1382 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1383 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1384 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1385 | unsigned int offset, |
| 1386 | unsigned int len, |
| 1387 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1388 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1389 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1390 | |
Kent Overstreet | 5341a62 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1391 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1392 | if (!bio) |
| 1393 | return NULL; /* ENOMEM */ |
| 1394 | |
Kent Overstreet | 5341a62 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1395 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1396 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1397 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1398 | return bio; |
| 1399 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1400 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1401 | /* |
| 1402 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1403 | * into the first bio in the source chain and continuing for the |
| 1404 | * number of bytes indicated. The result is another bio chain of |
| 1405 | * exactly the given length, or a null pointer on error. |
| 1406 | * |
| 1407 | * The bio_src and offset parameters are both in-out. On entry they |
| 1408 | * refer to the first source bio and the offset into that bio where |
| 1409 | * the start of data to be cloned is located. |
| 1410 | * |
| 1411 | * On return, bio_src is updated to refer to the bio in the source |
| 1412 | * chain that contains first un-cloned byte, and *offset will |
| 1413 | * contain the offset of that byte within that bio. |
| 1414 | */ |
| 1415 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1416 | unsigned int *offset, |
| 1417 | unsigned int len, |
| 1418 | gfp_t gfpmask) |
| 1419 | { |
| 1420 | struct bio *bi = *bio_src; |
| 1421 | unsigned int off = *offset; |
| 1422 | struct bio *chain = NULL; |
| 1423 | struct bio **end; |
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 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1426 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1427 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1428 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1429 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1430 | end = &chain; |
| 1431 | while (len) { |
| 1432 | unsigned int bi_size; |
| 1433 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1434 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1435 | if (!bi) { |
| 1436 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1437 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1438 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1439 | 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] | 1440 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1441 | if (!bio) |
| 1442 | goto out_err; /* ENOMEM */ |
| 1443 | |
| 1444 | *end = bio; |
| 1445 | end = &bio->bi_next; |
| 1446 | |
| 1447 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1448 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1449 | bi = bi->bi_next; |
| 1450 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1451 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1452 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1453 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1454 | *bio_src = bi; |
| 1455 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1456 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1457 | return chain; |
| 1458 | out_err: |
| 1459 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1460 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1461 | return NULL; |
| 1462 | } |
| 1463 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1464 | /* |
| 1465 | * The default/initial value for all object request flags is 0. For |
| 1466 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1467 | * again. |
| 1468 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1469 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1470 | { |
| 1471 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1472 | struct rbd_device *rbd_dev; |
| 1473 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1474 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1475 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1476 | obj_request); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1481 | { |
| 1482 | smp_mb(); |
| 1483 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1484 | } |
| 1485 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1486 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1487 | { |
| 1488 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1489 | struct rbd_device *rbd_dev = NULL; |
| 1490 | |
| 1491 | if (obj_request_img_data_test(obj_request)) |
| 1492 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1493 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1494 | obj_request); |
| 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1499 | { |
| 1500 | smp_mb(); |
| 1501 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1502 | } |
| 1503 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1504 | /* |
| 1505 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1506 | * flag. The latter is set based on the "exists" value provided. |
| 1507 | * |
| 1508 | * Note that for our purposes once an object exists it never goes |
| 1509 | * away again. It's possible that the response from two existence |
| 1510 | * checks are separated by the creation of the target object, and |
| 1511 | * the first ("doesn't exist") response arrives *after* the second |
| 1512 | * ("does exist"). In that case we ignore the second one. |
| 1513 | */ |
| 1514 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1515 | bool exists) |
| 1516 | { |
| 1517 | if (exists) |
| 1518 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1519 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1520 | smp_mb(); |
| 1521 | } |
| 1522 | |
| 1523 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1524 | { |
| 1525 | smp_mb(); |
| 1526 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1527 | } |
| 1528 | |
| 1529 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1530 | { |
| 1531 | smp_mb(); |
| 1532 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1533 | } |
| 1534 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1535 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1536 | { |
| 1537 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1538 | |
| 1539 | return obj_request->img_offset < |
| 1540 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1541 | } |
| 1542 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1543 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1544 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1545 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1546 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1547 | kref_get(&obj_request->kref); |
| 1548 | } |
| 1549 | |
| 1550 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1551 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1552 | { |
| 1553 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1554 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1555 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1556 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1557 | } |
| 1558 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1559 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1560 | { |
| 1561 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1562 | atomic_read(&img_request->kref.refcount)); |
| 1563 | kref_get(&img_request->kref); |
| 1564 | } |
| 1565 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1566 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1567 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1568 | static void rbd_img_request_destroy(struct kref *kref); |
| 1569 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1570 | { |
| 1571 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1572 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1573 | atomic_read(&img_request->kref.refcount)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1574 | if (img_request_child_test(img_request)) |
| 1575 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1576 | else |
| 1577 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1581 | struct rbd_obj_request *obj_request) |
| 1582 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1583 | rbd_assert(obj_request->img_request == NULL); |
| 1584 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1585 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1586 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1587 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1588 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1589 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1590 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1591 | img_request->obj_request_count++; |
| 1592 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1593 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1594 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1598 | struct rbd_obj_request *obj_request) |
| 1599 | { |
| 1600 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1601 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1602 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1603 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1604 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1605 | rbd_assert(img_request->obj_request_count > 0); |
| 1606 | img_request->obj_request_count--; |
| 1607 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1608 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1609 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1610 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1611 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1612 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1613 | rbd_obj_request_put(obj_request); |
| 1614 | } |
| 1615 | |
| 1616 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1617 | { |
| 1618 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1619 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1620 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1621 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1622 | return true; |
| 1623 | default: |
| 1624 | return false; |
| 1625 | } |
| 1626 | } |
| 1627 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1628 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1629 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1630 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1631 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1632 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1633 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1634 | dout("%s %p \"%s\" %llu~%llu osd_req %p\n", __func__, |
| 1635 | obj_request, obj_request->object_name, obj_request->offset, |
| 1636 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1637 | if (obj_request_img_data_test(obj_request)) { |
| 1638 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1639 | rbd_img_request_get(obj_request->img_request); |
| 1640 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1641 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1645 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1646 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1647 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1648 | |
| 1649 | /* |
| 1650 | * If no error occurred, compute the aggregate transfer |
| 1651 | * count for the image request. We could instead use |
| 1652 | * atomic64_cmpxchg() to update it as each object request |
| 1653 | * completes; not clear which way is better off hand. |
| 1654 | */ |
| 1655 | if (!img_request->result) { |
| 1656 | struct rbd_obj_request *obj_request; |
| 1657 | u64 xferred = 0; |
| 1658 | |
| 1659 | for_each_obj_request(img_request, obj_request) |
| 1660 | xferred += obj_request->xferred; |
| 1661 | img_request->xferred = xferred; |
| 1662 | } |
| 1663 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1664 | if (img_request->callback) |
| 1665 | img_request->callback(img_request); |
| 1666 | else |
| 1667 | rbd_img_request_put(img_request); |
| 1668 | } |
| 1669 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1670 | /* |
| 1671 | * The default/initial value for all image request flags is 0. Each |
| 1672 | * is conditionally set to 1 at image request initialization time |
| 1673 | * and currently never change thereafter. |
| 1674 | */ |
| 1675 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1676 | { |
| 1677 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1678 | smp_mb(); |
| 1679 | } |
| 1680 | |
| 1681 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1682 | { |
| 1683 | smp_mb(); |
| 1684 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1685 | } |
| 1686 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1687 | /* |
| 1688 | * Set the discard flag when the img_request is an discard request |
| 1689 | */ |
| 1690 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1691 | { |
| 1692 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1693 | smp_mb(); |
| 1694 | } |
| 1695 | |
| 1696 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1697 | { |
| 1698 | smp_mb(); |
| 1699 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1700 | } |
| 1701 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1702 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1703 | { |
| 1704 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1705 | smp_mb(); |
| 1706 | } |
| 1707 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1708 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1709 | { |
| 1710 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1711 | smp_mb(); |
| 1712 | } |
| 1713 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1714 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1715 | { |
| 1716 | smp_mb(); |
| 1717 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1718 | } |
| 1719 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1720 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1721 | { |
| 1722 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1723 | smp_mb(); |
| 1724 | } |
| 1725 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1726 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1727 | { |
| 1728 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1729 | smp_mb(); |
| 1730 | } |
| 1731 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1732 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1733 | { |
| 1734 | smp_mb(); |
| 1735 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1736 | } |
| 1737 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1738 | static enum obj_operation_type |
| 1739 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1740 | { |
| 1741 | if (img_request_write_test(img_request)) |
| 1742 | return OBJ_OP_WRITE; |
| 1743 | else if (img_request_discard_test(img_request)) |
| 1744 | return OBJ_OP_DISCARD; |
| 1745 | else |
| 1746 | return OBJ_OP_READ; |
| 1747 | } |
| 1748 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1749 | static void |
| 1750 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1751 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1752 | u64 xferred = obj_request->xferred; |
| 1753 | u64 length = obj_request->length; |
| 1754 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1755 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1756 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1757 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1758 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1759 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1760 | * length of the request. A short read also implies zero-fill |
| 1761 | * to the end of the request. An error requires the whole |
| 1762 | * length of the request to be reported finished with an error |
| 1763 | * to the block layer. In each case we update the xferred |
| 1764 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1765 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1766 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1767 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1768 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1769 | zero_bio_chain(obj_request->bio_list, 0); |
| 1770 | else |
| 1771 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1772 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1773 | } else if (xferred < length && !obj_request->result) { |
| 1774 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1775 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1776 | else |
| 1777 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1778 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1779 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1780 | obj_request_done_set(obj_request); |
| 1781 | } |
| 1782 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1783 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1784 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1785 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1786 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1787 | if (obj_request->callback) |
| 1788 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1789 | else |
| 1790 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1791 | } |
| 1792 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1793 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1794 | { |
| 1795 | obj_request->result = err; |
| 1796 | obj_request->xferred = 0; |
| 1797 | /* |
| 1798 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1799 | * rbd_img_obj_callback() |
| 1800 | */ |
| 1801 | if (obj_request_img_data_test(obj_request)) { |
| 1802 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1803 | rbd_img_request_get(obj_request->img_request); |
| 1804 | } |
| 1805 | obj_request_done_set(obj_request); |
| 1806 | rbd_obj_request_complete(obj_request); |
| 1807 | } |
| 1808 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1809 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1810 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1811 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1812 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1813 | bool layered = false; |
| 1814 | |
| 1815 | if (obj_request_img_data_test(obj_request)) { |
| 1816 | img_request = obj_request->img_request; |
| 1817 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1818 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1819 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1820 | |
| 1821 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1822 | obj_request, img_request, obj_request->result, |
| 1823 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1824 | if (layered && obj_request->result == -ENOENT && |
| 1825 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1826 | rbd_img_parent_read(obj_request); |
| 1827 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1828 | rbd_img_obj_request_read_callback(obj_request); |
| 1829 | else |
| 1830 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1831 | } |
| 1832 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1833 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1834 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1835 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1836 | obj_request->result, obj_request->length); |
| 1837 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1838 | * There is no such thing as a successful short write. Set |
| 1839 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1840 | */ |
| 1841 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1842 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1843 | } |
| 1844 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1845 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1846 | { |
| 1847 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1848 | obj_request->result, obj_request->length); |
| 1849 | /* |
| 1850 | * There is no such thing as a successful short discard. Set |
| 1851 | * it to our originally-requested length. |
| 1852 | */ |
| 1853 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1854 | /* discarding a non-existent object is not a problem */ |
| 1855 | if (obj_request->result == -ENOENT) |
| 1856 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1857 | obj_request_done_set(obj_request); |
| 1858 | } |
| 1859 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1860 | /* |
| 1861 | * For a simple stat call there's nothing to do. We'll do more if |
| 1862 | * this is part of a write sequence for a layered image. |
| 1863 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1864 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1865 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1866 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1867 | obj_request_done_set(obj_request); |
| 1868 | } |
| 1869 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1870 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1871 | { |
| 1872 | dout("%s: obj %p\n", __func__, obj_request); |
| 1873 | |
| 1874 | if (obj_request_img_data_test(obj_request)) |
| 1875 | rbd_osd_copyup_callback(obj_request); |
| 1876 | else |
| 1877 | obj_request_done_set(obj_request); |
| 1878 | } |
| 1879 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1880 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1881 | { |
| 1882 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1883 | u16 opcode; |
| 1884 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1885 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1886 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1887 | if (obj_request_img_data_test(obj_request)) { |
| 1888 | rbd_assert(obj_request->img_request); |
| 1889 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1890 | } else { |
| 1891 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1892 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1893 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1894 | if (osd_req->r_result < 0) |
| 1895 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1896 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1897 | /* |
| 1898 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1899 | * passed to the block layer, which just supports a 32-bit |
| 1900 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1901 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1902 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1903 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1904 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1905 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1906 | switch (opcode) { |
| 1907 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1908 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1909 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1910 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1911 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1912 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1913 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1914 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1915 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1916 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1917 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1918 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1919 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1920 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1921 | case CEPH_OSD_OP_DELETE: |
| 1922 | case CEPH_OSD_OP_TRUNCATE: |
| 1923 | case CEPH_OSD_OP_ZERO: |
| 1924 | rbd_osd_discard_callback(obj_request); |
| 1925 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1926 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1927 | rbd_osd_call_callback(obj_request); |
| 1928 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1929 | default: |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1930 | rbd_warn(NULL, "%s: unsupported op %hu", |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1931 | obj_request->object_name, (unsigned short) opcode); |
| 1932 | break; |
| 1933 | } |
| 1934 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1935 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1936 | rbd_obj_request_complete(obj_request); |
| 1937 | } |
| 1938 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1939 | 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] | 1940 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1941 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1942 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1943 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1944 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1948 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1949 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1950 | |
Ilya Dryomov | bb873b539 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1951 | osd_req->r_mtime = CURRENT_TIME; |
| 1952 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1953 | } |
| 1954 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 1955 | static struct ceph_osd_request * |
| 1956 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1957 | struct ceph_snap_context *snapc, |
| 1958 | int num_ops, unsigned int flags, |
| 1959 | struct rbd_obj_request *obj_request) |
| 1960 | { |
| 1961 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1962 | struct ceph_osd_request *req; |
| 1963 | |
| 1964 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1965 | if (!req) |
| 1966 | return NULL; |
| 1967 | |
| 1968 | req->r_flags = flags; |
| 1969 | req->r_callback = rbd_osd_req_callback; |
| 1970 | req->r_priv = obj_request; |
| 1971 | |
| 1972 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
| 1973 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, "%s", |
| 1974 | obj_request->object_name)) |
| 1975 | goto err_req; |
| 1976 | |
| 1977 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1978 | goto err_req; |
| 1979 | |
| 1980 | return req; |
| 1981 | |
| 1982 | err_req: |
| 1983 | ceph_osdc_put_request(req); |
| 1984 | return NULL; |
| 1985 | } |
| 1986 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1987 | /* |
| 1988 | * Create an osd request. A read request has one osd op (read). |
| 1989 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1990 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1991 | * technically osd watch is a write request, hence this distinction.) |
| 1992 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1993 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1994 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1995 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1996 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1997 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1998 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1999 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2000 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2001 | if (obj_request_img_data_test(obj_request) && |
| 2002 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2003 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2004 | if (op_type == OBJ_OP_WRITE) { |
| 2005 | rbd_assert(img_request_write_test(img_request)); |
| 2006 | } else { |
| 2007 | rbd_assert(img_request_discard_test(img_request)); |
| 2008 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2009 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2010 | } |
| 2011 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2012 | 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] | 2013 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 2014 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 2015 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
| 2016 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK : CEPH_OSD_FLAG_READ, |
| 2017 | obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2018 | } |
| 2019 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2020 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2021 | * Create a copyup osd request based on the information in the object |
| 2022 | * request supplied. A copyup request has two or three osd ops, a |
| 2023 | * copyup method call, potentially a hint op, and a write or truncate |
| 2024 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2025 | */ |
| 2026 | static struct ceph_osd_request * |
| 2027 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 2028 | { |
| 2029 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2030 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2031 | |
| 2032 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2033 | img_request = obj_request->img_request; |
| 2034 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2035 | rbd_assert(img_request_write_test(img_request) || |
| 2036 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2037 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2038 | if (img_request_discard_test(img_request)) |
| 2039 | num_osd_ops = 2; |
| 2040 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 2041 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 2042 | img_request->snapc, num_osd_ops, |
| 2043 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 2044 | obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2045 | } |
| 2046 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2047 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2048 | { |
| 2049 | ceph_osdc_put_request(osd_req); |
| 2050 | } |
| 2051 | |
| 2052 | /* object_name is assumed to be a non-null pointer and NUL-terminated */ |
| 2053 | |
| 2054 | static struct rbd_obj_request *rbd_obj_request_create(const char *object_name, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2055 | enum obj_request_type type) |
| 2056 | { |
| 2057 | struct rbd_obj_request *obj_request; |
| 2058 | size_t size; |
| 2059 | char *name; |
| 2060 | |
| 2061 | rbd_assert(obj_request_type_valid(type)); |
| 2062 | |
| 2063 | size = strlen(object_name) + 1; |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2064 | name = kmalloc(size, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2065 | if (!name) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2066 | return NULL; |
| 2067 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2068 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2069 | if (!obj_request) { |
| 2070 | kfree(name); |
| 2071 | return NULL; |
| 2072 | } |
| 2073 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2074 | obj_request->object_name = memcpy(name, object_name, size); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2075 | obj_request->which = BAD_WHICH; |
| 2076 | obj_request->type = type; |
| 2077 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2078 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2079 | kref_init(&obj_request->kref); |
| 2080 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2081 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2082 | return obj_request; |
| 2083 | } |
| 2084 | |
| 2085 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2086 | { |
| 2087 | struct rbd_obj_request *obj_request; |
| 2088 | |
| 2089 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2090 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2091 | dout("%s: obj %p\n", __func__, obj_request); |
| 2092 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2093 | rbd_assert(obj_request->img_request == NULL); |
| 2094 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2095 | |
| 2096 | if (obj_request->osd_req) |
| 2097 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2098 | |
| 2099 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2100 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2101 | case OBJ_REQUEST_NODATA: |
| 2102 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2103 | case OBJ_REQUEST_BIO: |
| 2104 | if (obj_request->bio_list) |
| 2105 | bio_chain_put(obj_request->bio_list); |
| 2106 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2107 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2108 | /* img_data requests don't own their page array */ |
| 2109 | if (obj_request->pages && |
| 2110 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2111 | ceph_release_page_vector(obj_request->pages, |
| 2112 | obj_request->page_count); |
| 2113 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2114 | } |
| 2115 | |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2116 | kfree(obj_request->object_name); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2117 | obj_request->object_name = NULL; |
| 2118 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2119 | } |
| 2120 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2121 | /* It's OK to call this for a device with no parent */ |
| 2122 | |
| 2123 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2124 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2125 | { |
| 2126 | rbd_dev_remove_parent(rbd_dev); |
| 2127 | rbd_spec_put(rbd_dev->parent_spec); |
| 2128 | rbd_dev->parent_spec = NULL; |
| 2129 | rbd_dev->parent_overlap = 0; |
| 2130 | } |
| 2131 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2132 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2133 | * Parent image reference counting is used to determine when an |
| 2134 | * image's parent fields can be safely torn down--after there are no |
| 2135 | * more in-flight requests to the parent image. When the last |
| 2136 | * reference is dropped, cleaning them up is safe. |
| 2137 | */ |
| 2138 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2139 | { |
| 2140 | int counter; |
| 2141 | |
| 2142 | if (!rbd_dev->parent_spec) |
| 2143 | return; |
| 2144 | |
| 2145 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2146 | if (counter > 0) |
| 2147 | return; |
| 2148 | |
| 2149 | /* Last reference; clean up parent data structures */ |
| 2150 | |
| 2151 | if (!counter) |
| 2152 | rbd_dev_unparent(rbd_dev); |
| 2153 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2154 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | /* |
| 2158 | * If an image has a non-zero parent overlap, get a reference to its |
| 2159 | * parent. |
| 2160 | * |
| 2161 | * Returns true if the rbd device has a parent with a non-zero |
| 2162 | * overlap and a reference for it was successfully taken, or |
| 2163 | * false otherwise. |
| 2164 | */ |
| 2165 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2166 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2167 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2168 | |
| 2169 | if (!rbd_dev->parent_spec) |
| 2170 | return false; |
| 2171 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2172 | down_read(&rbd_dev->header_rwsem); |
| 2173 | if (rbd_dev->parent_overlap) |
| 2174 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2175 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2176 | |
| 2177 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2178 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2179 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2180 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2181 | } |
| 2182 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2183 | /* |
| 2184 | * Caller is responsible for filling in the list of object requests |
| 2185 | * that comprises the image request, and the Linux request pointer |
| 2186 | * (if there is one). |
| 2187 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2188 | static struct rbd_img_request *rbd_img_request_create( |
| 2189 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2190 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2191 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2192 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2193 | { |
| 2194 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2195 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2196 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2197 | if (!img_request) |
| 2198 | return NULL; |
| 2199 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2200 | img_request->rq = NULL; |
| 2201 | img_request->rbd_dev = rbd_dev; |
| 2202 | img_request->offset = offset; |
| 2203 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2204 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2205 | if (op_type == OBJ_OP_DISCARD) { |
| 2206 | img_request_discard_set(img_request); |
| 2207 | img_request->snapc = snapc; |
| 2208 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2209 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2210 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2211 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2212 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2213 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2214 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2215 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2216 | spin_lock_init(&img_request->completion_lock); |
| 2217 | img_request->next_completion = 0; |
| 2218 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2219 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2220 | img_request->obj_request_count = 0; |
| 2221 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2222 | kref_init(&img_request->kref); |
| 2223 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2224 | 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] | 2225 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2226 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2227 | return img_request; |
| 2228 | } |
| 2229 | |
| 2230 | static void rbd_img_request_destroy(struct kref *kref) |
| 2231 | { |
| 2232 | struct rbd_img_request *img_request; |
| 2233 | struct rbd_obj_request *obj_request; |
| 2234 | struct rbd_obj_request *next_obj_request; |
| 2235 | |
| 2236 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2237 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2238 | dout("%s: img %p\n", __func__, img_request); |
| 2239 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2240 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2241 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2242 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2243 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2244 | if (img_request_layered_test(img_request)) { |
| 2245 | img_request_layered_clear(img_request); |
| 2246 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2247 | } |
| 2248 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2249 | if (img_request_write_test(img_request) || |
| 2250 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2251 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2252 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2253 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2254 | } |
| 2255 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2256 | static struct rbd_img_request *rbd_parent_request_create( |
| 2257 | struct rbd_obj_request *obj_request, |
| 2258 | u64 img_offset, u64 length) |
| 2259 | { |
| 2260 | struct rbd_img_request *parent_request; |
| 2261 | struct rbd_device *rbd_dev; |
| 2262 | |
| 2263 | rbd_assert(obj_request->img_request); |
| 2264 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2265 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2266 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2267 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2268 | if (!parent_request) |
| 2269 | return NULL; |
| 2270 | |
| 2271 | img_request_child_set(parent_request); |
| 2272 | rbd_obj_request_get(obj_request); |
| 2273 | parent_request->obj_request = obj_request; |
| 2274 | |
| 2275 | return parent_request; |
| 2276 | } |
| 2277 | |
| 2278 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2279 | { |
| 2280 | struct rbd_img_request *parent_request; |
| 2281 | struct rbd_obj_request *orig_request; |
| 2282 | |
| 2283 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2284 | orig_request = parent_request->obj_request; |
| 2285 | |
| 2286 | parent_request->obj_request = NULL; |
| 2287 | rbd_obj_request_put(orig_request); |
| 2288 | img_request_child_clear(parent_request); |
| 2289 | |
| 2290 | rbd_img_request_destroy(kref); |
| 2291 | } |
| 2292 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2293 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2294 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2295 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2296 | unsigned int xferred; |
| 2297 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2298 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2299 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2300 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2301 | img_request = obj_request->img_request; |
| 2302 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2303 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2304 | xferred = (unsigned int)obj_request->xferred; |
| 2305 | result = obj_request->result; |
| 2306 | if (result) { |
| 2307 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2308 | enum obj_operation_type op_type; |
| 2309 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2310 | if (img_request_discard_test(img_request)) |
| 2311 | op_type = OBJ_OP_DISCARD; |
| 2312 | else if (img_request_write_test(img_request)) |
| 2313 | op_type = OBJ_OP_WRITE; |
| 2314 | else |
| 2315 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2316 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2317 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2318 | obj_op_name(op_type), obj_request->length, |
| 2319 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2320 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2321 | result, xferred); |
| 2322 | if (!img_request->result) |
| 2323 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2324 | /* |
| 2325 | * Need to end I/O on the entire obj_request worth of |
| 2326 | * bytes in case of error. |
| 2327 | */ |
| 2328 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2329 | } |
| 2330 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2331 | if (img_request_child_test(img_request)) { |
| 2332 | rbd_assert(img_request->obj_request != NULL); |
| 2333 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2334 | } else { |
| 2335 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2336 | |
| 2337 | more = blk_update_request(img_request->rq, result, xferred); |
| 2338 | if (!more) |
| 2339 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2343 | } |
| 2344 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2345 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2346 | { |
| 2347 | struct rbd_img_request *img_request; |
| 2348 | u32 which = obj_request->which; |
| 2349 | bool more = true; |
| 2350 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2351 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2352 | img_request = obj_request->img_request; |
| 2353 | |
| 2354 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2355 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2356 | rbd_assert(img_request->obj_request_count > 0); |
| 2357 | rbd_assert(which != BAD_WHICH); |
| 2358 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2359 | |
| 2360 | spin_lock_irq(&img_request->completion_lock); |
| 2361 | if (which != img_request->next_completion) |
| 2362 | goto out; |
| 2363 | |
| 2364 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2365 | rbd_assert(more); |
| 2366 | rbd_assert(which < img_request->obj_request_count); |
| 2367 | |
| 2368 | if (!obj_request_done_test(obj_request)) |
| 2369 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2370 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2371 | which++; |
| 2372 | } |
| 2373 | |
| 2374 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2375 | img_request->next_completion = which; |
| 2376 | out: |
| 2377 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2378 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2379 | |
| 2380 | if (!more) |
| 2381 | rbd_img_request_complete(img_request); |
| 2382 | } |
| 2383 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2384 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2385 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2386 | * them for submission. num_ops is the current number of |
| 2387 | * osd operations already to the object request. |
| 2388 | */ |
| 2389 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2390 | struct ceph_osd_request *osd_request, |
| 2391 | enum obj_operation_type op_type, |
| 2392 | unsigned int num_ops) |
| 2393 | { |
| 2394 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2395 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2396 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2397 | u64 offset = obj_request->offset; |
| 2398 | u64 length = obj_request->length; |
| 2399 | u64 img_end; |
| 2400 | u16 opcode; |
| 2401 | |
| 2402 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2403 | if (!offset && length == object_size && |
| 2404 | (!img_request_layered_test(img_request) || |
| 2405 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2406 | opcode = CEPH_OSD_OP_DELETE; |
| 2407 | } else if ((offset + length == object_size)) { |
| 2408 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2409 | } else { |
| 2410 | down_read(&rbd_dev->header_rwsem); |
| 2411 | img_end = rbd_dev->header.image_size; |
| 2412 | up_read(&rbd_dev->header_rwsem); |
| 2413 | |
| 2414 | if (obj_request->img_offset + length == img_end) |
| 2415 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2416 | else |
| 2417 | opcode = CEPH_OSD_OP_ZERO; |
| 2418 | } |
| 2419 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2420 | if (!offset && length == object_size) |
| 2421 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2422 | else |
| 2423 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2424 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2425 | object_size, object_size); |
| 2426 | num_ops++; |
| 2427 | } else { |
| 2428 | opcode = CEPH_OSD_OP_READ; |
| 2429 | } |
| 2430 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2431 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2432 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2433 | else |
| 2434 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2435 | offset, length, 0, 0); |
| 2436 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2437 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2438 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2439 | obj_request->bio_list, length); |
| 2440 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2441 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2442 | obj_request->pages, length, |
| 2443 | offset & ~PAGE_MASK, false, false); |
| 2444 | |
| 2445 | /* Discards are also writes */ |
| 2446 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2447 | rbd_osd_req_format_write(obj_request); |
| 2448 | else |
| 2449 | rbd_osd_req_format_read(obj_request); |
| 2450 | } |
| 2451 | |
| 2452 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2453 | * Split up an image request into one or more object requests, each |
| 2454 | * to a different object. The "type" parameter indicates whether |
| 2455 | * "data_desc" is the pointer to the head of a list of bio |
| 2456 | * structures, or the base of a page array. In either case this |
| 2457 | * function assumes data_desc describes memory sufficient to hold |
| 2458 | * all data described by the image request. |
| 2459 | */ |
| 2460 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2461 | enum obj_request_type type, |
| 2462 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2463 | { |
| 2464 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2465 | struct rbd_obj_request *obj_request = NULL; |
| 2466 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2467 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2468 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2469 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2470 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2471 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2472 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2473 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2474 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2475 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2476 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2477 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2478 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2479 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2480 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2481 | |
| 2482 | if (type == OBJ_REQUEST_BIO) { |
| 2483 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2484 | rbd_assert(img_offset == |
| 2485 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2486 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2487 | pages = data_desc; |
| 2488 | } |
| 2489 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2490 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2491 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2492 | const char *object_name; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2493 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2494 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2495 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2496 | object_name = rbd_segment_name(rbd_dev, img_offset); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2497 | if (!object_name) |
| 2498 | goto out_unwind; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2499 | obj_request = rbd_obj_request_create(object_name, type); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 2500 | /* object request has its own copy of the object name */ |
| 2501 | rbd_segment_name_free(object_name); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2502 | if (!obj_request) |
| 2503 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2504 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2505 | obj_request->offset = offset; |
| 2506 | obj_request->length = length; |
| 2507 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2508 | /* |
| 2509 | * set obj_request->img_request before creating the |
| 2510 | * osd_request so that it gets the right snapc |
| 2511 | */ |
| 2512 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2513 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2514 | if (type == OBJ_REQUEST_BIO) { |
| 2515 | unsigned int clone_size; |
| 2516 | |
| 2517 | rbd_assert(length <= (u64)UINT_MAX); |
| 2518 | clone_size = (unsigned int)length; |
| 2519 | obj_request->bio_list = |
| 2520 | bio_chain_clone_range(&bio_list, |
| 2521 | &bio_offset, |
| 2522 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2523 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2524 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2525 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2526 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2527 | unsigned int page_count; |
| 2528 | |
| 2529 | obj_request->pages = pages; |
| 2530 | page_count = (u32)calc_pages_for(offset, length); |
| 2531 | obj_request->page_count = page_count; |
| 2532 | if ((offset + length) & ~PAGE_MASK) |
| 2533 | page_count--; /* more on last page */ |
| 2534 | pages += page_count; |
| 2535 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2536 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2537 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2538 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2539 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2540 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2541 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2542 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2543 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2544 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2545 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2546 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2547 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2548 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2549 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2550 | resid -= length; |
| 2551 | } |
| 2552 | |
| 2553 | return 0; |
| 2554 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2555 | out_unwind: |
| 2556 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2557 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2558 | |
| 2559 | return -ENOMEM; |
| 2560 | } |
| 2561 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2562 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2563 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2564 | { |
| 2565 | struct rbd_img_request *img_request; |
| 2566 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2567 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2568 | u32 page_count; |
| 2569 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2570 | dout("%s: obj %p\n", __func__, obj_request); |
| 2571 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2572 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2573 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2574 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2575 | img_request = obj_request->img_request; |
| 2576 | rbd_assert(img_request); |
| 2577 | |
| 2578 | rbd_dev = img_request->rbd_dev; |
| 2579 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2580 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2581 | pages = obj_request->copyup_pages; |
| 2582 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2583 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2584 | page_count = obj_request->copyup_page_count; |
| 2585 | rbd_assert(page_count); |
| 2586 | obj_request->copyup_page_count = 0; |
| 2587 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2588 | |
| 2589 | /* |
| 2590 | * We want the transfer count to reflect the size of the |
| 2591 | * original write request. There is no such thing as a |
| 2592 | * successful short write, so if the request was successful |
| 2593 | * we can just set it to the originally-requested length. |
| 2594 | */ |
| 2595 | if (!obj_request->result) |
| 2596 | obj_request->xferred = obj_request->length; |
| 2597 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2598 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2602 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2603 | { |
| 2604 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2605 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2606 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2607 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2608 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2609 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2610 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2611 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2612 | |
| 2613 | rbd_assert(img_request_child_test(img_request)); |
| 2614 | |
| 2615 | /* First get what we need from the image request */ |
| 2616 | |
| 2617 | pages = img_request->copyup_pages; |
| 2618 | rbd_assert(pages != NULL); |
| 2619 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2620 | page_count = img_request->copyup_page_count; |
| 2621 | rbd_assert(page_count); |
| 2622 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2623 | |
| 2624 | orig_request = img_request->obj_request; |
| 2625 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2626 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2627 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2628 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2629 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2630 | rbd_img_request_put(img_request); |
| 2631 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2632 | rbd_assert(orig_request->img_request); |
| 2633 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2634 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2635 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2636 | /* |
| 2637 | * If the overlap has become 0 (most likely because the |
| 2638 | * image has been flattened) we need to free the pages |
| 2639 | * and re-submit the original write request. |
| 2640 | */ |
| 2641 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2642 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2643 | rbd_obj_request_submit(orig_request); |
| 2644 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2648 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2649 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2650 | /* |
| 2651 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2652 | * 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] | 2653 | * request. Allocate the new copyup osd request for the |
| 2654 | * original request, and release the old one. |
| 2655 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2656 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2657 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2658 | if (!osd_req) |
| 2659 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2660 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2661 | orig_request->osd_req = osd_req; |
| 2662 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2663 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2664 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2665 | /* Initialize the copyup op */ |
| 2666 | |
| 2667 | 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] | 2668 | 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] | 2669 | false, false); |
| 2670 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2671 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2672 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2673 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2674 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2675 | |
| 2676 | /* All set, send it off. */ |
| 2677 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2678 | rbd_obj_request_submit(orig_request); |
| 2679 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2680 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2681 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2682 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2683 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | /* |
| 2687 | * Read from the parent image the range of data that covers the |
| 2688 | * entire target of the given object request. This is used for |
| 2689 | * satisfying a layered image write request when the target of an |
| 2690 | * object request from the image request does not exist. |
| 2691 | * |
| 2692 | * A page array big enough to hold the returned data is allocated |
| 2693 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2694 | * When the read completes, this page array will be transferred to |
| 2695 | * the original object request for the copyup operation. |
| 2696 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2697 | * If an error occurs, it is recorded as the result of the original |
| 2698 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2699 | */ |
| 2700 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2701 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2702 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2703 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2704 | u64 img_offset; |
| 2705 | u64 length; |
| 2706 | struct page **pages = NULL; |
| 2707 | u32 page_count; |
| 2708 | int result; |
| 2709 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2710 | rbd_assert(rbd_dev->parent != NULL); |
| 2711 | |
| 2712 | /* |
| 2713 | * Determine the byte range covered by the object in the |
| 2714 | * child image to which the original request was to be sent. |
| 2715 | */ |
| 2716 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2717 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2718 | |
| 2719 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2720 | * There is no defined parent data beyond the parent |
| 2721 | * overlap, so limit what we read at that boundary if |
| 2722 | * necessary. |
| 2723 | */ |
| 2724 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2725 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2726 | length = rbd_dev->parent_overlap - img_offset; |
| 2727 | } |
| 2728 | |
| 2729 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2730 | * Allocate a page array big enough to receive the data read |
| 2731 | * from the parent. |
| 2732 | */ |
| 2733 | page_count = (u32)calc_pages_for(0, length); |
| 2734 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2735 | if (IS_ERR(pages)) { |
| 2736 | result = PTR_ERR(pages); |
| 2737 | pages = NULL; |
| 2738 | goto out_err; |
| 2739 | } |
| 2740 | |
| 2741 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2742 | parent_request = rbd_parent_request_create(obj_request, |
| 2743 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2744 | if (!parent_request) |
| 2745 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2746 | |
| 2747 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2748 | if (result) |
| 2749 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2750 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2751 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2752 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2753 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2754 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2755 | result = rbd_img_request_submit(parent_request); |
| 2756 | if (!result) |
| 2757 | return 0; |
| 2758 | |
| 2759 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2760 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2761 | parent_request->obj_request = NULL; |
| 2762 | rbd_obj_request_put(obj_request); |
| 2763 | out_err: |
| 2764 | if (pages) |
| 2765 | ceph_release_page_vector(pages, page_count); |
| 2766 | if (parent_request) |
| 2767 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2768 | return result; |
| 2769 | } |
| 2770 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2771 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2772 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2773 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2774 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2775 | int result; |
| 2776 | |
| 2777 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2778 | |
| 2779 | /* |
| 2780 | * All we need from the object request is the original |
| 2781 | * request and the result of the STAT op. Grab those, then |
| 2782 | * we're done with the request. |
| 2783 | */ |
| 2784 | orig_request = obj_request->obj_request; |
| 2785 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2786 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2787 | rbd_assert(orig_request); |
| 2788 | rbd_assert(orig_request->img_request); |
| 2789 | |
| 2790 | result = obj_request->result; |
| 2791 | obj_request->result = 0; |
| 2792 | |
| 2793 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2794 | obj_request, orig_request, result, |
| 2795 | obj_request->xferred, obj_request->length); |
| 2796 | rbd_obj_request_put(obj_request); |
| 2797 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2798 | /* |
| 2799 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2800 | * image has been flattened) we need to re-submit the |
| 2801 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2802 | */ |
| 2803 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2804 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2805 | rbd_obj_request_submit(orig_request); |
| 2806 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2807 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2808 | |
| 2809 | /* |
| 2810 | * Our only purpose here is to determine whether the object |
| 2811 | * exists, and we don't want to treat the non-existence as |
| 2812 | * an error. If something else comes back, transfer the |
| 2813 | * error to the original request and complete it now. |
| 2814 | */ |
| 2815 | if (!result) { |
| 2816 | obj_request_existence_set(orig_request, true); |
| 2817 | } else if (result == -ENOENT) { |
| 2818 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2819 | } else { |
| 2820 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /* |
| 2824 | * Resubmit the original request now that we have recorded |
| 2825 | * whether the target object exists. |
| 2826 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2827 | result = rbd_img_obj_request_submit(orig_request); |
| 2828 | if (result) |
| 2829 | goto fail_orig_request; |
| 2830 | |
| 2831 | return; |
| 2832 | |
| 2833 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2834 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2838 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2839 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2840 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2841 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2842 | u32 page_count; |
| 2843 | size_t size; |
| 2844 | int ret; |
| 2845 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2846 | stat_request = rbd_obj_request_create(obj_request->object_name, |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2847 | OBJ_REQUEST_PAGES); |
| 2848 | if (!stat_request) |
| 2849 | return -ENOMEM; |
| 2850 | |
| 2851 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2852 | stat_request); |
| 2853 | if (!stat_request->osd_req) { |
| 2854 | ret = -ENOMEM; |
| 2855 | goto fail_stat_request; |
| 2856 | } |
| 2857 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2858 | /* |
| 2859 | * The response data for a STAT call consists of: |
| 2860 | * le64 length; |
| 2861 | * struct { |
| 2862 | * le32 tv_sec; |
| 2863 | * le32 tv_nsec; |
| 2864 | * } mtime; |
| 2865 | */ |
| 2866 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2867 | page_count = (u32)calc_pages_for(0, size); |
| 2868 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2869 | if (IS_ERR(pages)) { |
| 2870 | ret = PTR_ERR(pages); |
| 2871 | goto fail_stat_request; |
| 2872 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2873 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2874 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2875 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2876 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2877 | |
| 2878 | rbd_obj_request_get(obj_request); |
| 2879 | stat_request->obj_request = obj_request; |
| 2880 | stat_request->pages = pages; |
| 2881 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2882 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2883 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2884 | rbd_obj_request_submit(stat_request); |
| 2885 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2886 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2887 | fail_stat_request: |
| 2888 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2889 | return ret; |
| 2890 | } |
| 2891 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2892 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2893 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2894 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2895 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2896 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2897 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2898 | if (!img_request_write_test(img_request) && |
| 2899 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2900 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2901 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2902 | /* Non-layered writes */ |
| 2903 | if (!img_request_layered_test(img_request)) |
| 2904 | return true; |
| 2905 | |
| 2906 | /* |
| 2907 | * Layered writes outside of the parent overlap range don't |
| 2908 | * share any data with the parent. |
| 2909 | */ |
| 2910 | if (!obj_request_overlaps_parent(obj_request)) |
| 2911 | return true; |
| 2912 | |
| 2913 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2914 | * Entire-object layered writes - we will overwrite whatever |
| 2915 | * parent data there is anyway. |
| 2916 | */ |
| 2917 | if (!obj_request->offset && |
| 2918 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2919 | return true; |
| 2920 | |
| 2921 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2922 | * If the object is known to already exist, its parent data has |
| 2923 | * already been copied. |
| 2924 | */ |
| 2925 | if (obj_request_known_test(obj_request) && |
| 2926 | obj_request_exists_test(obj_request)) |
| 2927 | return true; |
| 2928 | |
| 2929 | return false; |
| 2930 | } |
| 2931 | |
| 2932 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2933 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2934 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2935 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2936 | rbd_assert(obj_request->img_request); |
| 2937 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2938 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2939 | rbd_obj_request_submit(obj_request); |
| 2940 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2944 | * It's a layered write. The target object might exist but |
| 2945 | * we may not know that yet. If we know it doesn't exist, |
| 2946 | * start by reading the data for the full target object from |
| 2947 | * 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] | 2948 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2949 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2950 | return rbd_img_obj_parent_read_full(obj_request); |
| 2951 | |
| 2952 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2953 | |
| 2954 | return rbd_img_obj_exists_submit(obj_request); |
| 2955 | } |
| 2956 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2957 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2958 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2959 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2960 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2961 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2962 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2963 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2964 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2965 | rbd_img_request_get(img_request); |
| 2966 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2967 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2968 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2969 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2970 | } |
| 2971 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2972 | out_put_ireq: |
| 2973 | rbd_img_request_put(img_request); |
| 2974 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2975 | } |
| 2976 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2977 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2978 | { |
| 2979 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2980 | struct rbd_device *rbd_dev; |
| 2981 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2982 | u64 img_xferred; |
| 2983 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2984 | |
| 2985 | rbd_assert(img_request_child_test(img_request)); |
| 2986 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2987 | /* First get what we need from the image request and release it */ |
| 2988 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2989 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2990 | img_xferred = img_request->xferred; |
| 2991 | img_result = img_request->result; |
| 2992 | rbd_img_request_put(img_request); |
| 2993 | |
| 2994 | /* |
| 2995 | * If the overlap has become 0 (most likely because the |
| 2996 | * image has been flattened) we need to re-submit the |
| 2997 | * original request. |
| 2998 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2999 | rbd_assert(obj_request); |
| 3000 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3001 | rbd_dev = obj_request->img_request->rbd_dev; |
| 3002 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 3003 | rbd_obj_request_submit(obj_request); |
| 3004 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3008 | if (obj_request->result) |
| 3009 | goto out; |
| 3010 | |
| 3011 | /* |
| 3012 | * We need to zero anything beyond the parent overlap |
| 3013 | * boundary. Since rbd_img_obj_request_read_callback() |
| 3014 | * will zero anything beyond the end of a short read, an |
| 3015 | * easy way to do this is to pretend the data from the |
| 3016 | * parent came up short--ending at the overlap boundary. |
| 3017 | */ |
| 3018 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 3019 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3020 | if (obj_end > rbd_dev->parent_overlap) { |
| 3021 | u64 xferred = 0; |
| 3022 | |
| 3023 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 3024 | xferred = rbd_dev->parent_overlap - |
| 3025 | obj_request->img_offset; |
| 3026 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3027 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3028 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3029 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3030 | } |
| 3031 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3032 | rbd_img_obj_request_read_callback(obj_request); |
| 3033 | rbd_obj_request_complete(obj_request); |
| 3034 | } |
| 3035 | |
| 3036 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 3037 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3038 | struct rbd_img_request *img_request; |
| 3039 | int result; |
| 3040 | |
| 3041 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 3042 | rbd_assert(obj_request->img_request != NULL); |
| 3043 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3044 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3045 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3046 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3047 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3048 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3049 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3050 | result = -ENOMEM; |
| 3051 | if (!img_request) |
| 3052 | goto out_err; |
| 3053 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3054 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3055 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3056 | obj_request->bio_list); |
| 3057 | else |
| 3058 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3059 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3060 | if (result) |
| 3061 | goto out_err; |
| 3062 | |
| 3063 | img_request->callback = rbd_img_parent_read_callback; |
| 3064 | result = rbd_img_request_submit(img_request); |
| 3065 | if (result) |
| 3066 | goto out_err; |
| 3067 | |
| 3068 | return; |
| 3069 | out_err: |
| 3070 | if (img_request) |
| 3071 | rbd_img_request_put(img_request); |
| 3072 | obj_request->result = result; |
| 3073 | obj_request->xferred = 0; |
| 3074 | obj_request_done_set(obj_request); |
| 3075 | } |
| 3076 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3077 | static const struct rbd_client_id rbd_empty_cid; |
| 3078 | |
| 3079 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3080 | const struct rbd_client_id *rhs) |
| 3081 | { |
| 3082 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3083 | } |
| 3084 | |
| 3085 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3086 | { |
| 3087 | struct rbd_client_id cid; |
| 3088 | |
| 3089 | mutex_lock(&rbd_dev->watch_mutex); |
| 3090 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3091 | cid.handle = rbd_dev->watch_cookie; |
| 3092 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3093 | return cid; |
| 3094 | } |
| 3095 | |
| 3096 | /* |
| 3097 | * lock_rwsem must be held for write |
| 3098 | */ |
| 3099 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3100 | const struct rbd_client_id *cid) |
| 3101 | { |
| 3102 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3103 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3104 | cid->gid, cid->handle); |
| 3105 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3106 | } |
| 3107 | |
| 3108 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3109 | { |
| 3110 | mutex_lock(&rbd_dev->watch_mutex); |
| 3111 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3112 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3113 | } |
| 3114 | |
| 3115 | /* |
| 3116 | * lock_rwsem must be held for write |
| 3117 | */ |
| 3118 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3119 | { |
| 3120 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3121 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3122 | char cookie[32]; |
| 3123 | int ret; |
| 3124 | |
| 3125 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
| 3126 | |
| 3127 | format_lock_cookie(rbd_dev, cookie); |
| 3128 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3129 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3130 | RBD_LOCK_TAG, "", 0); |
| 3131 | if (ret) |
| 3132 | return ret; |
| 3133 | |
| 3134 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
| 3135 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3136 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3137 | return 0; |
| 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | * lock_rwsem must be held for write |
| 3142 | */ |
| 3143 | static int rbd_unlock(struct rbd_device *rbd_dev) |
| 3144 | { |
| 3145 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3146 | char cookie[32]; |
| 3147 | int ret; |
| 3148 | |
| 3149 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
| 3150 | |
| 3151 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 3152 | |
| 3153 | format_lock_cookie(rbd_dev, cookie); |
| 3154 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3155 | RBD_LOCK_NAME, cookie); |
| 3156 | if (ret && ret != -ENOENT) { |
| 3157 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); |
| 3158 | return ret; |
| 3159 | } |
| 3160 | |
| 3161 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3162 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
| 3163 | return 0; |
| 3164 | } |
| 3165 | |
| 3166 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3167 | enum rbd_notify_op notify_op, |
| 3168 | struct page ***preply_pages, |
| 3169 | size_t *preply_len) |
| 3170 | { |
| 3171 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3172 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3173 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3174 | char buf[buf_size]; |
| 3175 | void *p = buf; |
| 3176 | |
| 3177 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3178 | |
| 3179 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3180 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3181 | ceph_encode_32(&p, notify_op); |
| 3182 | ceph_encode_64(&p, cid.gid); |
| 3183 | ceph_encode_64(&p, cid.handle); |
| 3184 | |
| 3185 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3186 | &rbd_dev->header_oloc, buf, buf_size, |
| 3187 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3188 | } |
| 3189 | |
| 3190 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3191 | enum rbd_notify_op notify_op) |
| 3192 | { |
| 3193 | struct page **reply_pages; |
| 3194 | size_t reply_len; |
| 3195 | |
| 3196 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3197 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3198 | } |
| 3199 | |
| 3200 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3201 | { |
| 3202 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3203 | acquired_lock_work); |
| 3204 | |
| 3205 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3206 | } |
| 3207 | |
| 3208 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3209 | { |
| 3210 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3211 | released_lock_work); |
| 3212 | |
| 3213 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3214 | } |
| 3215 | |
| 3216 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3217 | { |
| 3218 | struct page **reply_pages; |
| 3219 | size_t reply_len; |
| 3220 | bool lock_owner_responded = false; |
| 3221 | int ret; |
| 3222 | |
| 3223 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3224 | |
| 3225 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3226 | &reply_pages, &reply_len); |
| 3227 | if (ret && ret != -ETIMEDOUT) { |
| 3228 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3229 | goto out; |
| 3230 | } |
| 3231 | |
| 3232 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3233 | void *p = page_address(reply_pages[0]); |
| 3234 | void *const end = p + reply_len; |
| 3235 | u32 n; |
| 3236 | |
| 3237 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3238 | while (n--) { |
| 3239 | u8 struct_v; |
| 3240 | u32 len; |
| 3241 | |
| 3242 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3243 | p += 8 + 8; /* skip gid and cookie */ |
| 3244 | |
| 3245 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3246 | if (!len) |
| 3247 | continue; |
| 3248 | |
| 3249 | if (lock_owner_responded) { |
| 3250 | rbd_warn(rbd_dev, |
| 3251 | "duplicate lock owners detected"); |
| 3252 | ret = -EIO; |
| 3253 | goto out; |
| 3254 | } |
| 3255 | |
| 3256 | lock_owner_responded = true; |
| 3257 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3258 | &struct_v, &len); |
| 3259 | if (ret) { |
| 3260 | rbd_warn(rbd_dev, |
| 3261 | "failed to decode ResponseMessage: %d", |
| 3262 | ret); |
| 3263 | goto e_inval; |
| 3264 | } |
| 3265 | |
| 3266 | ret = ceph_decode_32(&p); |
| 3267 | } |
| 3268 | } |
| 3269 | |
| 3270 | if (!lock_owner_responded) { |
| 3271 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3272 | ret = -ETIMEDOUT; |
| 3273 | } |
| 3274 | |
| 3275 | out: |
| 3276 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3277 | return ret; |
| 3278 | |
| 3279 | e_inval: |
| 3280 | ret = -EINVAL; |
| 3281 | goto out; |
| 3282 | } |
| 3283 | |
| 3284 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3285 | { |
| 3286 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3287 | |
| 3288 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3289 | if (wake_all) |
| 3290 | wake_up_all(&rbd_dev->lock_waitq); |
| 3291 | else |
| 3292 | wake_up(&rbd_dev->lock_waitq); |
| 3293 | } |
| 3294 | |
| 3295 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3296 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3297 | { |
| 3298 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3299 | u8 lock_type; |
| 3300 | char *lock_tag; |
| 3301 | int ret; |
| 3302 | |
| 3303 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3304 | |
| 3305 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3306 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3307 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3308 | if (ret) |
| 3309 | return ret; |
| 3310 | |
| 3311 | if (*num_lockers == 0) { |
| 3312 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3313 | goto out; |
| 3314 | } |
| 3315 | |
| 3316 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3317 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3318 | lock_tag); |
| 3319 | ret = -EBUSY; |
| 3320 | goto out; |
| 3321 | } |
| 3322 | |
| 3323 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3324 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3325 | ret = -EBUSY; |
| 3326 | goto out; |
| 3327 | } |
| 3328 | |
| 3329 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3330 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3331 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3332 | (*lockers)[0].id.cookie); |
| 3333 | ret = -EBUSY; |
| 3334 | goto out; |
| 3335 | } |
| 3336 | |
| 3337 | out: |
| 3338 | kfree(lock_tag); |
| 3339 | return ret; |
| 3340 | } |
| 3341 | |
| 3342 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3343 | const struct ceph_locker *locker) |
| 3344 | { |
| 3345 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3346 | struct ceph_watch_item *watchers; |
| 3347 | u32 num_watchers; |
| 3348 | u64 cookie; |
| 3349 | int i; |
| 3350 | int ret; |
| 3351 | |
| 3352 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3353 | &rbd_dev->header_oloc, &watchers, |
| 3354 | &num_watchers); |
| 3355 | if (ret) |
| 3356 | return ret; |
| 3357 | |
| 3358 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3359 | for (i = 0; i < num_watchers; i++) { |
| 3360 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3361 | sizeof(locker->info.addr)) && |
| 3362 | watchers[i].cookie == cookie) { |
| 3363 | struct rbd_client_id cid = { |
| 3364 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3365 | .handle = cookie, |
| 3366 | }; |
| 3367 | |
| 3368 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3369 | rbd_dev, cid.gid, cid.handle); |
| 3370 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3371 | ret = 1; |
| 3372 | goto out; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3377 | ret = 0; |
| 3378 | out: |
| 3379 | kfree(watchers); |
| 3380 | return ret; |
| 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * lock_rwsem must be held for write |
| 3385 | */ |
| 3386 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3387 | { |
| 3388 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3389 | struct ceph_locker *lockers; |
| 3390 | u32 num_lockers; |
| 3391 | int ret; |
| 3392 | |
| 3393 | for (;;) { |
| 3394 | ret = rbd_lock(rbd_dev); |
| 3395 | if (ret != -EBUSY) |
| 3396 | return ret; |
| 3397 | |
| 3398 | /* determine if the current lock holder is still alive */ |
| 3399 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3400 | if (ret) |
| 3401 | return ret; |
| 3402 | |
| 3403 | if (num_lockers == 0) |
| 3404 | goto again; |
| 3405 | |
| 3406 | ret = find_watcher(rbd_dev, lockers); |
| 3407 | if (ret) { |
| 3408 | if (ret > 0) |
| 3409 | ret = 0; /* have to request lock */ |
| 3410 | goto out; |
| 3411 | } |
| 3412 | |
| 3413 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3414 | ENTITY_NAME(lockers[0].id.name)); |
| 3415 | |
| 3416 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3417 | &lockers[0].info.addr); |
| 3418 | if (ret) { |
| 3419 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3420 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3421 | goto out; |
| 3422 | } |
| 3423 | |
| 3424 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3425 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3426 | lockers[0].id.cookie, |
| 3427 | &lockers[0].id.name); |
| 3428 | if (ret && ret != -ENOENT) |
| 3429 | goto out; |
| 3430 | |
| 3431 | again: |
| 3432 | ceph_free_lockers(lockers, num_lockers); |
| 3433 | } |
| 3434 | |
| 3435 | out: |
| 3436 | ceph_free_lockers(lockers, num_lockers); |
| 3437 | return ret; |
| 3438 | } |
| 3439 | |
| 3440 | /* |
| 3441 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3442 | */ |
| 3443 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3444 | int *pret) |
| 3445 | { |
| 3446 | enum rbd_lock_state lock_state; |
| 3447 | |
| 3448 | down_read(&rbd_dev->lock_rwsem); |
| 3449 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3450 | rbd_dev->lock_state); |
| 3451 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3452 | lock_state = rbd_dev->lock_state; |
| 3453 | up_read(&rbd_dev->lock_rwsem); |
| 3454 | return lock_state; |
| 3455 | } |
| 3456 | |
| 3457 | up_read(&rbd_dev->lock_rwsem); |
| 3458 | down_write(&rbd_dev->lock_rwsem); |
| 3459 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3460 | rbd_dev->lock_state); |
| 3461 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3462 | *pret = rbd_try_lock(rbd_dev); |
| 3463 | if (*pret) |
| 3464 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3465 | } |
| 3466 | |
| 3467 | lock_state = rbd_dev->lock_state; |
| 3468 | up_write(&rbd_dev->lock_rwsem); |
| 3469 | return lock_state; |
| 3470 | } |
| 3471 | |
| 3472 | static void rbd_acquire_lock(struct work_struct *work) |
| 3473 | { |
| 3474 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3475 | struct rbd_device, lock_dwork); |
| 3476 | enum rbd_lock_state lock_state; |
| 3477 | int ret; |
| 3478 | |
| 3479 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3480 | again: |
| 3481 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3482 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3483 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3484 | wake_requests(rbd_dev, true); |
| 3485 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3486 | rbd_dev, lock_state, ret); |
| 3487 | return; |
| 3488 | } |
| 3489 | |
| 3490 | ret = rbd_request_lock(rbd_dev); |
| 3491 | if (ret == -ETIMEDOUT) { |
| 3492 | goto again; /* treat this as a dead client */ |
| 3493 | } else if (ret < 0) { |
| 3494 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3495 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3496 | RBD_RETRY_DELAY); |
| 3497 | } else { |
| 3498 | /* |
| 3499 | * lock owner acked, but resend if we don't see them |
| 3500 | * release the lock |
| 3501 | */ |
| 3502 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3503 | rbd_dev); |
| 3504 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3505 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3506 | } |
| 3507 | } |
| 3508 | |
| 3509 | /* |
| 3510 | * lock_rwsem must be held for write |
| 3511 | */ |
| 3512 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3513 | { |
| 3514 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3515 | rbd_dev->lock_state); |
| 3516 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3517 | return false; |
| 3518 | |
| 3519 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3520 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3521 | /* |
| 3522 | * Ensure that all in-flight IO is flushed. |
| 3523 | * |
| 3524 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3525 | * may be shared with other devices. |
| 3526 | */ |
| 3527 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3528 | up_read(&rbd_dev->lock_rwsem); |
| 3529 | |
| 3530 | down_write(&rbd_dev->lock_rwsem); |
| 3531 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3532 | rbd_dev->lock_state); |
| 3533 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3534 | return false; |
| 3535 | |
| 3536 | if (!rbd_unlock(rbd_dev)) |
| 3537 | /* |
| 3538 | * Give others a chance to grab the lock - we would re-acquire |
| 3539 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3540 | * otherwise. We need to ack our own notifications, so this |
| 3541 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3542 | * after wake_requests() in rbd_handle_released_lock(). |
| 3543 | */ |
| 3544 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3545 | |
| 3546 | return true; |
| 3547 | } |
| 3548 | |
| 3549 | static void rbd_release_lock_work(struct work_struct *work) |
| 3550 | { |
| 3551 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3552 | unlock_work); |
| 3553 | |
| 3554 | down_write(&rbd_dev->lock_rwsem); |
| 3555 | rbd_release_lock(rbd_dev); |
| 3556 | up_write(&rbd_dev->lock_rwsem); |
| 3557 | } |
| 3558 | |
| 3559 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3560 | void **p) |
| 3561 | { |
| 3562 | struct rbd_client_id cid = { 0 }; |
| 3563 | |
| 3564 | if (struct_v >= 2) { |
| 3565 | cid.gid = ceph_decode_64(p); |
| 3566 | cid.handle = ceph_decode_64(p); |
| 3567 | } |
| 3568 | |
| 3569 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3570 | cid.handle); |
| 3571 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3572 | down_write(&rbd_dev->lock_rwsem); |
| 3573 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3574 | /* |
| 3575 | * we already know that the remote client is |
| 3576 | * the owner |
| 3577 | */ |
| 3578 | up_write(&rbd_dev->lock_rwsem); |
| 3579 | return; |
| 3580 | } |
| 3581 | |
| 3582 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3583 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3584 | } else { |
| 3585 | down_read(&rbd_dev->lock_rwsem); |
| 3586 | } |
| 3587 | |
| 3588 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3589 | wake_requests(rbd_dev, false); |
| 3590 | up_read(&rbd_dev->lock_rwsem); |
| 3591 | } |
| 3592 | |
| 3593 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3594 | void **p) |
| 3595 | { |
| 3596 | struct rbd_client_id cid = { 0 }; |
| 3597 | |
| 3598 | if (struct_v >= 2) { |
| 3599 | cid.gid = ceph_decode_64(p); |
| 3600 | cid.handle = ceph_decode_64(p); |
| 3601 | } |
| 3602 | |
| 3603 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3604 | cid.handle); |
| 3605 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3606 | down_write(&rbd_dev->lock_rwsem); |
| 3607 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3608 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3609 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3610 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3611 | up_write(&rbd_dev->lock_rwsem); |
| 3612 | return; |
| 3613 | } |
| 3614 | |
| 3615 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3616 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3617 | } else { |
| 3618 | down_read(&rbd_dev->lock_rwsem); |
| 3619 | } |
| 3620 | |
| 3621 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3622 | wake_requests(rbd_dev, false); |
| 3623 | up_read(&rbd_dev->lock_rwsem); |
| 3624 | } |
| 3625 | |
| 3626 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3627 | void **p) |
| 3628 | { |
| 3629 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3630 | struct rbd_client_id cid = { 0 }; |
| 3631 | bool need_to_send; |
| 3632 | |
| 3633 | if (struct_v >= 2) { |
| 3634 | cid.gid = ceph_decode_64(p); |
| 3635 | cid.handle = ceph_decode_64(p); |
| 3636 | } |
| 3637 | |
| 3638 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3639 | cid.handle); |
| 3640 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3641 | return false; |
| 3642 | |
| 3643 | down_read(&rbd_dev->lock_rwsem); |
| 3644 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3645 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3646 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3647 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3648 | rbd_dev); |
| 3649 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3650 | } |
| 3651 | } |
| 3652 | up_read(&rbd_dev->lock_rwsem); |
| 3653 | return need_to_send; |
| 3654 | } |
| 3655 | |
| 3656 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3657 | u64 notify_id, u64 cookie, s32 *result) |
| 3658 | { |
| 3659 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3660 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3661 | char buf[buf_size]; |
| 3662 | int ret; |
| 3663 | |
| 3664 | if (result) { |
| 3665 | void *p = buf; |
| 3666 | |
| 3667 | /* encode ResponseMessage */ |
| 3668 | ceph_start_encoding(&p, 1, 1, |
| 3669 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3670 | ceph_encode_32(&p, *result); |
| 3671 | } else { |
| 3672 | buf_size = 0; |
| 3673 | } |
| 3674 | |
| 3675 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3676 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3677 | buf, buf_size); |
| 3678 | if (ret) |
| 3679 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3680 | } |
| 3681 | |
| 3682 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3683 | u64 cookie) |
| 3684 | { |
| 3685 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3686 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3687 | } |
| 3688 | |
| 3689 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3690 | u64 notify_id, u64 cookie, s32 result) |
| 3691 | { |
| 3692 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3693 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3694 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3695 | |
| 3696 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3697 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3698 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3699 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3700 | void *p = data; |
| 3701 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3702 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3703 | u32 len; |
| 3704 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3705 | int ret; |
| 3706 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3707 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3708 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3709 | if (data_len) { |
| 3710 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3711 | &struct_v, &len); |
| 3712 | if (ret) { |
| 3713 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3714 | ret); |
| 3715 | return; |
| 3716 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3717 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3718 | notify_op = ceph_decode_32(&p); |
| 3719 | } else { |
| 3720 | /* legacy notification for header updates */ |
| 3721 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3722 | len = 0; |
| 3723 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3724 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3725 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3726 | switch (notify_op) { |
| 3727 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3728 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3729 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3730 | break; |
| 3731 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3732 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3733 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3734 | break; |
| 3735 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3736 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3737 | /* |
| 3738 | * send ResponseMessage(0) back so the client |
| 3739 | * can detect a missing owner |
| 3740 | */ |
| 3741 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3742 | cookie, 0); |
| 3743 | else |
| 3744 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3745 | break; |
| 3746 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3747 | ret = rbd_dev_refresh(rbd_dev); |
| 3748 | if (ret) |
| 3749 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3750 | |
| 3751 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3752 | break; |
| 3753 | default: |
| 3754 | if (rbd_is_lock_owner(rbd_dev)) |
| 3755 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3756 | cookie, -EOPNOTSUPP); |
| 3757 | else |
| 3758 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3759 | break; |
| 3760 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3761 | } |
| 3762 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3763 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3764 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3765 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3766 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3767 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3768 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3769 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3770 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3771 | down_write(&rbd_dev->lock_rwsem); |
| 3772 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3773 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3774 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3775 | mutex_lock(&rbd_dev->watch_mutex); |
| 3776 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3777 | __rbd_unregister_watch(rbd_dev); |
| 3778 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3779 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3780 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3781 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3782 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3786 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3787 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3788 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3789 | { |
| 3790 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3791 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3792 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3793 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3794 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3795 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3796 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3797 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3798 | rbd_watch_errcb, rbd_dev); |
| 3799 | if (IS_ERR(handle)) |
| 3800 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3801 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3802 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3803 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3804 | } |
| 3805 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3806 | /* |
| 3807 | * watch_mutex must be locked |
| 3808 | */ |
| 3809 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3810 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3811 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3812 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3813 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3814 | rbd_assert(rbd_dev->watch_handle); |
| 3815 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3816 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3817 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3818 | if (ret) |
| 3819 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3820 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3821 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3822 | } |
| 3823 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3824 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3825 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3826 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3827 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3828 | mutex_lock(&rbd_dev->watch_mutex); |
| 3829 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3830 | ret = __rbd_register_watch(rbd_dev); |
| 3831 | if (ret) |
| 3832 | goto out; |
| 3833 | |
| 3834 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3835 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3836 | |
| 3837 | out: |
| 3838 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3839 | return ret; |
| 3840 | } |
| 3841 | |
| 3842 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3843 | { |
| 3844 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3845 | |
| 3846 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3847 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3848 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3849 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3850 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3851 | } |
| 3852 | |
| 3853 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3854 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3855 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3856 | cancel_tasks_sync(rbd_dev); |
| 3857 | |
| 3858 | mutex_lock(&rbd_dev->watch_mutex); |
| 3859 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3860 | __rbd_unregister_watch(rbd_dev); |
| 3861 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3862 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3863 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3864 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3867 | static void rbd_reregister_watch(struct work_struct *work) |
| 3868 | { |
| 3869 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3870 | struct rbd_device, watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3871 | bool was_lock_owner = false; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3872 | bool need_to_wake = false; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3873 | int ret; |
| 3874 | |
| 3875 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3876 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3877 | down_write(&rbd_dev->lock_rwsem); |
| 3878 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3879 | was_lock_owner = rbd_release_lock(rbd_dev); |
| 3880 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3881 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3882 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3883 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3884 | goto out; |
| 3885 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3886 | |
| 3887 | ret = __rbd_register_watch(rbd_dev); |
| 3888 | if (ret) { |
| 3889 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3890 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3891 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3892 | need_to_wake = true; |
| 3893 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3894 | queue_delayed_work(rbd_dev->task_wq, |
| 3895 | &rbd_dev->watch_dwork, |
| 3896 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3897 | } |
| 3898 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3899 | goto out; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3900 | } |
| 3901 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3902 | need_to_wake = true; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3903 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3904 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3905 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3906 | |
| 3907 | ret = rbd_dev_refresh(rbd_dev); |
| 3908 | if (ret) |
| 3909 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
| 3910 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3911 | if (was_lock_owner) { |
| 3912 | ret = rbd_try_lock(rbd_dev); |
| 3913 | if (ret) |
| 3914 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", |
| 3915 | ret); |
| 3916 | } |
| 3917 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3918 | out: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3919 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3920 | if (need_to_wake) |
| 3921 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3922 | } |
| 3923 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3924 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3925 | * Synchronous osd object method call. Returns the number of bytes |
| 3926 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3927 | */ |
| 3928 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3929 | struct ceph_object_id *oid, |
| 3930 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3931 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3932 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3933 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3934 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3935 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3936 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3937 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3938 | struct page *req_page = NULL; |
| 3939 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3940 | int ret; |
| 3941 | |
| 3942 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3943 | * Method calls are ultimately read operations. The result |
| 3944 | * should placed into the inbound buffer provided. They |
| 3945 | * also supply outbound data--parameters for the object |
| 3946 | * method. Currently if this is present it will be a |
| 3947 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3948 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3949 | if (outbound) { |
| 3950 | if (outbound_size > PAGE_SIZE) |
| 3951 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3952 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3953 | req_page = alloc_page(GFP_KERNEL); |
| 3954 | if (!req_page) |
| 3955 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3956 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3957 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3958 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3959 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3960 | reply_page = alloc_page(GFP_KERNEL); |
| 3961 | if (!reply_page) { |
| 3962 | if (req_page) |
| 3963 | __free_page(req_page); |
| 3964 | return -ENOMEM; |
| 3965 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3966 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3967 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3968 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3969 | reply_page, &inbound_size); |
| 3970 | if (!ret) { |
| 3971 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3972 | ret = inbound_size; |
| 3973 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3974 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3975 | if (req_page) |
| 3976 | __free_page(req_page); |
| 3977 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3978 | return ret; |
| 3979 | } |
| 3980 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3981 | /* |
| 3982 | * lock_rwsem must be held for read |
| 3983 | */ |
| 3984 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3985 | { |
| 3986 | DEFINE_WAIT(wait); |
| 3987 | |
| 3988 | do { |
| 3989 | /* |
| 3990 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3991 | * and cancel_delayed_work() in wake_requests(). |
| 3992 | */ |
| 3993 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3994 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3995 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3996 | TASK_UNINTERRUPTIBLE); |
| 3997 | up_read(&rbd_dev->lock_rwsem); |
| 3998 | schedule(); |
| 3999 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4000 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4001 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 4002 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4003 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 4004 | } |
| 4005 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4006 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4007 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4008 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 4009 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4010 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4011 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4012 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4013 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4014 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4015 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4016 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4017 | int result; |
| 4018 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4019 | if (rq->cmd_type != REQ_TYPE_FS) { |
| 4020 | dout("%s: non-fs request type %d\n", __func__, |
| 4021 | (int) rq->cmd_type); |
| 4022 | result = -EIO; |
| 4023 | goto err; |
| 4024 | } |
| 4025 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4026 | if (req_op(rq) == REQ_OP_DISCARD) |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4027 | op_type = OBJ_OP_DISCARD; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4028 | else if (req_op(rq) == REQ_OP_WRITE) |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4029 | op_type = OBJ_OP_WRITE; |
| 4030 | else |
| 4031 | op_type = OBJ_OP_READ; |
| 4032 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4033 | /* Ignore/skip any zero-length requests */ |
| 4034 | |
| 4035 | if (!length) { |
| 4036 | dout("%s: zero-length request\n", __func__); |
| 4037 | result = 0; |
| 4038 | goto err_rq; |
| 4039 | } |
| 4040 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4041 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4042 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4043 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4044 | if (rbd_dev->mapping.read_only) { |
| 4045 | result = -EROFS; |
| 4046 | goto err_rq; |
| 4047 | } |
| 4048 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4049 | } |
| 4050 | |
| 4051 | /* |
| 4052 | * Quit early if the mapped snapshot no longer exists. It's |
| 4053 | * still possible the snapshot will have disappeared by the |
| 4054 | * time our request arrives at the osd, but there's no sense in |
| 4055 | * sending it if we already know. |
| 4056 | */ |
| 4057 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4058 | dout("request for non-existent snapshot"); |
| 4059 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4060 | result = -ENXIO; |
| 4061 | goto err_rq; |
| 4062 | } |
| 4063 | |
| 4064 | if (offset && length > U64_MAX - offset + 1) { |
| 4065 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4066 | length); |
| 4067 | result = -EINVAL; |
| 4068 | goto err_rq; /* Shouldn't happen */ |
| 4069 | } |
| 4070 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4071 | blk_mq_start_request(rq); |
| 4072 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4073 | down_read(&rbd_dev->header_rwsem); |
| 4074 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4075 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4076 | snapc = rbd_dev->header.snapc; |
| 4077 | ceph_get_snap_context(snapc); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4078 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4079 | } else { |
| 4080 | must_be_locked = rbd_dev->opts->lock_on_read && |
| 4081 | rbd_is_lock_supported(rbd_dev); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4082 | } |
| 4083 | up_read(&rbd_dev->header_rwsem); |
| 4084 | |
| 4085 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4086 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4087 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4088 | result = -EIO; |
| 4089 | goto err_rq; |
| 4090 | } |
| 4091 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4092 | if (must_be_locked) { |
| 4093 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4094 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4095 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4096 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4097 | |
| 4098 | WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^ |
| 4099 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 4100 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4101 | result = -EBLACKLISTED; |
| 4102 | goto err_unlock; |
| 4103 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4104 | } |
| 4105 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4106 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4107 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4108 | if (!img_request) { |
| 4109 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4110 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4111 | } |
| 4112 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4113 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4114 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4115 | if (op_type == OBJ_OP_DISCARD) |
| 4116 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4117 | NULL); |
| 4118 | else |
| 4119 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4120 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4121 | if (result) |
| 4122 | goto err_img_request; |
| 4123 | |
| 4124 | result = rbd_img_request_submit(img_request); |
| 4125 | if (result) |
| 4126 | goto err_img_request; |
| 4127 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4128 | if (must_be_locked) |
| 4129 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4130 | return; |
| 4131 | |
| 4132 | err_img_request: |
| 4133 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4134 | err_unlock: |
| 4135 | if (must_be_locked) |
| 4136 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4137 | err_rq: |
| 4138 | if (result) |
| 4139 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4140 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4141 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4142 | err: |
| 4143 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4144 | } |
| 4145 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4146 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4147 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4148 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4149 | struct request *rq = bd->rq; |
| 4150 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4151 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4152 | queue_work(rbd_wq, work); |
| 4153 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4154 | } |
| 4155 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4156 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4157 | { |
| 4158 | struct gendisk *disk = rbd_dev->disk; |
| 4159 | |
| 4160 | if (!disk) |
| 4161 | return; |
| 4162 | |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4163 | rbd_dev->disk = NULL; |
| 4164 | if (disk->flags & GENHD_FL_UP) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4165 | del_gendisk(disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4166 | if (disk->queue) |
| 4167 | blk_cleanup_queue(disk->queue); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4168 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4169 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4170 | put_disk(disk); |
| 4171 | } |
| 4172 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4173 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4174 | struct ceph_object_id *oid, |
| 4175 | struct ceph_object_locator *oloc, |
| 4176 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4177 | |
| 4178 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4179 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4180 | struct ceph_osd_request *req; |
| 4181 | struct page **pages; |
| 4182 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4183 | int ret; |
| 4184 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4185 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4186 | if (!req) |
| 4187 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4188 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4189 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4190 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4191 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4192 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4193 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4194 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4195 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4196 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4197 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4198 | if (IS_ERR(pages)) { |
| 4199 | ret = PTR_ERR(pages); |
| 4200 | goto out_req; |
| 4201 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4202 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4203 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4204 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4205 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4206 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4207 | ceph_osdc_start_request(osdc, req, false); |
| 4208 | ret = ceph_osdc_wait_request(osdc, req); |
| 4209 | if (ret >= 0) |
| 4210 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4211 | |
| 4212 | out_req: |
| 4213 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4214 | return ret; |
| 4215 | } |
| 4216 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4217 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4218 | * Read the complete header for the given rbd device. On successful |
| 4219 | * return, the rbd_dev->header field will contain up-to-date |
| 4220 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4221 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4222 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4223 | { |
| 4224 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4225 | u32 snap_count = 0; |
| 4226 | u64 names_size = 0; |
| 4227 | u32 want_count; |
| 4228 | int ret; |
| 4229 | |
| 4230 | /* |
| 4231 | * The complete header will include an array of its 64-bit |
| 4232 | * snapshot ids, followed by the names of those snapshots as |
| 4233 | * a contiguous block of NUL-terminated strings. Note that |
| 4234 | * the number of snapshots could change by the time we read |
| 4235 | * it in, in which case we re-read it. |
| 4236 | */ |
| 4237 | do { |
| 4238 | size_t size; |
| 4239 | |
| 4240 | kfree(ondisk); |
| 4241 | |
| 4242 | size = sizeof (*ondisk); |
| 4243 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4244 | size += names_size; |
| 4245 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4246 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4247 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4248 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4249 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4250 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4251 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4252 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4253 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4254 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4255 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4256 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4257 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4258 | } |
| 4259 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4260 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4261 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4262 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4266 | want_count = snap_count; |
| 4267 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4268 | } while (snap_count != want_count); |
| 4269 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4270 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4271 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4272 | kfree(ondisk); |
| 4273 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4274 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4277 | /* |
| 4278 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4279 | * has disappeared from the (just updated) snapshot context. |
| 4280 | */ |
| 4281 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4282 | { |
| 4283 | u64 snap_id; |
| 4284 | |
| 4285 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4286 | return; |
| 4287 | |
| 4288 | snap_id = rbd_dev->spec->snap_id; |
| 4289 | if (snap_id == CEPH_NOSNAP) |
| 4290 | return; |
| 4291 | |
| 4292 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4293 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4294 | } |
| 4295 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4296 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4297 | { |
| 4298 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4299 | |
| 4300 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4301 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4302 | * try to update its size. If REMOVING is set, updating size |
| 4303 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4304 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4305 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4306 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4307 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4308 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4309 | set_capacity(rbd_dev->disk, size); |
| 4310 | revalidate_disk(rbd_dev->disk); |
| 4311 | } |
| 4312 | } |
| 4313 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4314 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4315 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4316 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4317 | int ret; |
| 4318 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4319 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4320 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4321 | |
| 4322 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4323 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4324 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4325 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4326 | /* |
| 4327 | * If there is a parent, see if it has disappeared due to the |
| 4328 | * mapped image getting flattened. |
| 4329 | */ |
| 4330 | if (rbd_dev->parent) { |
| 4331 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4332 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4333 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4334 | } |
| 4335 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4336 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4337 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4338 | } else { |
| 4339 | /* validate mapped snapshot's EXISTS flag */ |
| 4340 | rbd_exists_validate(rbd_dev); |
| 4341 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4342 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4343 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4344 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4345 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4346 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4347 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4348 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4349 | } |
| 4350 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4351 | static int rbd_init_request(void *data, struct request *rq, |
| 4352 | unsigned int hctx_idx, unsigned int request_idx, |
| 4353 | unsigned int numa_node) |
| 4354 | { |
| 4355 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4356 | |
| 4357 | INIT_WORK(work, rbd_queue_workfn); |
| 4358 | return 0; |
| 4359 | } |
| 4360 | |
| 4361 | static struct blk_mq_ops rbd_mq_ops = { |
| 4362 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4363 | .init_request = rbd_init_request, |
| 4364 | }; |
| 4365 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4366 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4367 | { |
| 4368 | struct gendisk *disk; |
| 4369 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4370 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4371 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4372 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4373 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4374 | disk = alloc_disk(single_major ? |
| 4375 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4376 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4377 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4378 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4379 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4380 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4381 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4382 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4383 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4384 | if (single_major) |
| 4385 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4386 | disk->fops = &rbd_bd_ops; |
| 4387 | disk->private_data = rbd_dev; |
| 4388 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4389 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4390 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4391 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4392 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4393 | 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] | 4394 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4395 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4396 | |
| 4397 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4398 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4399 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4400 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4401 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4402 | if (IS_ERR(q)) { |
| 4403 | err = PTR_ERR(q); |
| 4404 | goto out_tag_set; |
| 4405 | } |
| 4406 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4407 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4408 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4409 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4410 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4411 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4412 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4413 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4414 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4415 | blk_queue_max_segment_size(q, segment_size); |
| 4416 | blk_queue_io_min(q, segment_size); |
| 4417 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4418 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4419 | /* enable the discard support */ |
| 4420 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4421 | q->limits.discard_granularity = segment_size; |
| 4422 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4423 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4424 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4425 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4426 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
| 4427 | q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 4428 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4429 | disk->queue = q; |
| 4430 | |
| 4431 | q->queuedata = rbd_dev; |
| 4432 | |
| 4433 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4434 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4435 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4436 | out_tag_set: |
| 4437 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4438 | out_disk: |
| 4439 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4440 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4441 | } |
| 4442 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4443 | /* |
| 4444 | sysfs |
| 4445 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4446 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4447 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4448 | { |
| 4449 | return container_of(dev, struct rbd_device, dev); |
| 4450 | } |
| 4451 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4452 | static ssize_t rbd_size_show(struct device *dev, |
| 4453 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4454 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4455 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4456 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4457 | return sprintf(buf, "%llu\n", |
| 4458 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4459 | } |
| 4460 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4461 | /* |
| 4462 | * Note this shows the features for whatever's mapped, which is not |
| 4463 | * necessarily the base image. |
| 4464 | */ |
| 4465 | static ssize_t rbd_features_show(struct device *dev, |
| 4466 | struct device_attribute *attr, char *buf) |
| 4467 | { |
| 4468 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4469 | |
| 4470 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4471 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4472 | } |
| 4473 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4474 | static ssize_t rbd_major_show(struct device *dev, |
| 4475 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4476 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4477 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4478 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4479 | if (rbd_dev->major) |
| 4480 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4481 | |
| 4482 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4483 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4484 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4485 | static ssize_t rbd_minor_show(struct device *dev, |
| 4486 | struct device_attribute *attr, char *buf) |
| 4487 | { |
| 4488 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4489 | |
| 4490 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4491 | } |
| 4492 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4493 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4494 | struct device_attribute *attr, char *buf) |
| 4495 | { |
| 4496 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4497 | struct ceph_entity_addr *client_addr = |
| 4498 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4499 | |
| 4500 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4501 | le32_to_cpu(client_addr->nonce)); |
| 4502 | } |
| 4503 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4504 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4505 | struct device_attribute *attr, char *buf) |
| 4506 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4507 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4508 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4509 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4510 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4513 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4514 | struct device_attribute *attr, char *buf) |
| 4515 | { |
| 4516 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4517 | |
| 4518 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4519 | } |
| 4520 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4521 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4522 | struct device_attribute *attr, char *buf) |
| 4523 | { |
| 4524 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4525 | |
| 4526 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
| 4529 | static ssize_t rbd_pool_show(struct device *dev, |
| 4530 | struct device_attribute *attr, char *buf) |
| 4531 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4532 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4533 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4534 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4537 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4538 | struct device_attribute *attr, char *buf) |
| 4539 | { |
| 4540 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4541 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4542 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4543 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4544 | } |
| 4545 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4546 | static ssize_t rbd_name_show(struct device *dev, |
| 4547 | struct device_attribute *attr, char *buf) |
| 4548 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4549 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4550 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4551 | if (rbd_dev->spec->image_name) |
| 4552 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4553 | |
| 4554 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4555 | } |
| 4556 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4557 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4558 | struct device_attribute *attr, char *buf) |
| 4559 | { |
| 4560 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4561 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4562 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4563 | } |
| 4564 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4565 | /* |
| 4566 | * Shows the name of the currently-mapped snapshot (or |
| 4567 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4568 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4569 | static ssize_t rbd_snap_show(struct device *dev, |
| 4570 | struct device_attribute *attr, |
| 4571 | char *buf) |
| 4572 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4573 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4574 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4575 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4576 | } |
| 4577 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4578 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4579 | struct device_attribute *attr, char *buf) |
| 4580 | { |
| 4581 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4582 | |
| 4583 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4584 | } |
| 4585 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4586 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4587 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4588 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4589 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4590 | */ |
| 4591 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4592 | struct device_attribute *attr, |
| 4593 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4594 | { |
| 4595 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4596 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4597 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4598 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4599 | return sprintf(buf, "(no parent image)\n"); |
| 4600 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4601 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4602 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4603 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4604 | count += sprintf(&buf[count], "%s" |
| 4605 | "pool_id %llu\npool_name %s\n" |
| 4606 | "image_id %s\nimage_name %s\n" |
| 4607 | "snap_id %llu\nsnap_name %s\n" |
| 4608 | "overlap %llu\n", |
| 4609 | !count ? "" : "\n", /* first? */ |
| 4610 | spec->pool_id, spec->pool_name, |
| 4611 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4612 | spec->snap_id, spec->snap_name, |
| 4613 | rbd_dev->parent_overlap); |
| 4614 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4615 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4616 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4617 | } |
| 4618 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4619 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4620 | struct device_attribute *attr, |
| 4621 | const char *buf, |
| 4622 | size_t size) |
| 4623 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4624 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4625 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4626 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4627 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4628 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4629 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4630 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4631 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4632 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4633 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4634 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4635 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4636 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4637 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4638 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4639 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4640 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4641 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4642 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4643 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4644 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4645 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4646 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4647 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4648 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4649 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4650 | |
| 4651 | static struct attribute *rbd_attrs[] = { |
| 4652 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4653 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4654 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4655 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4656 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4657 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4658 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4659 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4660 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4661 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4662 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4663 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4664 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4665 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4666 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4667 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4668 | NULL |
| 4669 | }; |
| 4670 | |
| 4671 | static struct attribute_group rbd_attr_group = { |
| 4672 | .attrs = rbd_attrs, |
| 4673 | }; |
| 4674 | |
| 4675 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4676 | &rbd_attr_group, |
| 4677 | NULL |
| 4678 | }; |
| 4679 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4680 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4681 | |
| 4682 | static struct device_type rbd_device_type = { |
| 4683 | .name = "rbd", |
| 4684 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4685 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4686 | }; |
| 4687 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4688 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4689 | { |
| 4690 | kref_get(&spec->kref); |
| 4691 | |
| 4692 | return spec; |
| 4693 | } |
| 4694 | |
| 4695 | static void rbd_spec_free(struct kref *kref); |
| 4696 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4697 | { |
| 4698 | if (spec) |
| 4699 | kref_put(&spec->kref, rbd_spec_free); |
| 4700 | } |
| 4701 | |
| 4702 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4703 | { |
| 4704 | struct rbd_spec *spec; |
| 4705 | |
| 4706 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4707 | if (!spec) |
| 4708 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4709 | |
| 4710 | spec->pool_id = CEPH_NOPOOL; |
| 4711 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4712 | kref_init(&spec->kref); |
| 4713 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4714 | return spec; |
| 4715 | } |
| 4716 | |
| 4717 | static void rbd_spec_free(struct kref *kref) |
| 4718 | { |
| 4719 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4720 | |
| 4721 | kfree(spec->pool_name); |
| 4722 | kfree(spec->image_id); |
| 4723 | kfree(spec->image_name); |
| 4724 | kfree(spec->snap_name); |
| 4725 | kfree(spec); |
| 4726 | } |
| 4727 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4728 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4729 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4730 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4731 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4732 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4733 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4734 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4735 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4736 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4737 | rbd_put_client(rbd_dev->rbd_client); |
| 4738 | rbd_spec_put(rbd_dev->spec); |
| 4739 | kfree(rbd_dev->opts); |
| 4740 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4741 | } |
| 4742 | |
| 4743 | static void rbd_dev_release(struct device *dev) |
| 4744 | { |
| 4745 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4746 | bool need_put = !!rbd_dev->opts; |
| 4747 | |
| 4748 | if (need_put) { |
| 4749 | destroy_workqueue(rbd_dev->task_wq); |
| 4750 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4751 | } |
| 4752 | |
| 4753 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4754 | |
| 4755 | /* |
| 4756 | * This is racy, but way better than putting module outside of |
| 4757 | * the release callback. The race window is pretty small, so |
| 4758 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4759 | */ |
| 4760 | if (need_put) |
| 4761 | module_put(THIS_MODULE); |
| 4762 | } |
| 4763 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4764 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4765 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4766 | { |
| 4767 | struct rbd_device *rbd_dev; |
| 4768 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4769 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4770 | if (!rbd_dev) |
| 4771 | return NULL; |
| 4772 | |
| 4773 | spin_lock_init(&rbd_dev->lock); |
| 4774 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4775 | init_rwsem(&rbd_dev->header_rwsem); |
| 4776 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4777 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4778 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4779 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4780 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4781 | mutex_init(&rbd_dev->watch_mutex); |
| 4782 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4783 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4784 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4785 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4786 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4787 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4788 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4789 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4790 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4791 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4792 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4793 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4794 | rbd_dev->dev.type = &rbd_device_type; |
| 4795 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4796 | device_initialize(&rbd_dev->dev); |
| 4797 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4798 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4799 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4800 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4801 | return rbd_dev; |
| 4802 | } |
| 4803 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4804 | /* |
| 4805 | * Create a mapping rbd_dev. |
| 4806 | */ |
| 4807 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4808 | struct rbd_spec *spec, |
| 4809 | struct rbd_options *opts) |
| 4810 | { |
| 4811 | struct rbd_device *rbd_dev; |
| 4812 | |
| 4813 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4814 | if (!rbd_dev) |
| 4815 | return NULL; |
| 4816 | |
| 4817 | rbd_dev->opts = opts; |
| 4818 | |
| 4819 | /* get an id and fill in device name */ |
| 4820 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4821 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4822 | GFP_KERNEL); |
| 4823 | if (rbd_dev->dev_id < 0) |
| 4824 | goto fail_rbd_dev; |
| 4825 | |
| 4826 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4827 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4828 | rbd_dev->name); |
| 4829 | if (!rbd_dev->task_wq) |
| 4830 | goto fail_dev_id; |
| 4831 | |
| 4832 | /* we have a ref from do_rbd_add() */ |
| 4833 | __module_get(THIS_MODULE); |
| 4834 | |
| 4835 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4836 | return rbd_dev; |
| 4837 | |
| 4838 | fail_dev_id: |
| 4839 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4840 | fail_rbd_dev: |
| 4841 | rbd_dev_free(rbd_dev); |
| 4842 | return NULL; |
| 4843 | } |
| 4844 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4845 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4846 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4847 | if (rbd_dev) |
| 4848 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4849 | } |
| 4850 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4851 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4852 | * Get the size and object order for an image snapshot, or if |
| 4853 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4854 | * image. |
| 4855 | */ |
| 4856 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4857 | u8 *order, u64 *snap_size) |
| 4858 | { |
| 4859 | __le64 snapid = cpu_to_le64(snap_id); |
| 4860 | int ret; |
| 4861 | struct { |
| 4862 | u8 order; |
| 4863 | __le64 size; |
| 4864 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4865 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4866 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4867 | &rbd_dev->header_oloc, "get_size", |
| 4868 | &snapid, sizeof(snapid), |
| 4869 | &size_buf, sizeof(size_buf)); |
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 | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4871 | if (ret < 0) |
| 4872 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4873 | if (ret < sizeof (size_buf)) |
| 4874 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4875 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4876 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4877 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4878 | dout(" order %u", (unsigned int)*order); |
| 4879 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4880 | *snap_size = le64_to_cpu(size_buf.size); |
| 4881 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4882 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4883 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4884 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4885 | |
| 4886 | return 0; |
| 4887 | } |
| 4888 | |
| 4889 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4890 | { |
| 4891 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4892 | &rbd_dev->header.obj_order, |
| 4893 | &rbd_dev->header.image_size); |
| 4894 | } |
| 4895 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4896 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4897 | { |
| 4898 | void *reply_buf; |
| 4899 | int ret; |
| 4900 | void *p; |
| 4901 | |
| 4902 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4903 | if (!reply_buf) |
| 4904 | return -ENOMEM; |
| 4905 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4906 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4907 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4908 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4909 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4910 | if (ret < 0) |
| 4911 | goto out; |
| 4912 | |
| 4913 | p = reply_buf; |
| 4914 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4915 | p + ret, NULL, GFP_NOIO); |
| 4916 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4917 | |
| 4918 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4919 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4920 | rbd_dev->header.object_prefix = NULL; |
| 4921 | } else { |
| 4922 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4923 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4924 | out: |
| 4925 | kfree(reply_buf); |
| 4926 | |
| 4927 | return ret; |
| 4928 | } |
| 4929 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4930 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4931 | u64 *snap_features) |
| 4932 | { |
| 4933 | __le64 snapid = cpu_to_le64(snap_id); |
| 4934 | struct { |
| 4935 | __le64 features; |
| 4936 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4937 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4938 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4939 | int ret; |
| 4940 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4941 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4942 | &rbd_dev->header_oloc, "get_features", |
| 4943 | &snapid, sizeof(snapid), |
| 4944 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4945 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4946 | if (ret < 0) |
| 4947 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4948 | if (ret < sizeof (features_buf)) |
| 4949 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4950 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4951 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4952 | if (unsup) { |
| 4953 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4954 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4955 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4956 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4957 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4958 | *snap_features = le64_to_cpu(features_buf.features); |
| 4959 | |
| 4960 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4961 | (unsigned long long)snap_id, |
| 4962 | (unsigned long long)*snap_features, |
| 4963 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4964 | |
| 4965 | return 0; |
| 4966 | } |
| 4967 | |
| 4968 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4969 | { |
| 4970 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4971 | &rbd_dev->header.features); |
| 4972 | } |
| 4973 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4974 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4975 | { |
| 4976 | struct rbd_spec *parent_spec; |
| 4977 | size_t size; |
| 4978 | void *reply_buf = NULL; |
| 4979 | __le64 snapid; |
| 4980 | void *p; |
| 4981 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4982 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4983 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4984 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4985 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4986 | int ret; |
| 4987 | |
| 4988 | parent_spec = rbd_spec_alloc(); |
| 4989 | if (!parent_spec) |
| 4990 | return -ENOMEM; |
| 4991 | |
| 4992 | size = sizeof (__le64) + /* pool_id */ |
| 4993 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4994 | sizeof (__le64) + /* snap_id */ |
| 4995 | sizeof (__le64); /* overlap */ |
| 4996 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4997 | if (!reply_buf) { |
| 4998 | ret = -ENOMEM; |
| 4999 | goto out_err; |
| 5000 | } |
| 5001 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5002 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5003 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5004 | &rbd_dev->header_oloc, "get_parent", |
| 5005 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5006 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5007 | if (ret < 0) |
| 5008 | goto out_err; |
| 5009 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5010 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5011 | end = reply_buf + ret; |
| 5012 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5013 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5014 | if (pool_id == CEPH_NOPOOL) { |
| 5015 | /* |
| 5016 | * Either the parent never existed, or we have |
| 5017 | * record of it but the image got flattened so it no |
| 5018 | * longer has a parent. When the parent of a |
| 5019 | * layered image disappears we immediately set the |
| 5020 | * overlap to 0. The effect of this is that all new |
| 5021 | * requests will be treated as if the image had no |
| 5022 | * parent. |
| 5023 | */ |
| 5024 | if (rbd_dev->parent_overlap) { |
| 5025 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5026 | rbd_dev_parent_put(rbd_dev); |
| 5027 | pr_info("%s: clone image has been flattened\n", |
| 5028 | rbd_dev->disk->disk_name); |
| 5029 | } |
| 5030 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5031 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5032 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5033 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5034 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5035 | |
| 5036 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5037 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5038 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5039 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5040 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5041 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5042 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5043 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5044 | if (IS_ERR(image_id)) { |
| 5045 | ret = PTR_ERR(image_id); |
| 5046 | goto out_err; |
| 5047 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5048 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5049 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5050 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5051 | /* |
| 5052 | * The parent won't change (except when the clone is |
| 5053 | * flattened, already handled that). So we only need to |
| 5054 | * record the parent spec we have not already done so. |
| 5055 | */ |
| 5056 | if (!rbd_dev->parent_spec) { |
| 5057 | parent_spec->pool_id = pool_id; |
| 5058 | parent_spec->image_id = image_id; |
| 5059 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5060 | rbd_dev->parent_spec = parent_spec; |
| 5061 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5062 | } else { |
| 5063 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5064 | } |
| 5065 | |
| 5066 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5067 | * We always update the parent overlap. If it's zero we issue |
| 5068 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5069 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5070 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5071 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5072 | /* refresh, careful to warn just once */ |
| 5073 | if (rbd_dev->parent_overlap) |
| 5074 | rbd_warn(rbd_dev, |
| 5075 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5076 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5077 | /* initial probe */ |
| 5078 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5079 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5080 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5081 | rbd_dev->parent_overlap = overlap; |
| 5082 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5083 | out: |
| 5084 | ret = 0; |
| 5085 | out_err: |
| 5086 | kfree(reply_buf); |
| 5087 | rbd_spec_put(parent_spec); |
| 5088 | |
| 5089 | return ret; |
| 5090 | } |
| 5091 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5092 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5093 | { |
| 5094 | struct { |
| 5095 | __le64 stripe_unit; |
| 5096 | __le64 stripe_count; |
| 5097 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5098 | size_t size = sizeof (striping_info_buf); |
| 5099 | void *p; |
| 5100 | u64 obj_size; |
| 5101 | u64 stripe_unit; |
| 5102 | u64 stripe_count; |
| 5103 | int ret; |
| 5104 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5105 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5106 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5107 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5108 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5109 | if (ret < 0) |
| 5110 | return ret; |
| 5111 | if (ret < size) |
| 5112 | return -ERANGE; |
| 5113 | |
| 5114 | /* |
| 5115 | * We don't actually support the "fancy striping" feature |
| 5116 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5117 | * defaults the behavior is the same as before. So find |
| 5118 | * out, and only fail if the image has non-default values. |
| 5119 | */ |
| 5120 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5121 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5122 | p = &striping_info_buf; |
| 5123 | stripe_unit = ceph_decode_64(&p); |
| 5124 | if (stripe_unit != obj_size) { |
| 5125 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5126 | "(got %llu want %llu)", |
| 5127 | stripe_unit, obj_size); |
| 5128 | return -EINVAL; |
| 5129 | } |
| 5130 | stripe_count = ceph_decode_64(&p); |
| 5131 | if (stripe_count != 1) { |
| 5132 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5133 | "(got %llu want 1)", stripe_count); |
| 5134 | return -EINVAL; |
| 5135 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5136 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5137 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5138 | |
| 5139 | return 0; |
| 5140 | } |
| 5141 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5142 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5143 | { |
| 5144 | __le64 data_pool_id; |
| 5145 | int ret; |
| 5146 | |
| 5147 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5148 | &rbd_dev->header_oloc, "get_data_pool", |
| 5149 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5150 | if (ret < 0) |
| 5151 | return ret; |
| 5152 | if (ret < sizeof(data_pool_id)) |
| 5153 | return -EBADMSG; |
| 5154 | |
| 5155 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5156 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5157 | return 0; |
| 5158 | } |
| 5159 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5160 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5161 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5162 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5163 | size_t image_id_size; |
| 5164 | char *image_id; |
| 5165 | void *p; |
| 5166 | void *end; |
| 5167 | size_t size; |
| 5168 | void *reply_buf = NULL; |
| 5169 | size_t len = 0; |
| 5170 | char *image_name = NULL; |
| 5171 | int ret; |
| 5172 | |
| 5173 | rbd_assert(!rbd_dev->spec->image_name); |
| 5174 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5175 | len = strlen(rbd_dev->spec->image_id); |
| 5176 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5177 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5178 | if (!image_id) |
| 5179 | return NULL; |
| 5180 | |
| 5181 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5182 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5183 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5184 | |
| 5185 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5186 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5187 | if (!reply_buf) |
| 5188 | goto out; |
| 5189 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5190 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5191 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5192 | "dir_get_name", image_id, image_id_size, |
| 5193 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5194 | if (ret < 0) |
| 5195 | goto out; |
| 5196 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5197 | end = reply_buf + ret; |
| 5198 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5199 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5200 | if (IS_ERR(image_name)) |
| 5201 | image_name = NULL; |
| 5202 | else |
| 5203 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5204 | out: |
| 5205 | kfree(reply_buf); |
| 5206 | kfree(image_id); |
| 5207 | |
| 5208 | return image_name; |
| 5209 | } |
| 5210 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5211 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5212 | { |
| 5213 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5214 | const char *snap_name; |
| 5215 | u32 which = 0; |
| 5216 | |
| 5217 | /* Skip over names until we find the one we are looking for */ |
| 5218 | |
| 5219 | snap_name = rbd_dev->header.snap_names; |
| 5220 | while (which < snapc->num_snaps) { |
| 5221 | if (!strcmp(name, snap_name)) |
| 5222 | return snapc->snaps[which]; |
| 5223 | snap_name += strlen(snap_name) + 1; |
| 5224 | which++; |
| 5225 | } |
| 5226 | return CEPH_NOSNAP; |
| 5227 | } |
| 5228 | |
| 5229 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5230 | { |
| 5231 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5232 | u32 which; |
| 5233 | bool found = false; |
| 5234 | u64 snap_id; |
| 5235 | |
| 5236 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5237 | const char *snap_name; |
| 5238 | |
| 5239 | snap_id = snapc->snaps[which]; |
| 5240 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5241 | if (IS_ERR(snap_name)) { |
| 5242 | /* ignore no-longer existing snapshots */ |
| 5243 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5244 | continue; |
| 5245 | else |
| 5246 | break; |
| 5247 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5248 | found = !strcmp(name, snap_name); |
| 5249 | kfree(snap_name); |
| 5250 | } |
| 5251 | return found ? snap_id : CEPH_NOSNAP; |
| 5252 | } |
| 5253 | |
| 5254 | /* |
| 5255 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5256 | * no snapshot by that name is found, or if an error occurs. |
| 5257 | */ |
| 5258 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5259 | { |
| 5260 | if (rbd_dev->image_format == 1) |
| 5261 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5262 | |
| 5263 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5264 | } |
| 5265 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5266 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5267 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5268 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5269 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5270 | { |
| 5271 | struct rbd_spec *spec = rbd_dev->spec; |
| 5272 | |
| 5273 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5274 | rbd_assert(spec->image_id && spec->image_name); |
| 5275 | rbd_assert(spec->snap_name); |
| 5276 | |
| 5277 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5278 | u64 snap_id; |
| 5279 | |
| 5280 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5281 | if (snap_id == CEPH_NOSNAP) |
| 5282 | return -ENOENT; |
| 5283 | |
| 5284 | spec->snap_id = snap_id; |
| 5285 | } else { |
| 5286 | spec->snap_id = CEPH_NOSNAP; |
| 5287 | } |
| 5288 | |
| 5289 | return 0; |
| 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * A parent image will have all ids but none of the names. |
| 5294 | * |
| 5295 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5296 | * can't figure out the name for an image id. |
| 5297 | */ |
| 5298 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5299 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5300 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5301 | struct rbd_spec *spec = rbd_dev->spec; |
| 5302 | const char *pool_name; |
| 5303 | const char *image_name; |
| 5304 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5305 | int ret; |
| 5306 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5307 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5308 | rbd_assert(spec->image_id); |
| 5309 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5310 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5311 | /* 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] | 5312 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5313 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5314 | if (!pool_name) { |
| 5315 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5316 | return -EIO; |
| 5317 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5318 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5319 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5320 | return -ENOMEM; |
| 5321 | |
| 5322 | /* Fetch the image name; tolerate failure here */ |
| 5323 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5324 | image_name = rbd_dev_image_name(rbd_dev); |
| 5325 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5326 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5327 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5328 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5329 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5330 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5331 | if (IS_ERR(snap_name)) { |
| 5332 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5333 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | spec->pool_name = pool_name; |
| 5337 | spec->image_name = image_name; |
| 5338 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5339 | |
| 5340 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5341 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5342 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5343 | kfree(image_name); |
| 5344 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5345 | return ret; |
| 5346 | } |
| 5347 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5348 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5349 | { |
| 5350 | size_t size; |
| 5351 | int ret; |
| 5352 | void *reply_buf; |
| 5353 | void *p; |
| 5354 | void *end; |
| 5355 | u64 seq; |
| 5356 | u32 snap_count; |
| 5357 | struct ceph_snap_context *snapc; |
| 5358 | u32 i; |
| 5359 | |
| 5360 | /* |
| 5361 | * We'll need room for the seq value (maximum snapshot id), |
| 5362 | * snapshot count, and array of that many snapshot ids. |
| 5363 | * For now we have a fixed upper limit on the number we're |
| 5364 | * prepared to receive. |
| 5365 | */ |
| 5366 | size = sizeof (__le64) + sizeof (__le32) + |
| 5367 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5368 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5369 | if (!reply_buf) |
| 5370 | return -ENOMEM; |
| 5371 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5372 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5373 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5374 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5375 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5376 | if (ret < 0) |
| 5377 | goto out; |
| 5378 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5379 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5380 | end = reply_buf + ret; |
| 5381 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5382 | ceph_decode_64_safe(&p, end, seq, out); |
| 5383 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5384 | |
| 5385 | /* |
| 5386 | * Make sure the reported number of snapshot ids wouldn't go |
| 5387 | * beyond the end of our buffer. But before checking that, |
| 5388 | * make sure the computed size of the snapshot context we |
| 5389 | * allocate is representable in a size_t. |
| 5390 | */ |
| 5391 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5392 | / sizeof (u64)) { |
| 5393 | ret = -EINVAL; |
| 5394 | goto out; |
| 5395 | } |
| 5396 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5397 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5398 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5399 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5400 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5401 | if (!snapc) { |
| 5402 | ret = -ENOMEM; |
| 5403 | goto out; |
| 5404 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5405 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5406 | for (i = 0; i < snap_count; i++) |
| 5407 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5408 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5409 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5410 | rbd_dev->header.snapc = snapc; |
| 5411 | |
| 5412 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5413 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5414 | out: |
| 5415 | kfree(reply_buf); |
| 5416 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5417 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5418 | } |
| 5419 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5420 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5421 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5422 | { |
| 5423 | size_t size; |
| 5424 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5425 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5426 | int ret; |
| 5427 | void *p; |
| 5428 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5429 | char *snap_name; |
| 5430 | |
| 5431 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5432 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5433 | if (!reply_buf) |
| 5434 | return ERR_PTR(-ENOMEM); |
| 5435 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5436 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5437 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5438 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5439 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5440 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5441 | if (ret < 0) { |
| 5442 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5443 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5444 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5445 | |
| 5446 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5447 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5448 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5449 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5450 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5451 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5452 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5453 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5454 | out: |
| 5455 | kfree(reply_buf); |
| 5456 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5457 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5458 | } |
| 5459 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5460 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5461 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5462 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5463 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5464 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5465 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5466 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5467 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5468 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5469 | if (first_time) { |
| 5470 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5471 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5472 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5473 | } |
| 5474 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5475 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5476 | if (ret && first_time) { |
| 5477 | kfree(rbd_dev->header.object_prefix); |
| 5478 | rbd_dev->header.object_prefix = NULL; |
| 5479 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5480 | |
| 5481 | return ret; |
| 5482 | } |
| 5483 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5484 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5485 | { |
| 5486 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5487 | |
| 5488 | if (rbd_dev->image_format == 1) |
| 5489 | return rbd_dev_v1_header_info(rbd_dev); |
| 5490 | |
| 5491 | return rbd_dev_v2_header_info(rbd_dev); |
| 5492 | } |
| 5493 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5494 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5495 | * Skips over white space at *buf, and updates *buf to point to the |
| 5496 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5497 | * the token (string of non-white space characters) found. Note |
| 5498 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5499 | */ |
| 5500 | static inline size_t next_token(const char **buf) |
| 5501 | { |
| 5502 | /* |
| 5503 | * These are the characters that produce nonzero for |
| 5504 | * isspace() in the "C" and "POSIX" locales. |
| 5505 | */ |
| 5506 | const char *spaces = " \f\n\r\t\v"; |
| 5507 | |
| 5508 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5509 | |
| 5510 | return strcspn(*buf, spaces); /* Return token length */ |
| 5511 | } |
| 5512 | |
| 5513 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5514 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5515 | * enough to hold a copy of it, and copies the token into the new |
| 5516 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5517 | * that a duplicate buffer is created even for a zero-length token. |
| 5518 | * |
| 5519 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5520 | * pointer if memory for the duplicate was not available. If |
| 5521 | * the lenp argument is a non-null pointer, the length of the token |
| 5522 | * (not including the '\0') is returned in *lenp. |
| 5523 | * |
| 5524 | * If successful, the *buf pointer will be updated to point beyond |
| 5525 | * the end of the found token. |
| 5526 | * |
| 5527 | * Note: uses GFP_KERNEL for allocation. |
| 5528 | */ |
| 5529 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5530 | { |
| 5531 | char *dup; |
| 5532 | size_t len; |
| 5533 | |
| 5534 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5535 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5536 | if (!dup) |
| 5537 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5538 | *(dup + len) = '\0'; |
| 5539 | *buf += len; |
| 5540 | |
| 5541 | if (lenp) |
| 5542 | *lenp = len; |
| 5543 | |
| 5544 | return dup; |
| 5545 | } |
| 5546 | |
| 5547 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5548 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5549 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5550 | * and the data written is passed here via a NUL-terminated buffer. |
| 5551 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5552 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5553 | * The information extracted from these options is recorded in |
| 5554 | * the other parameters which return dynamically-allocated |
| 5555 | * structures: |
| 5556 | * ceph_opts |
| 5557 | * The address of a pointer that will refer to a ceph options |
| 5558 | * structure. Caller must release the returned pointer using |
| 5559 | * ceph_destroy_options() when it is no longer needed. |
| 5560 | * rbd_opts |
| 5561 | * Address of an rbd options pointer. Fully initialized by |
| 5562 | * this function; caller must release with kfree(). |
| 5563 | * spec |
| 5564 | * Address of an rbd image specification pointer. Fully |
| 5565 | * initialized by this function based on parsed options. |
| 5566 | * Caller must release with rbd_spec_put(). |
| 5567 | * |
| 5568 | * The options passed take this form: |
| 5569 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5570 | * where: |
| 5571 | * <mon_addrs> |
| 5572 | * A comma-separated list of one or more monitor addresses. |
| 5573 | * A monitor address is an ip address, optionally followed |
| 5574 | * by a port number (separated by a colon). |
| 5575 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5576 | * <options> |
| 5577 | * A comma-separated list of ceph and/or rbd options. |
| 5578 | * <pool_name> |
| 5579 | * The name of the rados pool containing the rbd image. |
| 5580 | * <image_name> |
| 5581 | * The name of the image in that pool to map. |
| 5582 | * <snap_id> |
| 5583 | * An optional snapshot id. If provided, the mapping will |
| 5584 | * present data from the image at the time that snapshot was |
| 5585 | * created. The image head is used if no snapshot id is |
| 5586 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5587 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5588 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5589 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5590 | struct rbd_options **opts, |
| 5591 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5592 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5593 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5594 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5595 | const char *mon_addrs; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5596 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5597 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5598 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5599 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5600 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5601 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5602 | |
| 5603 | /* The first four tokens are required */ |
| 5604 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5605 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5606 | if (!len) { |
| 5607 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5608 | return -EINVAL; |
| 5609 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5610 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5611 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5612 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5613 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5614 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5615 | options = dup_token(&buf, NULL); |
| 5616 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5617 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5618 | if (!*options) { |
| 5619 | rbd_warn(NULL, "no options provided"); |
| 5620 | goto out_err; |
| 5621 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5622 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5623 | spec = rbd_spec_alloc(); |
| 5624 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5625 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5626 | |
| 5627 | spec->pool_name = dup_token(&buf, NULL); |
| 5628 | if (!spec->pool_name) |
| 5629 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5630 | if (!*spec->pool_name) { |
| 5631 | rbd_warn(NULL, "no pool name provided"); |
| 5632 | goto out_err; |
| 5633 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5634 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5635 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5636 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5637 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5638 | if (!*spec->image_name) { |
| 5639 | rbd_warn(NULL, "no image name provided"); |
| 5640 | goto out_err; |
| 5641 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5642 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5643 | /* |
| 5644 | * Snapshot name is optional; default is to use "-" |
| 5645 | * (indicating the head/no snapshot). |
| 5646 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5647 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5648 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5649 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5650 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5651 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5652 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5653 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5654 | } |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5655 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5656 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5657 | goto out_mem; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5658 | *(snap_name + len) = '\0'; |
| 5659 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5660 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5661 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5662 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5663 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5664 | if (!rbd_opts) |
| 5665 | goto out_mem; |
| 5666 | |
| 5667 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5668 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5669 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5670 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5671 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5672 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5673 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5674 | if (IS_ERR(copts)) { |
| 5675 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5676 | goto out_err; |
| 5677 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5678 | kfree(options); |
| 5679 | |
| 5680 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5681 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5682 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5683 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5684 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5685 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5686 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5687 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5688 | kfree(rbd_opts); |
| 5689 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5690 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5691 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5692 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5693 | } |
| 5694 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5695 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5696 | * Return pool id (>= 0) or a negative error code. |
| 5697 | */ |
| 5698 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5699 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5700 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5701 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5702 | int tries = 0; |
| 5703 | int ret; |
| 5704 | |
| 5705 | again: |
| 5706 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5707 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5708 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5709 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5710 | if (ret < 0) |
| 5711 | return ret; |
| 5712 | |
| 5713 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5714 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5715 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5716 | newest_epoch, |
| 5717 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5718 | goto again; |
| 5719 | } else { |
| 5720 | /* the osdmap we have is new enough */ |
| 5721 | return -ENOENT; |
| 5722 | } |
| 5723 | } |
| 5724 | |
| 5725 | return ret; |
| 5726 | } |
| 5727 | |
| 5728 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5729 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5730 | * name given to it by the user. Internally, that identifier is |
| 5731 | * what's used to specify the names of objects related to the image. |
| 5732 | * |
| 5733 | * A special "rbd id" object is used to map an rbd image name to its |
| 5734 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5735 | * with the supplied name. |
| 5736 | * |
| 5737 | * This function will record the given rbd_dev's image_id field if |
| 5738 | * it can be determined, and in that case will return 0. If any |
| 5739 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5740 | * image_id field will be unchanged (and should be NULL). |
| 5741 | */ |
| 5742 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5743 | { |
| 5744 | int ret; |
| 5745 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5746 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5747 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5748 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5749 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5750 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5751 | * When probing a parent image, the image id is already |
| 5752 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5753 | * need to fetch the image id again in this case. We |
| 5754 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5755 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5756 | if (rbd_dev->spec->image_id) { |
| 5757 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5758 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5759 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5760 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5761 | |
| 5762 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5763 | * First, see if the format 2 image id file exists, and if |
| 5764 | * so, get the image's persistent id from it. |
| 5765 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5766 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5767 | rbd_dev->spec->image_name); |
| 5768 | if (ret) |
| 5769 | return ret; |
| 5770 | |
| 5771 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5772 | |
| 5773 | /* Response will be an encoded string, which includes a length */ |
| 5774 | |
| 5775 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5776 | response = kzalloc(size, GFP_NOIO); |
| 5777 | if (!response) { |
| 5778 | ret = -ENOMEM; |
| 5779 | goto out; |
| 5780 | } |
| 5781 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5782 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5783 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5784 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5785 | "get_id", NULL, 0, |
| 5786 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5787 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5788 | if (ret == -ENOENT) { |
| 5789 | image_id = kstrdup("", GFP_KERNEL); |
| 5790 | ret = image_id ? 0 : -ENOMEM; |
| 5791 | if (!ret) |
| 5792 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5793 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5794 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5795 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5796 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5797 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5798 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5799 | if (!ret) |
| 5800 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | if (!ret) { |
| 5804 | rbd_dev->spec->image_id = image_id; |
| 5805 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5806 | } |
| 5807 | out: |
| 5808 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5809 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5810 | return ret; |
| 5811 | } |
| 5812 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5813 | /* |
| 5814 | * Undo whatever state changes are made by v1 or v2 header info |
| 5815 | * call. |
| 5816 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5817 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5818 | { |
| 5819 | struct rbd_image_header *header; |
| 5820 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5821 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5822 | |
| 5823 | /* Free dynamic fields from the header, then zero it out */ |
| 5824 | |
| 5825 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5826 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5827 | kfree(header->snap_sizes); |
| 5828 | kfree(header->snap_names); |
| 5829 | kfree(header->object_prefix); |
| 5830 | memset(header, 0, sizeof (*header)); |
| 5831 | } |
| 5832 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5833 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5834 | { |
| 5835 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5836 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5837 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5838 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5839 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5840 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5841 | /* |
| 5842 | * Get the and check features for the image. Currently the |
| 5843 | * features are assumed to never change. |
| 5844 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5845 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5846 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5847 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5848 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5849 | /* If the image supports fancy striping, get its parameters */ |
| 5850 | |
| 5851 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5852 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5853 | if (ret < 0) |
| 5854 | goto out_err; |
| 5855 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5856 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5857 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5858 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5859 | if (ret) |
| 5860 | goto out_err; |
| 5861 | } |
| 5862 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5863 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5864 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5865 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5866 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5867 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5868 | kfree(rbd_dev->header.object_prefix); |
| 5869 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5870 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5871 | } |
| 5872 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5873 | /* |
| 5874 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5875 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5876 | * length of the already discovered part of the parent chain. |
| 5877 | */ |
| 5878 | 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] | 5879 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5880 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5881 | int ret; |
| 5882 | |
| 5883 | if (!rbd_dev->parent_spec) |
| 5884 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5885 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5886 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5887 | pr_info("parent chain is too long (%d)\n", depth); |
| 5888 | ret = -EINVAL; |
| 5889 | goto out_err; |
| 5890 | } |
| 5891 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5892 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5893 | if (!parent) { |
| 5894 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5895 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5896 | } |
| 5897 | |
| 5898 | /* |
| 5899 | * Images related by parent/child relationships always share |
| 5900 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5901 | */ |
| 5902 | __rbd_get_client(rbd_dev->rbd_client); |
| 5903 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5904 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5905 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5906 | if (ret < 0) |
| 5907 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5908 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5909 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5910 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5911 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5912 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5913 | out_err: |
| 5914 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5915 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5916 | return ret; |
| 5917 | } |
| 5918 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5919 | /* |
| 5920 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5921 | * upon return. |
| 5922 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5923 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5924 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5925 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5926 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5927 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5928 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5929 | if (!single_major) { |
| 5930 | ret = register_blkdev(0, rbd_dev->name); |
| 5931 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5932 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5933 | |
| 5934 | rbd_dev->major = ret; |
| 5935 | rbd_dev->minor = 0; |
| 5936 | } else { |
| 5937 | rbd_dev->major = rbd_major; |
| 5938 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5939 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5940 | |
| 5941 | /* Set up the blkdev mapping. */ |
| 5942 | |
| 5943 | ret = rbd_init_disk(rbd_dev); |
| 5944 | if (ret) |
| 5945 | goto err_out_blkdev; |
| 5946 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5947 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5948 | if (ret) |
| 5949 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5950 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5951 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 5952 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5953 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5954 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
| 5955 | ret = device_add(&rbd_dev->dev); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5956 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5957 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5958 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5959 | /* Everything's ready. Announce the disk to the world. */ |
| 5960 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5961 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5962 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5963 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5964 | spin_lock(&rbd_dev_list_lock); |
| 5965 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5966 | spin_unlock(&rbd_dev_list_lock); |
| 5967 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5968 | add_disk(rbd_dev->disk); |
Ilya Dryomov | ca7909e | 2016-08-18 18:38:41 +0200 | [diff] [blame] | 5969 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5970 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5971 | rbd_dev->header.features); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5972 | |
| 5973 | return ret; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5974 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5975 | err_out_mapping: |
| 5976 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5977 | err_out_disk: |
| 5978 | rbd_free_disk(rbd_dev); |
| 5979 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5980 | if (!single_major) |
| 5981 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5982 | err_out_unlock: |
| 5983 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5984 | return ret; |
| 5985 | } |
| 5986 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5987 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5988 | { |
| 5989 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5990 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5991 | |
| 5992 | /* Record the header object name for this rbd image. */ |
| 5993 | |
| 5994 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5995 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5996 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5997 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5998 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5999 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6000 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6001 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6002 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6003 | } |
| 6004 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6005 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6006 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6007 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6008 | rbd_dev->image_format = 0; |
| 6009 | kfree(rbd_dev->spec->image_id); |
| 6010 | rbd_dev->spec->image_id = NULL; |
| 6011 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6012 | rbd_dev_destroy(rbd_dev); |
| 6013 | } |
| 6014 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6015 | /* |
| 6016 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6017 | * device. If this image is the one being mapped (i.e., not a |
| 6018 | * parent), initiate a watch on its header object before using that |
| 6019 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6020 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6021 | 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] | 6022 | { |
| 6023 | int ret; |
| 6024 | |
| 6025 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6026 | * Get the id from the image id object. Unless there's an |
| 6027 | * error, rbd_dev->spec->image_id will be filled in with |
| 6028 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6029 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6030 | */ |
| 6031 | ret = rbd_dev_image_id(rbd_dev); |
| 6032 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6033 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6034 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6035 | ret = rbd_dev_header_name(rbd_dev); |
| 6036 | if (ret) |
| 6037 | goto err_out_format; |
| 6038 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6039 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6040 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6041 | if (ret) { |
| 6042 | if (ret == -ENOENT) |
| 6043 | pr_info("image %s/%s does not exist\n", |
| 6044 | rbd_dev->spec->pool_name, |
| 6045 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6046 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6047 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6048 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6049 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6050 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6051 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6052 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6053 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6054 | /* |
| 6055 | * If this image is the one being mapped, we have pool name and |
| 6056 | * id, image name and id, and snap name - need to fill snap id. |
| 6057 | * Otherwise this is a parent image, identified by pool, image |
| 6058 | * and snap ids - need to fill in names for those ids. |
| 6059 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6060 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6061 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6062 | else |
| 6063 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6064 | if (ret) { |
| 6065 | if (ret == -ENOENT) |
| 6066 | pr_info("snap %s/%s@%s does not exist\n", |
| 6067 | rbd_dev->spec->pool_name, |
| 6068 | rbd_dev->spec->image_name, |
| 6069 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6070 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6071 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6072 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6073 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6074 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6075 | if (ret) |
| 6076 | goto err_out_probe; |
| 6077 | |
| 6078 | /* |
| 6079 | * Need to warn users if this image is the one being |
| 6080 | * mapped and has a parent. |
| 6081 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6082 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6083 | rbd_warn(rbd_dev, |
| 6084 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6085 | } |
| 6086 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6087 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6088 | if (ret) |
| 6089 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6090 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6091 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6092 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6093 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6094 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6095 | err_out_probe: |
| 6096 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6097 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6098 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6099 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6100 | err_out_format: |
| 6101 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6102 | kfree(rbd_dev->spec->image_id); |
| 6103 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6104 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6105 | } |
| 6106 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6107 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6108 | const char *buf, |
| 6109 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6110 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6111 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6112 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6113 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6114 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6115 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6116 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6117 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6118 | |
| 6119 | if (!try_module_get(THIS_MODULE)) |
| 6120 | return -ENODEV; |
| 6121 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6122 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6123 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6124 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6125 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6126 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6127 | rbdc = rbd_get_client(ceph_opts); |
| 6128 | if (IS_ERR(rbdc)) { |
| 6129 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6130 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6131 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6132 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6133 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6134 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6135 | if (rc < 0) { |
| 6136 | if (rc == -ENOENT) |
| 6137 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6138 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6139 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6140 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6141 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6142 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6143 | if (!rbd_dev) { |
| 6144 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6145 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6146 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6147 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6148 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6149 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6150 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6151 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6152 | if (!rbd_dev->config_info) { |
| 6153 | rc = -ENOMEM; |
| 6154 | goto err_out_rbd_dev; |
| 6155 | } |
| 6156 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6157 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6158 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6159 | if (rc < 0) { |
| 6160 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6161 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6162 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6163 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6164 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6165 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6166 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6167 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6168 | read_only = true; |
| 6169 | rbd_dev->mapping.read_only = read_only; |
| 6170 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6171 | rc = rbd_dev_device_setup(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6172 | if (rc) { |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6173 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6174 | * rbd_unregister_watch() can't be moved into |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6175 | * rbd_dev_image_release() without refactoring, see |
| 6176 | * commit 1f3ef78861ac. |
| 6177 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6178 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6179 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6180 | goto out; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6181 | } |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6182 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6183 | rc = count; |
| 6184 | out: |
| 6185 | module_put(THIS_MODULE); |
| 6186 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6187 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6188 | err_out_rbd_dev: |
| 6189 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6190 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6191 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6192 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6193 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6194 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6195 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6196 | } |
| 6197 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6198 | static ssize_t rbd_add(struct bus_type *bus, |
| 6199 | const char *buf, |
| 6200 | size_t count) |
| 6201 | { |
| 6202 | if (single_major) |
| 6203 | return -EINVAL; |
| 6204 | |
| 6205 | return do_rbd_add(bus, buf, count); |
| 6206 | } |
| 6207 | |
| 6208 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6209 | const char *buf, |
| 6210 | size_t count) |
| 6211 | { |
| 6212 | return do_rbd_add(bus, buf, count); |
| 6213 | } |
| 6214 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6215 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6216 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6217 | rbd_free_disk(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6218 | |
| 6219 | spin_lock(&rbd_dev_list_lock); |
| 6220 | list_del_init(&rbd_dev->node); |
| 6221 | spin_unlock(&rbd_dev_list_lock); |
| 6222 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6223 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6224 | device_del(&rbd_dev->dev); |
Alex Elder | 6d80b13 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6225 | rbd_dev_mapping_clear(rbd_dev); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6226 | if (!single_major) |
| 6227 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6228 | } |
| 6229 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6230 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6231 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6232 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6233 | struct rbd_device *first = rbd_dev; |
| 6234 | struct rbd_device *second = first->parent; |
| 6235 | struct rbd_device *third; |
| 6236 | |
| 6237 | /* |
| 6238 | * Follow to the parent with no grandparent and |
| 6239 | * remove it. |
| 6240 | */ |
| 6241 | while (second && (third = second->parent)) { |
| 6242 | first = second; |
| 6243 | second = third; |
| 6244 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6245 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6246 | rbd_dev_image_release(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6247 | first->parent = NULL; |
| 6248 | first->parent_overlap = 0; |
| 6249 | |
| 6250 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6251 | rbd_spec_put(first->parent_spec); |
| 6252 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6253 | } |
| 6254 | } |
| 6255 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6256 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6257 | const char *buf, |
| 6258 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6259 | { |
| 6260 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6261 | struct list_head *tmp; |
| 6262 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6263 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6264 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6265 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6266 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6267 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6268 | dev_id = -1; |
| 6269 | opt_buf[0] = '\0'; |
| 6270 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6271 | if (dev_id < 0) { |
| 6272 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6273 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6274 | } |
| 6275 | if (opt_buf[0] != '\0') { |
| 6276 | if (!strcmp(opt_buf, "force")) { |
| 6277 | force = true; |
| 6278 | } else { |
| 6279 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6280 | return -EINVAL; |
| 6281 | } |
| 6282 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6283 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6284 | ret = -ENOENT; |
| 6285 | spin_lock(&rbd_dev_list_lock); |
| 6286 | list_for_each(tmp, &rbd_dev_list) { |
| 6287 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6288 | if (rbd_dev->dev_id == dev_id) { |
| 6289 | ret = 0; |
| 6290 | break; |
| 6291 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6292 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6293 | if (!ret) { |
| 6294 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6295 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6296 | ret = -EBUSY; |
| 6297 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6298 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6299 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6300 | spin_unlock_irq(&rbd_dev->lock); |
| 6301 | } |
| 6302 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6303 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6304 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6305 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6306 | if (force) { |
| 6307 | /* |
| 6308 | * Prevent new IO from being queued and wait for existing |
| 6309 | * IO to complete/fail. |
| 6310 | */ |
| 6311 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6312 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6313 | } |
| 6314 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6315 | down_write(&rbd_dev->lock_rwsem); |
| 6316 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6317 | rbd_unlock(rbd_dev); |
| 6318 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6319 | rbd_unregister_watch(rbd_dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6320 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 6321 | /* |
| 6322 | * Don't free anything from rbd_dev->disk until after all |
| 6323 | * notifies are completely processed. Otherwise |
| 6324 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting |
| 6325 | * in a potential use after free of rbd_dev->disk or rbd_dev. |
| 6326 | */ |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6327 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6328 | rbd_dev_image_release(rbd_dev); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 6329 | |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6330 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6331 | } |
| 6332 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6333 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6334 | const char *buf, |
| 6335 | size_t count) |
| 6336 | { |
| 6337 | if (single_major) |
| 6338 | return -EINVAL; |
| 6339 | |
| 6340 | return do_rbd_remove(bus, buf, count); |
| 6341 | } |
| 6342 | |
| 6343 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6344 | const char *buf, |
| 6345 | size_t count) |
| 6346 | { |
| 6347 | return do_rbd_remove(bus, buf, count); |
| 6348 | } |
| 6349 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6350 | /* |
| 6351 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6352 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6353 | */ |
| 6354 | static int rbd_sysfs_init(void) |
| 6355 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6356 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6357 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6358 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6359 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6360 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6361 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6362 | ret = bus_register(&rbd_bus_type); |
| 6363 | if (ret < 0) |
| 6364 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6365 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6366 | return ret; |
| 6367 | } |
| 6368 | |
| 6369 | static void rbd_sysfs_cleanup(void) |
| 6370 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6371 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6372 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6373 | } |
| 6374 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6375 | static int rbd_slab_init(void) |
| 6376 | { |
| 6377 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6378 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6379 | if (!rbd_img_request_cache) |
| 6380 | return -ENOMEM; |
| 6381 | |
| 6382 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6383 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6384 | if (!rbd_obj_request_cache) |
| 6385 | goto out_err; |
| 6386 | |
| 6387 | rbd_assert(!rbd_segment_name_cache); |
| 6388 | rbd_segment_name_cache = kmem_cache_create("rbd_segment_name", |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 6389 | CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6390 | if (rbd_segment_name_cache) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6391 | return 0; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6392 | out_err: |
Julia Lawall | 13bf283 | 2015-09-13 14:15:26 +0200 | [diff] [blame] | 6393 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6394 | rbd_obj_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6395 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6396 | kmem_cache_destroy(rbd_img_request_cache); |
| 6397 | rbd_img_request_cache = NULL; |
| 6398 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6399 | return -ENOMEM; |
| 6400 | } |
| 6401 | |
| 6402 | static void rbd_slab_exit(void) |
| 6403 | { |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6404 | rbd_assert(rbd_segment_name_cache); |
| 6405 | kmem_cache_destroy(rbd_segment_name_cache); |
| 6406 | rbd_segment_name_cache = NULL; |
| 6407 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6408 | rbd_assert(rbd_obj_request_cache); |
| 6409 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6410 | rbd_obj_request_cache = NULL; |
| 6411 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6412 | rbd_assert(rbd_img_request_cache); |
| 6413 | kmem_cache_destroy(rbd_img_request_cache); |
| 6414 | rbd_img_request_cache = NULL; |
| 6415 | } |
| 6416 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6417 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6418 | { |
| 6419 | int rc; |
| 6420 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6421 | if (!libceph_compatible(NULL)) { |
| 6422 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6423 | return -EINVAL; |
| 6424 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6425 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6426 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6427 | if (rc) |
| 6428 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6429 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6430 | /* |
| 6431 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6432 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6433 | */ |
| 6434 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6435 | if (!rbd_wq) { |
| 6436 | rc = -ENOMEM; |
| 6437 | goto err_out_slab; |
| 6438 | } |
| 6439 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6440 | if (single_major) { |
| 6441 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6442 | if (rbd_major < 0) { |
| 6443 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6444 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6445 | } |
| 6446 | } |
| 6447 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6448 | rc = rbd_sysfs_init(); |
| 6449 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6450 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6451 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6452 | if (single_major) |
| 6453 | pr_info("loaded (major %d)\n", rbd_major); |
| 6454 | else |
| 6455 | pr_info("loaded\n"); |
| 6456 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6457 | return 0; |
| 6458 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6459 | err_out_blkdev: |
| 6460 | if (single_major) |
| 6461 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6462 | err_out_wq: |
| 6463 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6464 | err_out_slab: |
| 6465 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6466 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6467 | } |
| 6468 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6469 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6470 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6471 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6472 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6473 | if (single_major) |
| 6474 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6475 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6476 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | module_init(rbd_init); |
| 6480 | module_exit(rbd_exit); |
| 6481 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6482 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6483 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6484 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6485 | /* following authorship retained from original osdblk.c */ |
| 6486 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6487 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6488 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6489 | MODULE_LICENSE("GPL"); |