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 | ecb4dc2 | 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 | ecb4dc2 | 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 | ecb4dc2 | 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; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 235 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 236 | u64 offset; /* object start byte */ |
| 237 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 238 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 239 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 240 | /* |
| 241 | * An object request associated with an image will have its |
| 242 | * img_data flag set; a standalone object request will not. |
| 243 | * |
| 244 | * A standalone object request will have which == BAD_WHICH |
| 245 | * and a null obj_request pointer. |
| 246 | * |
| 247 | * An object request initiated in support of a layered image |
| 248 | * object (to check for its existence before a write) will |
| 249 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 250 | * |
| 251 | * Finally, an object request for rbd image data will have |
| 252 | * which != BAD_WHICH, and will have a non-null img_request |
| 253 | * pointer. The value of which will be in the range |
| 254 | * 0..(img_request->obj_request_count-1). |
| 255 | */ |
| 256 | union { |
| 257 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 258 | struct { |
| 259 | struct rbd_img_request *img_request; |
| 260 | u64 img_offset; |
| 261 | /* links for img_request->obj_requests list */ |
| 262 | struct list_head links; |
| 263 | }; |
| 264 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 265 | u32 which; /* posn image request list */ |
| 266 | |
| 267 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 268 | union { |
| 269 | struct bio *bio_list; |
| 270 | struct { |
| 271 | struct page **pages; |
| 272 | u32 page_count; |
| 273 | }; |
| 274 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 275 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 276 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 277 | |
| 278 | struct ceph_osd_request *osd_req; |
| 279 | |
| 280 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 281 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 282 | |
| 283 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 284 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 285 | |
| 286 | struct kref kref; |
| 287 | }; |
| 288 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 289 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 290 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 291 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 292 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 293 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 294 | }; |
| 295 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 297 | struct rbd_device *rbd_dev; |
| 298 | u64 offset; /* starting image byte offset */ |
| 299 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 300 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 301 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 302 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 303 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 304 | }; |
| 305 | union { |
| 306 | struct request *rq; /* block request */ |
| 307 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 308 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 309 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 310 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 311 | spinlock_t completion_lock;/* protects next_completion */ |
| 312 | u32 next_completion; |
| 313 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 314 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 315 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 316 | |
| 317 | u32 obj_request_count; |
| 318 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 319 | |
| 320 | struct kref kref; |
| 321 | }; |
| 322 | |
| 323 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 324 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 325 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 326 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 327 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 328 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 329 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 330 | enum rbd_watch_state { |
| 331 | RBD_WATCH_STATE_UNREGISTERED, |
| 332 | RBD_WATCH_STATE_REGISTERED, |
| 333 | RBD_WATCH_STATE_ERROR, |
| 334 | }; |
| 335 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 336 | enum rbd_lock_state { |
| 337 | RBD_LOCK_STATE_UNLOCKED, |
| 338 | RBD_LOCK_STATE_LOCKED, |
| 339 | RBD_LOCK_STATE_RELEASING, |
| 340 | }; |
| 341 | |
| 342 | /* WatchNotify::ClientId */ |
| 343 | struct rbd_client_id { |
| 344 | u64 gid; |
| 345 | u64 handle; |
| 346 | }; |
| 347 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 348 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 349 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 350 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 351 | bool read_only; |
| 352 | }; |
| 353 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 354 | /* |
| 355 | * a single device |
| 356 | */ |
| 357 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 358 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 359 | |
| 360 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 361 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 362 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 363 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 364 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 365 | struct rbd_client *rbd_client; |
| 366 | |
| 367 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 368 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 369 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 370 | |
| 371 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 372 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 373 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 374 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 375 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 376 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 377 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 378 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 379 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 380 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 381 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 382 | struct mutex watch_mutex; |
| 383 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 384 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 385 | u64 watch_cookie; |
| 386 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 387 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 388 | struct rw_semaphore lock_rwsem; |
| 389 | enum rbd_lock_state lock_state; |
| 390 | struct rbd_client_id owner_cid; |
| 391 | struct work_struct acquired_lock_work; |
| 392 | struct work_struct released_lock_work; |
| 393 | struct delayed_work lock_dwork; |
| 394 | struct work_struct unlock_work; |
| 395 | wait_queue_head_t lock_waitq; |
| 396 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 397 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 398 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 399 | struct rbd_spec *parent_spec; |
| 400 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 401 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 402 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 403 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 404 | /* Block layer tags. */ |
| 405 | struct blk_mq_tag_set tag_set; |
| 406 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 407 | /* protects updating the header */ |
| 408 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 409 | |
| 410 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 411 | |
| 412 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 413 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 414 | /* sysfs related */ |
| 415 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 416 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 417 | }; |
| 418 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 419 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 420 | * Flag bits for rbd_dev->flags: |
| 421 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 422 | * by rbd_dev->lock |
| 423 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 424 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 425 | enum rbd_dev_flags { |
| 426 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 427 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 428 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 429 | }; |
| 430 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 431 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 432 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 433 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 434 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 435 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 436 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 437 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 438 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 439 | /* Slab caches for frequently-allocated structures */ |
| 440 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 441 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 442 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 443 | static struct kmem_cache *rbd_segment_name_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 444 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 445 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 446 | static DEFINE_IDA(rbd_dev_id_ida); |
| 447 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 448 | static struct workqueue_struct *rbd_wq; |
| 449 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 450 | /* |
| 451 | * Default to false for now, as single-major requires >= 0.75 version of |
| 452 | * userspace rbd utility. |
| 453 | */ |
| 454 | static bool single_major = false; |
| 455 | module_param(single_major, bool, S_IRUGO); |
| 456 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 457 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 458 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 459 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 460 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 461 | size_t count); |
| 462 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 463 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 464 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 465 | size_t count); |
| 466 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 467 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 468 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 469 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 470 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 471 | static int rbd_dev_id_to_minor(int dev_id) |
| 472 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 473 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | static int minor_to_rbd_dev_id(int minor) |
| 477 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 478 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 481 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
| 482 | { |
| 483 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 484 | rbd_dev->spec->snap_id == CEPH_NOSNAP && |
| 485 | !rbd_dev->mapping.read_only; |
| 486 | } |
| 487 | |
| 488 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 489 | { |
| 490 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 491 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 492 | } |
| 493 | |
| 494 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 495 | { |
| 496 | bool is_lock_owner; |
| 497 | |
| 498 | down_read(&rbd_dev->lock_rwsem); |
| 499 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 500 | up_read(&rbd_dev->lock_rwsem); |
| 501 | return is_lock_owner; |
| 502 | } |
| 503 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 504 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 505 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 506 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 507 | 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] | 508 | |
| 509 | static struct attribute *rbd_bus_attrs[] = { |
| 510 | &bus_attr_add.attr, |
| 511 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 512 | &bus_attr_add_single_major.attr, |
| 513 | &bus_attr_remove_single_major.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 514 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 515 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 516 | |
| 517 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 518 | struct attribute *attr, int index) |
| 519 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 520 | if (!single_major && |
| 521 | (attr == &bus_attr_add_single_major.attr || |
| 522 | attr == &bus_attr_remove_single_major.attr)) |
| 523 | return 0; |
| 524 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 525 | return attr->mode; |
| 526 | } |
| 527 | |
| 528 | static const struct attribute_group rbd_bus_group = { |
| 529 | .attrs = rbd_bus_attrs, |
| 530 | .is_visible = rbd_bus_is_visible, |
| 531 | }; |
| 532 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 533 | |
| 534 | static struct bus_type rbd_bus_type = { |
| 535 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 536 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | static void rbd_root_dev_release(struct device *dev) |
| 540 | { |
| 541 | } |
| 542 | |
| 543 | static struct device rbd_root_dev = { |
| 544 | .init_name = "rbd", |
| 545 | .release = rbd_root_dev_release, |
| 546 | }; |
| 547 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 548 | static __printf(2, 3) |
| 549 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 550 | { |
| 551 | struct va_format vaf; |
| 552 | va_list args; |
| 553 | |
| 554 | va_start(args, fmt); |
| 555 | vaf.fmt = fmt; |
| 556 | vaf.va = &args; |
| 557 | |
| 558 | if (!rbd_dev) |
| 559 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 560 | else if (rbd_dev->disk) |
| 561 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 562 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 563 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 564 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 565 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 566 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 567 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 568 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 569 | else /* punt */ |
| 570 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 571 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 572 | va_end(args); |
| 573 | } |
| 574 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 575 | #ifdef RBD_DEBUG |
| 576 | #define rbd_assert(expr) \ |
| 577 | if (unlikely(!(expr))) { \ |
| 578 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 579 | "at line %d:\n\n" \ |
| 580 | "\trbd_assert(%s);\n\n", \ |
| 581 | __func__, __LINE__, #expr); \ |
| 582 | BUG(); \ |
| 583 | } |
| 584 | #else /* !RBD_DEBUG */ |
| 585 | # define rbd_assert(expr) ((void) 0) |
| 586 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 587 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 588 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 589 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 590 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 591 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 592 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 593 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 594 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 595 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 596 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 597 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 598 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 599 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 600 | u8 *order, u64 *snap_size); |
| 601 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 602 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 603 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 604 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 605 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 606 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 607 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 608 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 609 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 610 | return -EROFS; |
| 611 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 612 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 613 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 614 | removing = true; |
| 615 | else |
| 616 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 617 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 618 | if (removing) |
| 619 | return -ENOENT; |
| 620 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 621 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 622 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 626 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 627 | { |
| 628 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 629 | unsigned long open_count_before; |
| 630 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 633 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 634 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 636 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 639 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 640 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 641 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | int val; |
| 643 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 644 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 646 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 647 | if (get_user(val, (int __user *)(arg))) |
| 648 | return -EFAULT; |
| 649 | |
| 650 | ro = val ? true : false; |
| 651 | /* Snapshot doesn't allow to write*/ |
| 652 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 653 | return -EROFS; |
| 654 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 655 | spin_lock_irq(&rbd_dev->lock); |
| 656 | /* prevent others open this device */ |
| 657 | if (rbd_dev->open_count > 1) { |
| 658 | ret = -EBUSY; |
| 659 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 660 | } |
| 661 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 662 | if (rbd_dev->mapping.read_only != ro) { |
| 663 | rbd_dev->mapping.read_only = ro; |
| 664 | ro_changed = true; |
| 665 | } |
| 666 | |
| 667 | out: |
| 668 | spin_unlock_irq(&rbd_dev->lock); |
| 669 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 670 | if (ret == 0 && ro_changed) |
| 671 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 672 | |
| 673 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 677 | unsigned int cmd, unsigned long arg) |
| 678 | { |
| 679 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 680 | int ret = 0; |
| 681 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 682 | switch (cmd) { |
| 683 | case BLKROSET: |
| 684 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 685 | break; |
| 686 | default: |
| 687 | ret = -ENOTTY; |
| 688 | } |
| 689 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 690 | return ret; |
| 691 | } |
| 692 | |
| 693 | #ifdef CONFIG_COMPAT |
| 694 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 695 | unsigned int cmd, unsigned long arg) |
| 696 | { |
| 697 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 698 | } |
| 699 | #endif /* CONFIG_COMPAT */ |
| 700 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 701 | static const struct block_device_operations rbd_bd_ops = { |
| 702 | .owner = THIS_MODULE, |
| 703 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 704 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 705 | .ioctl = rbd_ioctl, |
| 706 | #ifdef CONFIG_COMPAT |
| 707 | .compat_ioctl = rbd_compat_ioctl, |
| 708 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 709 | }; |
| 710 | |
| 711 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 712 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 713 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 715 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 716 | { |
| 717 | struct rbd_client *rbdc; |
| 718 | int ret = -ENOMEM; |
| 719 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 720 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 722 | if (!rbdc) |
| 723 | goto out_opt; |
| 724 | |
| 725 | kref_init(&rbdc->kref); |
| 726 | INIT_LIST_HEAD(&rbdc->node); |
| 727 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 728 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 729 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 730 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 731 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 732 | |
| 733 | ret = ceph_open_session(rbdc->client); |
| 734 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 735 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 736 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 737 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 738 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 739 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 740 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 741 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 742 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 743 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 744 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 745 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 746 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 747 | kfree(rbdc); |
| 748 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 749 | if (ceph_opts) |
| 750 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 751 | dout("%s: error %d\n", __func__, ret); |
| 752 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 753 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 756 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 757 | { |
| 758 | kref_get(&rbdc->kref); |
| 759 | |
| 760 | return rbdc; |
| 761 | } |
| 762 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 763 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 764 | * Find a ceph client with specific addr and configuration. If |
| 765 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 766 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 767 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 768 | { |
| 769 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 770 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 771 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 772 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 773 | return NULL; |
| 774 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 775 | spin_lock(&rbd_client_list_lock); |
| 776 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 777 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 778 | __rbd_get_client(client_node); |
| 779 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 780 | found = true; |
| 781 | break; |
| 782 | } |
| 783 | } |
| 784 | spin_unlock(&rbd_client_list_lock); |
| 785 | |
| 786 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 790 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 791 | */ |
| 792 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 793 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | Opt_last_int, |
| 795 | /* int args above */ |
| 796 | Opt_last_string, |
| 797 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 798 | Opt_read_only, |
| 799 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 800 | Opt_lock_on_read, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 801 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 802 | }; |
| 803 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 804 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 805 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 806 | /* int args above */ |
| 807 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 808 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 809 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 810 | {Opt_read_write, "read_write"}, |
| 811 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 812 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 813 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 814 | }; |
| 815 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 816 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 817 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 818 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 819 | bool lock_on_read; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 820 | }; |
| 821 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 822 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 823 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 824 | #define RBD_LOCK_ON_READ_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 825 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | static int parse_rbd_opts_token(char *c, void *private) |
| 827 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 828 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 829 | substring_t argstr[MAX_OPT_ARGS]; |
| 830 | int token, intval, ret; |
| 831 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 832 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 833 | if (token < Opt_last_int) { |
| 834 | ret = match_int(&argstr[0], &intval); |
| 835 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 836 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 837 | return ret; |
| 838 | } |
| 839 | dout("got int token %d val %d\n", token, intval); |
| 840 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 841 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 842 | } else { |
| 843 | dout("got token %d\n", token); |
| 844 | } |
| 845 | |
| 846 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 847 | case Opt_queue_depth: |
| 848 | if (intval < 1) { |
| 849 | pr_err("queue_depth out of range\n"); |
| 850 | return -EINVAL; |
| 851 | } |
| 852 | rbd_opts->queue_depth = intval; |
| 853 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 854 | case Opt_read_only: |
| 855 | rbd_opts->read_only = true; |
| 856 | break; |
| 857 | case Opt_read_write: |
| 858 | rbd_opts->read_only = false; |
| 859 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 860 | case Opt_lock_on_read: |
| 861 | rbd_opts->lock_on_read = true; |
| 862 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 863 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 864 | /* libceph prints "bad option" msg */ |
| 865 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 866 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 867 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 871 | static char* obj_op_name(enum obj_operation_type op_type) |
| 872 | { |
| 873 | switch (op_type) { |
| 874 | case OBJ_OP_READ: |
| 875 | return "read"; |
| 876 | case OBJ_OP_WRITE: |
| 877 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 878 | case OBJ_OP_DISCARD: |
| 879 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 880 | default: |
| 881 | return "???"; |
| 882 | } |
| 883 | } |
| 884 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 885 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 886 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 887 | * not exist create it. Either way, ceph_opts is consumed by this |
| 888 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 890 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 891 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 892 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 893 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 894 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 895 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 896 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 897 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 898 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 899 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 900 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 902 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* |
| 906 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 907 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 908 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 909 | */ |
| 910 | static void rbd_client_release(struct kref *kref) |
| 911 | { |
| 912 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 913 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 914 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 915 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 917 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 918 | |
| 919 | ceph_destroy_client(rbdc->client); |
| 920 | kfree(rbdc); |
| 921 | } |
| 922 | |
| 923 | /* |
| 924 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 925 | * it. |
| 926 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 927 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 928 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 929 | if (rbdc) |
| 930 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 931 | } |
| 932 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 933 | static bool rbd_image_format_valid(u32 image_format) |
| 934 | { |
| 935 | return image_format == 1 || image_format == 2; |
| 936 | } |
| 937 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 938 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 939 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 940 | size_t size; |
| 941 | u32 snap_count; |
| 942 | |
| 943 | /* The header has to start with the magic rbd header text */ |
| 944 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 945 | return false; |
| 946 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 947 | /* The bio layer requires at least sector-sized I/O */ |
| 948 | |
| 949 | if (ondisk->options.order < SECTOR_SHIFT) |
| 950 | return false; |
| 951 | |
| 952 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 953 | |
| 954 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 955 | return false; |
| 956 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 957 | /* |
| 958 | * The size of a snapshot header has to fit in a size_t, and |
| 959 | * that limits the number of snapshots. |
| 960 | */ |
| 961 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 962 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 963 | if (snap_count > size / sizeof (__le64)) |
| 964 | return false; |
| 965 | |
| 966 | /* |
| 967 | * Not only that, but the size of the entire the snapshot |
| 968 | * header must also be representable in a size_t. |
| 969 | */ |
| 970 | size -= snap_count * sizeof (__le64); |
| 971 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 972 | return false; |
| 973 | |
| 974 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 975 | } |
| 976 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 977 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 978 | * returns the size of an object in the image |
| 979 | */ |
| 980 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 981 | { |
| 982 | return 1U << header->obj_order; |
| 983 | } |
| 984 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 985 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 986 | { |
| 987 | if (rbd_dev->header.stripe_unit == 0 || |
| 988 | rbd_dev->header.stripe_count == 0) { |
| 989 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 990 | rbd_dev->header.stripe_count = 1; |
| 991 | } |
| 992 | |
| 993 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 994 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 995 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 996 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 997 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 998 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 999 | } |
| 1000 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1001 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1002 | * Fill an rbd image header with information from the given format 1 |
| 1003 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1004 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1005 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1006 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1007 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1008 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | bool first_time = header->object_prefix == NULL; |
| 1010 | struct ceph_snap_context *snapc; |
| 1011 | char *object_prefix = NULL; |
| 1012 | char *snap_names = NULL; |
| 1013 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1014 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1015 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1016 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1017 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1018 | /* Allocate this now to avoid having to handle failure below */ |
| 1019 | |
| 1020 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1021 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1022 | sizeof(ondisk->object_prefix), |
| 1023 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | if (!object_prefix) |
| 1025 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1029 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1030 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1031 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1032 | if (!snapc) |
| 1033 | goto out_err; |
| 1034 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1035 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1036 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1037 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1038 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1039 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1040 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1041 | if (snap_names_len > (u64)SIZE_MAX) |
| 1042 | goto out_2big; |
| 1043 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1044 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1045 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | |
| 1047 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1048 | snap_sizes = kmalloc_array(snap_count, |
| 1049 | sizeof(*header->snap_sizes), |
| 1050 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1051 | if (!snap_sizes) |
| 1052 | goto out_err; |
| 1053 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1054 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1055 | * Copy the names, and fill in each snapshot's id |
| 1056 | * and size. |
| 1057 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1058 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1060 | * snap_names_len bytes beyond the end of the |
| 1061 | * snapshot id array, this memcpy() is safe. |
| 1062 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1063 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1064 | snaps = ondisk->snaps; |
| 1065 | for (i = 0; i < snap_count; i++) { |
| 1066 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1067 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1068 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1069 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1070 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1071 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1072 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1073 | if (first_time) { |
| 1074 | header->object_prefix = object_prefix; |
| 1075 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1076 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1077 | } else { |
| 1078 | ceph_put_snap_context(header->snapc); |
| 1079 | kfree(header->snap_names); |
| 1080 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1084 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1085 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1086 | header->snapc = snapc; |
| 1087 | header->snap_names = snap_names; |
| 1088 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1089 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1090 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1091 | out_2big: |
| 1092 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1093 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1094 | kfree(snap_sizes); |
| 1095 | kfree(snap_names); |
| 1096 | ceph_put_snap_context(snapc); |
| 1097 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1098 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1099 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1102 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1103 | { |
| 1104 | const char *snap_name; |
| 1105 | |
| 1106 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1107 | |
| 1108 | /* Skip over names until we find the one we are looking for */ |
| 1109 | |
| 1110 | snap_name = rbd_dev->header.snap_names; |
| 1111 | while (which--) |
| 1112 | snap_name += strlen(snap_name) + 1; |
| 1113 | |
| 1114 | return kstrdup(snap_name, GFP_KERNEL); |
| 1115 | } |
| 1116 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1117 | /* |
| 1118 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1119 | * Note that result is for snapshots in *descending* order. |
| 1120 | */ |
| 1121 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1122 | { |
| 1123 | u64 snap_id1 = *(u64 *)s1; |
| 1124 | u64 snap_id2 = *(u64 *)s2; |
| 1125 | |
| 1126 | if (snap_id1 < snap_id2) |
| 1127 | return 1; |
| 1128 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Search a snapshot context to see if the given snapshot id is |
| 1133 | * present. |
| 1134 | * |
| 1135 | * Returns the position of the snapshot id in the array if it's found, |
| 1136 | * or BAD_SNAP_INDEX otherwise. |
| 1137 | * |
| 1138 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1139 | * reverse order, highest snapshot id first. |
| 1140 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1141 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1142 | { |
| 1143 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1144 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1145 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1146 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1147 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1148 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1149 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1150 | } |
| 1151 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1152 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1153 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1154 | { |
| 1155 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1156 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1157 | |
| 1158 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1159 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1160 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1161 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1162 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1163 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1166 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1167 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1168 | if (snap_id == CEPH_NOSNAP) |
| 1169 | return RBD_SNAP_HEAD_NAME; |
| 1170 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1171 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1172 | if (rbd_dev->image_format == 1) |
| 1173 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1174 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1175 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1178 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1179 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1180 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1181 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1182 | if (snap_id == CEPH_NOSNAP) { |
| 1183 | *snap_size = rbd_dev->header.image_size; |
| 1184 | } else if (rbd_dev->image_format == 1) { |
| 1185 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1186 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1187 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1188 | if (which == BAD_SNAP_INDEX) |
| 1189 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1190 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1191 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1192 | } else { |
| 1193 | u64 size = 0; |
| 1194 | int ret; |
| 1195 | |
| 1196 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1197 | if (ret) |
| 1198 | return ret; |
| 1199 | |
| 1200 | *snap_size = size; |
| 1201 | } |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1206 | u64 *snap_features) |
| 1207 | { |
| 1208 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1209 | if (snap_id == CEPH_NOSNAP) { |
| 1210 | *snap_features = rbd_dev->header.features; |
| 1211 | } else if (rbd_dev->image_format == 1) { |
| 1212 | *snap_features = 0; /* No features for format 1 */ |
| 1213 | } else { |
| 1214 | u64 features = 0; |
| 1215 | int ret; |
| 1216 | |
| 1217 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1218 | if (ret) |
| 1219 | return ret; |
| 1220 | |
| 1221 | *snap_features = features; |
| 1222 | } |
| 1223 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1226 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1227 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1228 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1229 | u64 size = 0; |
| 1230 | u64 features = 0; |
| 1231 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1232 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1233 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1234 | if (ret) |
| 1235 | return ret; |
| 1236 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1237 | if (ret) |
| 1238 | return ret; |
| 1239 | |
| 1240 | rbd_dev->mapping.size = size; |
| 1241 | rbd_dev->mapping.features = features; |
| 1242 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1243 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1246 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1247 | { |
| 1248 | rbd_dev->mapping.size = 0; |
| 1249 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1250 | } |
| 1251 | |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1252 | static void rbd_segment_name_free(const char *name) |
| 1253 | { |
| 1254 | /* The explicit cast here is needed to drop the const qualifier */ |
| 1255 | |
| 1256 | kmem_cache_free(rbd_segment_name_cache, (void *)name); |
| 1257 | } |
| 1258 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 1259 | 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] | 1260 | { |
Ilya Dryomov | 223768d | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1261 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1262 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1263 | char *name; |
| 1264 | u64 segment; |
| 1265 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1266 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 1267 | name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1268 | if (!name) |
| 1269 | return NULL; |
| 1270 | segment = offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1271 | ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format, |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1272 | rbd_dev->header.object_prefix, segment); |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1273 | if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1274 | pr_err("error formatting segment name for #%llu (%d)\n", |
| 1275 | segment, ret); |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1276 | rbd_segment_name_free(name); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1277 | name = NULL; |
| 1278 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1279 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1280 | return name; |
| 1281 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1282 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1283 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1284 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1285 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1286 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1287 | return offset & (segment_size - 1); |
| 1288 | } |
| 1289 | |
| 1290 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1291 | u64 offset, u64 length) |
| 1292 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1293 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1294 | |
| 1295 | offset &= segment_size - 1; |
| 1296 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1297 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1298 | if (offset + length > segment_size) |
| 1299 | length = segment_size - offset; |
| 1300 | |
| 1301 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * bio helpers |
| 1306 | */ |
| 1307 | |
| 1308 | static void bio_chain_put(struct bio *chain) |
| 1309 | { |
| 1310 | struct bio *tmp; |
| 1311 | |
| 1312 | while (chain) { |
| 1313 | tmp = chain; |
| 1314 | chain = chain->bi_next; |
| 1315 | bio_put(tmp); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | /* |
| 1320 | * zeros a bio chain, starting at specific offset |
| 1321 | */ |
| 1322 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1323 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1324 | struct bio_vec bv; |
| 1325 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1326 | unsigned long flags; |
| 1327 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1328 | int pos = 0; |
| 1329 | |
| 1330 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1331 | bio_for_each_segment(bv, chain, iter) { |
| 1332 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1333 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1334 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1335 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1336 | bv.bv_len - remainder); |
| 1337 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1338 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1339 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1340 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | chain = chain->bi_next; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1348 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1349 | * starting at the given byte offset from the start of the array and |
| 1350 | * continuing up to the given end offset. The pages array is |
| 1351 | * assumed to be big enough to hold all bytes up to the end. |
| 1352 | */ |
| 1353 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1354 | { |
| 1355 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1356 | |
| 1357 | rbd_assert(end > offset); |
| 1358 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1359 | while (offset < end) { |
| 1360 | size_t page_offset; |
| 1361 | size_t length; |
| 1362 | unsigned long flags; |
| 1363 | void *kaddr; |
| 1364 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1365 | page_offset = offset & ~PAGE_MASK; |
| 1366 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1367 | local_irq_save(flags); |
| 1368 | kaddr = kmap_atomic(*page); |
| 1369 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1370 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1371 | kunmap_atomic(kaddr); |
| 1372 | local_irq_restore(flags); |
| 1373 | |
| 1374 | offset += length; |
| 1375 | page++; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1380 | * Clone a portion of a bio, starting at the given byte offset |
| 1381 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1382 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1383 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1384 | unsigned int offset, |
| 1385 | unsigned int len, |
| 1386 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1387 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1388 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1389 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1390 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1391 | if (!bio) |
| 1392 | return NULL; /* ENOMEM */ |
| 1393 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1394 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1395 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1396 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1397 | return bio; |
| 1398 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1399 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1400 | /* |
| 1401 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1402 | * into the first bio in the source chain and continuing for the |
| 1403 | * number of bytes indicated. The result is another bio chain of |
| 1404 | * exactly the given length, or a null pointer on error. |
| 1405 | * |
| 1406 | * The bio_src and offset parameters are both in-out. On entry they |
| 1407 | * refer to the first source bio and the offset into that bio where |
| 1408 | * the start of data to be cloned is located. |
| 1409 | * |
| 1410 | * On return, bio_src is updated to refer to the bio in the source |
| 1411 | * chain that contains first un-cloned byte, and *offset will |
| 1412 | * contain the offset of that byte within that bio. |
| 1413 | */ |
| 1414 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1415 | unsigned int *offset, |
| 1416 | unsigned int len, |
| 1417 | gfp_t gfpmask) |
| 1418 | { |
| 1419 | struct bio *bi = *bio_src; |
| 1420 | unsigned int off = *offset; |
| 1421 | struct bio *chain = NULL; |
| 1422 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1423 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1424 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1425 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1426 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1427 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1428 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1429 | end = &chain; |
| 1430 | while (len) { |
| 1431 | unsigned int bi_size; |
| 1432 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1433 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1434 | if (!bi) { |
| 1435 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1436 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1437 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1438 | 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] | 1439 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1440 | if (!bio) |
| 1441 | goto out_err; /* ENOMEM */ |
| 1442 | |
| 1443 | *end = bio; |
| 1444 | end = &bio->bi_next; |
| 1445 | |
| 1446 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1447 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1448 | bi = bi->bi_next; |
| 1449 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1450 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1451 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1452 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1453 | *bio_src = bi; |
| 1454 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1455 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1456 | return chain; |
| 1457 | out_err: |
| 1458 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1459 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1460 | return NULL; |
| 1461 | } |
| 1462 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1463 | /* |
| 1464 | * The default/initial value for all object request flags is 0. For |
| 1465 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1466 | * again. |
| 1467 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1468 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1469 | { |
| 1470 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1471 | struct rbd_device *rbd_dev; |
| 1472 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1473 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1474 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1475 | obj_request); |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1480 | { |
| 1481 | smp_mb(); |
| 1482 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1483 | } |
| 1484 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1485 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1486 | { |
| 1487 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1488 | struct rbd_device *rbd_dev = NULL; |
| 1489 | |
| 1490 | if (obj_request_img_data_test(obj_request)) |
| 1491 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1492 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1493 | obj_request); |
| 1494 | } |
| 1495 | } |
| 1496 | |
| 1497 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1498 | { |
| 1499 | smp_mb(); |
| 1500 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1501 | } |
| 1502 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1503 | /* |
| 1504 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1505 | * flag. The latter is set based on the "exists" value provided. |
| 1506 | * |
| 1507 | * Note that for our purposes once an object exists it never goes |
| 1508 | * away again. It's possible that the response from two existence |
| 1509 | * checks are separated by the creation of the target object, and |
| 1510 | * the first ("doesn't exist") response arrives *after* the second |
| 1511 | * ("does exist"). In that case we ignore the second one. |
| 1512 | */ |
| 1513 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1514 | bool exists) |
| 1515 | { |
| 1516 | if (exists) |
| 1517 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1518 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1519 | smp_mb(); |
| 1520 | } |
| 1521 | |
| 1522 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1523 | { |
| 1524 | smp_mb(); |
| 1525 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1526 | } |
| 1527 | |
| 1528 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1529 | { |
| 1530 | smp_mb(); |
| 1531 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1532 | } |
| 1533 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1534 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1535 | { |
| 1536 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1537 | |
| 1538 | return obj_request->img_offset < |
| 1539 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1540 | } |
| 1541 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1542 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1543 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1544 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1545 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1546 | kref_get(&obj_request->kref); |
| 1547 | } |
| 1548 | |
| 1549 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1550 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1551 | { |
| 1552 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1553 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1554 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1555 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1556 | } |
| 1557 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1558 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1559 | { |
| 1560 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1561 | atomic_read(&img_request->kref.refcount)); |
| 1562 | kref_get(&img_request->kref); |
| 1563 | } |
| 1564 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1565 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1566 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1567 | static void rbd_img_request_destroy(struct kref *kref); |
| 1568 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1569 | { |
| 1570 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1571 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1572 | atomic_read(&img_request->kref.refcount)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1573 | if (img_request_child_test(img_request)) |
| 1574 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1575 | else |
| 1576 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1580 | struct rbd_obj_request *obj_request) |
| 1581 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1582 | rbd_assert(obj_request->img_request == NULL); |
| 1583 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1584 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1585 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1586 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1587 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1588 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1589 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1590 | img_request->obj_request_count++; |
| 1591 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1592 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1593 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1597 | struct rbd_obj_request *obj_request) |
| 1598 | { |
| 1599 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1600 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1601 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1602 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1603 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1604 | rbd_assert(img_request->obj_request_count > 0); |
| 1605 | img_request->obj_request_count--; |
| 1606 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1607 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1608 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1609 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1610 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1611 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1612 | rbd_obj_request_put(obj_request); |
| 1613 | } |
| 1614 | |
| 1615 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1616 | { |
| 1617 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1618 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1619 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1620 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1621 | return true; |
| 1622 | default: |
| 1623 | return false; |
| 1624 | } |
| 1625 | } |
| 1626 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1627 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1628 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1629 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1630 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1631 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1632 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 1633 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1634 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1635 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1636 | if (obj_request_img_data_test(obj_request)) { |
| 1637 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1638 | rbd_img_request_get(obj_request->img_request); |
| 1639 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1640 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1644 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1645 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1646 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1647 | |
| 1648 | /* |
| 1649 | * If no error occurred, compute the aggregate transfer |
| 1650 | * count for the image request. We could instead use |
| 1651 | * atomic64_cmpxchg() to update it as each object request |
| 1652 | * completes; not clear which way is better off hand. |
| 1653 | */ |
| 1654 | if (!img_request->result) { |
| 1655 | struct rbd_obj_request *obj_request; |
| 1656 | u64 xferred = 0; |
| 1657 | |
| 1658 | for_each_obj_request(img_request, obj_request) |
| 1659 | xferred += obj_request->xferred; |
| 1660 | img_request->xferred = xferred; |
| 1661 | } |
| 1662 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1663 | if (img_request->callback) |
| 1664 | img_request->callback(img_request); |
| 1665 | else |
| 1666 | rbd_img_request_put(img_request); |
| 1667 | } |
| 1668 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1669 | /* |
| 1670 | * The default/initial value for all image request flags is 0. Each |
| 1671 | * is conditionally set to 1 at image request initialization time |
| 1672 | * and currently never change thereafter. |
| 1673 | */ |
| 1674 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1675 | { |
| 1676 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1677 | smp_mb(); |
| 1678 | } |
| 1679 | |
| 1680 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1681 | { |
| 1682 | smp_mb(); |
| 1683 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1684 | } |
| 1685 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1686 | /* |
| 1687 | * Set the discard flag when the img_request is an discard request |
| 1688 | */ |
| 1689 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1690 | { |
| 1691 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1692 | smp_mb(); |
| 1693 | } |
| 1694 | |
| 1695 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1696 | { |
| 1697 | smp_mb(); |
| 1698 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1699 | } |
| 1700 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1701 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1702 | { |
| 1703 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1704 | smp_mb(); |
| 1705 | } |
| 1706 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1707 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1708 | { |
| 1709 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1710 | smp_mb(); |
| 1711 | } |
| 1712 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1713 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1714 | { |
| 1715 | smp_mb(); |
| 1716 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1717 | } |
| 1718 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1719 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1720 | { |
| 1721 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1722 | smp_mb(); |
| 1723 | } |
| 1724 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1725 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1726 | { |
| 1727 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1728 | smp_mb(); |
| 1729 | } |
| 1730 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1731 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1732 | { |
| 1733 | smp_mb(); |
| 1734 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1735 | } |
| 1736 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1737 | static enum obj_operation_type |
| 1738 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1739 | { |
| 1740 | if (img_request_write_test(img_request)) |
| 1741 | return OBJ_OP_WRITE; |
| 1742 | else if (img_request_discard_test(img_request)) |
| 1743 | return OBJ_OP_DISCARD; |
| 1744 | else |
| 1745 | return OBJ_OP_READ; |
| 1746 | } |
| 1747 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1748 | static void |
| 1749 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1750 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1751 | u64 xferred = obj_request->xferred; |
| 1752 | u64 length = obj_request->length; |
| 1753 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1754 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1755 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1756 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1757 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1758 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1759 | * length of the request. A short read also implies zero-fill |
| 1760 | * to the end of the request. An error requires the whole |
| 1761 | * length of the request to be reported finished with an error |
| 1762 | * to the block layer. In each case we update the xferred |
| 1763 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1764 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1765 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1766 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1767 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1768 | zero_bio_chain(obj_request->bio_list, 0); |
| 1769 | else |
| 1770 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1771 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1772 | } else if (xferred < length && !obj_request->result) { |
| 1773 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1774 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1775 | else |
| 1776 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1777 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1778 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1779 | obj_request_done_set(obj_request); |
| 1780 | } |
| 1781 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1782 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1783 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1784 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1785 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1786 | if (obj_request->callback) |
| 1787 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1788 | else |
| 1789 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1790 | } |
| 1791 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1792 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1793 | { |
| 1794 | obj_request->result = err; |
| 1795 | obj_request->xferred = 0; |
| 1796 | /* |
| 1797 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1798 | * rbd_img_obj_callback() |
| 1799 | */ |
| 1800 | if (obj_request_img_data_test(obj_request)) { |
| 1801 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1802 | rbd_img_request_get(obj_request->img_request); |
| 1803 | } |
| 1804 | obj_request_done_set(obj_request); |
| 1805 | rbd_obj_request_complete(obj_request); |
| 1806 | } |
| 1807 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1808 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1809 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1810 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1811 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1812 | bool layered = false; |
| 1813 | |
| 1814 | if (obj_request_img_data_test(obj_request)) { |
| 1815 | img_request = obj_request->img_request; |
| 1816 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1817 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1818 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1819 | |
| 1820 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1821 | obj_request, img_request, obj_request->result, |
| 1822 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1823 | if (layered && obj_request->result == -ENOENT && |
| 1824 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1825 | rbd_img_parent_read(obj_request); |
| 1826 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1827 | rbd_img_obj_request_read_callback(obj_request); |
| 1828 | else |
| 1829 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1830 | } |
| 1831 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1832 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1833 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1834 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1835 | obj_request->result, obj_request->length); |
| 1836 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1837 | * There is no such thing as a successful short write. Set |
| 1838 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1839 | */ |
| 1840 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1841 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1842 | } |
| 1843 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1844 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1845 | { |
| 1846 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1847 | obj_request->result, obj_request->length); |
| 1848 | /* |
| 1849 | * There is no such thing as a successful short discard. Set |
| 1850 | * it to our originally-requested length. |
| 1851 | */ |
| 1852 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1853 | /* discarding a non-existent object is not a problem */ |
| 1854 | if (obj_request->result == -ENOENT) |
| 1855 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1856 | obj_request_done_set(obj_request); |
| 1857 | } |
| 1858 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1859 | /* |
| 1860 | * For a simple stat call there's nothing to do. We'll do more if |
| 1861 | * this is part of a write sequence for a layered image. |
| 1862 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1863 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1864 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1865 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1866 | obj_request_done_set(obj_request); |
| 1867 | } |
| 1868 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1869 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1870 | { |
| 1871 | dout("%s: obj %p\n", __func__, obj_request); |
| 1872 | |
| 1873 | if (obj_request_img_data_test(obj_request)) |
| 1874 | rbd_osd_copyup_callback(obj_request); |
| 1875 | else |
| 1876 | obj_request_done_set(obj_request); |
| 1877 | } |
| 1878 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1879 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1880 | { |
| 1881 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1882 | u16 opcode; |
| 1883 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1884 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1885 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1886 | if (obj_request_img_data_test(obj_request)) { |
| 1887 | rbd_assert(obj_request->img_request); |
| 1888 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1889 | } else { |
| 1890 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1891 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1892 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1893 | if (osd_req->r_result < 0) |
| 1894 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1895 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1896 | /* |
| 1897 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1898 | * passed to the block layer, which just supports a 32-bit |
| 1899 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1900 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1901 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1902 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1903 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1904 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1905 | switch (opcode) { |
| 1906 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1907 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1908 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1909 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1910 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1911 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1912 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1913 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1914 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1915 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1916 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1917 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1918 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1919 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1920 | case CEPH_OSD_OP_DELETE: |
| 1921 | case CEPH_OSD_OP_TRUNCATE: |
| 1922 | case CEPH_OSD_OP_ZERO: |
| 1923 | rbd_osd_discard_callback(obj_request); |
| 1924 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1925 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1926 | rbd_osd_call_callback(obj_request); |
| 1927 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1928 | default: |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 1929 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
| 1930 | obj_request->object_no, opcode); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1931 | break; |
| 1932 | } |
| 1933 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1934 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1935 | rbd_obj_request_complete(obj_request); |
| 1936 | } |
| 1937 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1938 | 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] | 1939 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1940 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1941 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1942 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1943 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1944 | } |
| 1945 | |
| 1946 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1947 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1948 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1949 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1950 | osd_req->r_mtime = CURRENT_TIME; |
| 1951 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1952 | } |
| 1953 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1954 | static struct ceph_osd_request * |
| 1955 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1956 | struct ceph_snap_context *snapc, |
| 1957 | int num_ops, unsigned int flags, |
| 1958 | struct rbd_obj_request *obj_request) |
| 1959 | { |
| 1960 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1961 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 1962 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1963 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1964 | |
| 1965 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1966 | if (!req) |
| 1967 | return NULL; |
| 1968 | |
| 1969 | req->r_flags = flags; |
| 1970 | req->r_callback = rbd_osd_req_callback; |
| 1971 | req->r_priv = obj_request; |
| 1972 | |
| 1973 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 1974 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1975 | rbd_dev->header.object_prefix, obj_request->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1976 | goto err_req; |
| 1977 | |
| 1978 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1979 | goto err_req; |
| 1980 | |
| 1981 | return req; |
| 1982 | |
| 1983 | err_req: |
| 1984 | ceph_osdc_put_request(req); |
| 1985 | return NULL; |
| 1986 | } |
| 1987 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1988 | /* |
| 1989 | * Create an osd request. A read request has one osd op (read). |
| 1990 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1991 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1992 | * technically osd watch is a write request, hence this distinction.) |
| 1993 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1994 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1995 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1996 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1997 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1998 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1999 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2000 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2001 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2002 | if (obj_request_img_data_test(obj_request) && |
| 2003 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2004 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2005 | if (op_type == OBJ_OP_WRITE) { |
| 2006 | rbd_assert(img_request_write_test(img_request)); |
| 2007 | } else { |
| 2008 | rbd_assert(img_request_discard_test(img_request)); |
| 2009 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2010 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2011 | } |
| 2012 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2013 | 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] | 2014 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2015 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 2016 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
| 2017 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK : CEPH_OSD_FLAG_READ, |
| 2018 | obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2019 | } |
| 2020 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2021 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2022 | * Create a copyup osd request based on the information in the object |
| 2023 | * request supplied. A copyup request has two or three osd ops, a |
| 2024 | * copyup method call, potentially a hint op, and a write or truncate |
| 2025 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2026 | */ |
| 2027 | static struct ceph_osd_request * |
| 2028 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 2029 | { |
| 2030 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2031 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2032 | |
| 2033 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2034 | img_request = obj_request->img_request; |
| 2035 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2036 | rbd_assert(img_request_write_test(img_request) || |
| 2037 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2038 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2039 | if (img_request_discard_test(img_request)) |
| 2040 | num_osd_ops = 2; |
| 2041 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2042 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 2043 | img_request->snapc, num_osd_ops, |
| 2044 | CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK, |
| 2045 | obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2046 | } |
| 2047 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2048 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2049 | { |
| 2050 | ceph_osdc_put_request(osd_req); |
| 2051 | } |
| 2052 | |
| 2053 | /* object_name is assumed to be a non-null pointer and NUL-terminated */ |
| 2054 | |
| 2055 | 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] | 2056 | enum obj_request_type type) |
| 2057 | { |
| 2058 | struct rbd_obj_request *obj_request; |
| 2059 | size_t size; |
| 2060 | char *name; |
| 2061 | |
| 2062 | rbd_assert(obj_request_type_valid(type)); |
| 2063 | |
| 2064 | size = strlen(object_name) + 1; |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2065 | name = kmalloc(size, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2066 | if (!name) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2067 | return NULL; |
| 2068 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2069 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2070 | if (!obj_request) { |
| 2071 | kfree(name); |
| 2072 | return NULL; |
| 2073 | } |
| 2074 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2075 | obj_request->object_name = memcpy(name, object_name, size); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2076 | obj_request->which = BAD_WHICH; |
| 2077 | obj_request->type = type; |
| 2078 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2079 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2080 | kref_init(&obj_request->kref); |
| 2081 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2082 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2083 | return obj_request; |
| 2084 | } |
| 2085 | |
| 2086 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2087 | { |
| 2088 | struct rbd_obj_request *obj_request; |
| 2089 | |
| 2090 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2091 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2092 | dout("%s: obj %p\n", __func__, obj_request); |
| 2093 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2094 | rbd_assert(obj_request->img_request == NULL); |
| 2095 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2096 | |
| 2097 | if (obj_request->osd_req) |
| 2098 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2099 | |
| 2100 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2101 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2102 | case OBJ_REQUEST_NODATA: |
| 2103 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2104 | case OBJ_REQUEST_BIO: |
| 2105 | if (obj_request->bio_list) |
| 2106 | bio_chain_put(obj_request->bio_list); |
| 2107 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2108 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2109 | /* img_data requests don't own their page array */ |
| 2110 | if (obj_request->pages && |
| 2111 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2112 | ceph_release_page_vector(obj_request->pages, |
| 2113 | obj_request->page_count); |
| 2114 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2115 | } |
| 2116 | |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2117 | kfree(obj_request->object_name); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2118 | obj_request->object_name = NULL; |
| 2119 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2120 | } |
| 2121 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2122 | /* It's OK to call this for a device with no parent */ |
| 2123 | |
| 2124 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2125 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2126 | { |
| 2127 | rbd_dev_remove_parent(rbd_dev); |
| 2128 | rbd_spec_put(rbd_dev->parent_spec); |
| 2129 | rbd_dev->parent_spec = NULL; |
| 2130 | rbd_dev->parent_overlap = 0; |
| 2131 | } |
| 2132 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2133 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2134 | * Parent image reference counting is used to determine when an |
| 2135 | * image's parent fields can be safely torn down--after there are no |
| 2136 | * more in-flight requests to the parent image. When the last |
| 2137 | * reference is dropped, cleaning them up is safe. |
| 2138 | */ |
| 2139 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2140 | { |
| 2141 | int counter; |
| 2142 | |
| 2143 | if (!rbd_dev->parent_spec) |
| 2144 | return; |
| 2145 | |
| 2146 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2147 | if (counter > 0) |
| 2148 | return; |
| 2149 | |
| 2150 | /* Last reference; clean up parent data structures */ |
| 2151 | |
| 2152 | if (!counter) |
| 2153 | rbd_dev_unparent(rbd_dev); |
| 2154 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2155 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | /* |
| 2159 | * If an image has a non-zero parent overlap, get a reference to its |
| 2160 | * parent. |
| 2161 | * |
| 2162 | * Returns true if the rbd device has a parent with a non-zero |
| 2163 | * overlap and a reference for it was successfully taken, or |
| 2164 | * false otherwise. |
| 2165 | */ |
| 2166 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2167 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2168 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2169 | |
| 2170 | if (!rbd_dev->parent_spec) |
| 2171 | return false; |
| 2172 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2173 | down_read(&rbd_dev->header_rwsem); |
| 2174 | if (rbd_dev->parent_overlap) |
| 2175 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2176 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2177 | |
| 2178 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2179 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2180 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2181 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2182 | } |
| 2183 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2184 | /* |
| 2185 | * Caller is responsible for filling in the list of object requests |
| 2186 | * that comprises the image request, and the Linux request pointer |
| 2187 | * (if there is one). |
| 2188 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2189 | static struct rbd_img_request *rbd_img_request_create( |
| 2190 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2191 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2192 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2193 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2194 | { |
| 2195 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2196 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2197 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2198 | if (!img_request) |
| 2199 | return NULL; |
| 2200 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2201 | img_request->rq = NULL; |
| 2202 | img_request->rbd_dev = rbd_dev; |
| 2203 | img_request->offset = offset; |
| 2204 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2205 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2206 | if (op_type == OBJ_OP_DISCARD) { |
| 2207 | img_request_discard_set(img_request); |
| 2208 | img_request->snapc = snapc; |
| 2209 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2210 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2211 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2212 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2213 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2214 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2215 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2216 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2217 | spin_lock_init(&img_request->completion_lock); |
| 2218 | img_request->next_completion = 0; |
| 2219 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2220 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2221 | img_request->obj_request_count = 0; |
| 2222 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2223 | kref_init(&img_request->kref); |
| 2224 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2225 | 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] | 2226 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2227 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2228 | return img_request; |
| 2229 | } |
| 2230 | |
| 2231 | static void rbd_img_request_destroy(struct kref *kref) |
| 2232 | { |
| 2233 | struct rbd_img_request *img_request; |
| 2234 | struct rbd_obj_request *obj_request; |
| 2235 | struct rbd_obj_request *next_obj_request; |
| 2236 | |
| 2237 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2238 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2239 | dout("%s: img %p\n", __func__, img_request); |
| 2240 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2241 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2242 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2243 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2244 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2245 | if (img_request_layered_test(img_request)) { |
| 2246 | img_request_layered_clear(img_request); |
| 2247 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2248 | } |
| 2249 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2250 | if (img_request_write_test(img_request) || |
| 2251 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2252 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2253 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2254 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2255 | } |
| 2256 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2257 | static struct rbd_img_request *rbd_parent_request_create( |
| 2258 | struct rbd_obj_request *obj_request, |
| 2259 | u64 img_offset, u64 length) |
| 2260 | { |
| 2261 | struct rbd_img_request *parent_request; |
| 2262 | struct rbd_device *rbd_dev; |
| 2263 | |
| 2264 | rbd_assert(obj_request->img_request); |
| 2265 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2266 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2267 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2268 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2269 | if (!parent_request) |
| 2270 | return NULL; |
| 2271 | |
| 2272 | img_request_child_set(parent_request); |
| 2273 | rbd_obj_request_get(obj_request); |
| 2274 | parent_request->obj_request = obj_request; |
| 2275 | |
| 2276 | return parent_request; |
| 2277 | } |
| 2278 | |
| 2279 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2280 | { |
| 2281 | struct rbd_img_request *parent_request; |
| 2282 | struct rbd_obj_request *orig_request; |
| 2283 | |
| 2284 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2285 | orig_request = parent_request->obj_request; |
| 2286 | |
| 2287 | parent_request->obj_request = NULL; |
| 2288 | rbd_obj_request_put(orig_request); |
| 2289 | img_request_child_clear(parent_request); |
| 2290 | |
| 2291 | rbd_img_request_destroy(kref); |
| 2292 | } |
| 2293 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2294 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2295 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2296 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2297 | unsigned int xferred; |
| 2298 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2299 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2300 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2301 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2302 | img_request = obj_request->img_request; |
| 2303 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2304 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2305 | xferred = (unsigned int)obj_request->xferred; |
| 2306 | result = obj_request->result; |
| 2307 | if (result) { |
| 2308 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2309 | enum obj_operation_type op_type; |
| 2310 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2311 | if (img_request_discard_test(img_request)) |
| 2312 | op_type = OBJ_OP_DISCARD; |
| 2313 | else if (img_request_write_test(img_request)) |
| 2314 | op_type = OBJ_OP_WRITE; |
| 2315 | else |
| 2316 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2317 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2318 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2319 | obj_op_name(op_type), obj_request->length, |
| 2320 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2321 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2322 | result, xferred); |
| 2323 | if (!img_request->result) |
| 2324 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2325 | /* |
| 2326 | * Need to end I/O on the entire obj_request worth of |
| 2327 | * bytes in case of error. |
| 2328 | */ |
| 2329 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2330 | } |
| 2331 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2332 | if (img_request_child_test(img_request)) { |
| 2333 | rbd_assert(img_request->obj_request != NULL); |
| 2334 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2335 | } else { |
| 2336 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2337 | |
| 2338 | more = blk_update_request(img_request->rq, result, xferred); |
| 2339 | if (!more) |
| 2340 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2344 | } |
| 2345 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2346 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2347 | { |
| 2348 | struct rbd_img_request *img_request; |
| 2349 | u32 which = obj_request->which; |
| 2350 | bool more = true; |
| 2351 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2352 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2353 | img_request = obj_request->img_request; |
| 2354 | |
| 2355 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2356 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2357 | rbd_assert(img_request->obj_request_count > 0); |
| 2358 | rbd_assert(which != BAD_WHICH); |
| 2359 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2360 | |
| 2361 | spin_lock_irq(&img_request->completion_lock); |
| 2362 | if (which != img_request->next_completion) |
| 2363 | goto out; |
| 2364 | |
| 2365 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2366 | rbd_assert(more); |
| 2367 | rbd_assert(which < img_request->obj_request_count); |
| 2368 | |
| 2369 | if (!obj_request_done_test(obj_request)) |
| 2370 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2371 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2372 | which++; |
| 2373 | } |
| 2374 | |
| 2375 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2376 | img_request->next_completion = which; |
| 2377 | out: |
| 2378 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2379 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2380 | |
| 2381 | if (!more) |
| 2382 | rbd_img_request_complete(img_request); |
| 2383 | } |
| 2384 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2385 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2386 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2387 | * them for submission. num_ops is the current number of |
| 2388 | * osd operations already to the object request. |
| 2389 | */ |
| 2390 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2391 | struct ceph_osd_request *osd_request, |
| 2392 | enum obj_operation_type op_type, |
| 2393 | unsigned int num_ops) |
| 2394 | { |
| 2395 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2396 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2397 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2398 | u64 offset = obj_request->offset; |
| 2399 | u64 length = obj_request->length; |
| 2400 | u64 img_end; |
| 2401 | u16 opcode; |
| 2402 | |
| 2403 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2404 | if (!offset && length == object_size && |
| 2405 | (!img_request_layered_test(img_request) || |
| 2406 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2407 | opcode = CEPH_OSD_OP_DELETE; |
| 2408 | } else if ((offset + length == object_size)) { |
| 2409 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2410 | } else { |
| 2411 | down_read(&rbd_dev->header_rwsem); |
| 2412 | img_end = rbd_dev->header.image_size; |
| 2413 | up_read(&rbd_dev->header_rwsem); |
| 2414 | |
| 2415 | if (obj_request->img_offset + length == img_end) |
| 2416 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2417 | else |
| 2418 | opcode = CEPH_OSD_OP_ZERO; |
| 2419 | } |
| 2420 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2421 | if (!offset && length == object_size) |
| 2422 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2423 | else |
| 2424 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2425 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2426 | object_size, object_size); |
| 2427 | num_ops++; |
| 2428 | } else { |
| 2429 | opcode = CEPH_OSD_OP_READ; |
| 2430 | } |
| 2431 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2432 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2433 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2434 | else |
| 2435 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2436 | offset, length, 0, 0); |
| 2437 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2438 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2439 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2440 | obj_request->bio_list, length); |
| 2441 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2442 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2443 | obj_request->pages, length, |
| 2444 | offset & ~PAGE_MASK, false, false); |
| 2445 | |
| 2446 | /* Discards are also writes */ |
| 2447 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2448 | rbd_osd_req_format_write(obj_request); |
| 2449 | else |
| 2450 | rbd_osd_req_format_read(obj_request); |
| 2451 | } |
| 2452 | |
| 2453 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2454 | * Split up an image request into one or more object requests, each |
| 2455 | * to a different object. The "type" parameter indicates whether |
| 2456 | * "data_desc" is the pointer to the head of a list of bio |
| 2457 | * structures, or the base of a page array. In either case this |
| 2458 | * function assumes data_desc describes memory sufficient to hold |
| 2459 | * all data described by the image request. |
| 2460 | */ |
| 2461 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2462 | enum obj_request_type type, |
| 2463 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2464 | { |
| 2465 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2466 | struct rbd_obj_request *obj_request = NULL; |
| 2467 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2468 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2469 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2470 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2471 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2472 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2473 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2474 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2475 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2476 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2477 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2478 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2479 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2480 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2481 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2482 | |
| 2483 | if (type == OBJ_REQUEST_BIO) { |
| 2484 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2485 | rbd_assert(img_offset == |
| 2486 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2487 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2488 | pages = data_desc; |
| 2489 | } |
| 2490 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2491 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2492 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2493 | const char *object_name; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 2494 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2495 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2496 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2497 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2498 | object_name = rbd_segment_name(rbd_dev, img_offset); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2499 | if (!object_name) |
| 2500 | goto out_unwind; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2501 | obj_request = rbd_obj_request_create(object_name, type); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 2502 | /* object request has its own copy of the object name */ |
| 2503 | rbd_segment_name_free(object_name); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2504 | if (!obj_request) |
| 2505 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2506 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 2507 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2508 | obj_request->offset = offset; |
| 2509 | obj_request->length = length; |
| 2510 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2511 | /* |
| 2512 | * set obj_request->img_request before creating the |
| 2513 | * osd_request so that it gets the right snapc |
| 2514 | */ |
| 2515 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2516 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2517 | if (type == OBJ_REQUEST_BIO) { |
| 2518 | unsigned int clone_size; |
| 2519 | |
| 2520 | rbd_assert(length <= (u64)UINT_MAX); |
| 2521 | clone_size = (unsigned int)length; |
| 2522 | obj_request->bio_list = |
| 2523 | bio_chain_clone_range(&bio_list, |
| 2524 | &bio_offset, |
| 2525 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2526 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2527 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2528 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2529 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2530 | unsigned int page_count; |
| 2531 | |
| 2532 | obj_request->pages = pages; |
| 2533 | page_count = (u32)calc_pages_for(offset, length); |
| 2534 | obj_request->page_count = page_count; |
| 2535 | if ((offset + length) & ~PAGE_MASK) |
| 2536 | page_count--; /* more on last page */ |
| 2537 | pages += page_count; |
| 2538 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2539 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2540 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2541 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2542 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2543 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2544 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2545 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2546 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2547 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2548 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2549 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2550 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2551 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2552 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2553 | resid -= length; |
| 2554 | } |
| 2555 | |
| 2556 | return 0; |
| 2557 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2558 | out_unwind: |
| 2559 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2560 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2561 | |
| 2562 | return -ENOMEM; |
| 2563 | } |
| 2564 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2565 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2566 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2567 | { |
| 2568 | struct rbd_img_request *img_request; |
| 2569 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2570 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2571 | u32 page_count; |
| 2572 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2573 | dout("%s: obj %p\n", __func__, obj_request); |
| 2574 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2575 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2576 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2577 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2578 | img_request = obj_request->img_request; |
| 2579 | rbd_assert(img_request); |
| 2580 | |
| 2581 | rbd_dev = img_request->rbd_dev; |
| 2582 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2583 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2584 | pages = obj_request->copyup_pages; |
| 2585 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2586 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2587 | page_count = obj_request->copyup_page_count; |
| 2588 | rbd_assert(page_count); |
| 2589 | obj_request->copyup_page_count = 0; |
| 2590 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2591 | |
| 2592 | /* |
| 2593 | * We want the transfer count to reflect the size of the |
| 2594 | * original write request. There is no such thing as a |
| 2595 | * successful short write, so if the request was successful |
| 2596 | * we can just set it to the originally-requested length. |
| 2597 | */ |
| 2598 | if (!obj_request->result) |
| 2599 | obj_request->xferred = obj_request->length; |
| 2600 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2601 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2605 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2606 | { |
| 2607 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2608 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2609 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2610 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2611 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2612 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2613 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2614 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2615 | |
| 2616 | rbd_assert(img_request_child_test(img_request)); |
| 2617 | |
| 2618 | /* First get what we need from the image request */ |
| 2619 | |
| 2620 | pages = img_request->copyup_pages; |
| 2621 | rbd_assert(pages != NULL); |
| 2622 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2623 | page_count = img_request->copyup_page_count; |
| 2624 | rbd_assert(page_count); |
| 2625 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2626 | |
| 2627 | orig_request = img_request->obj_request; |
| 2628 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2629 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2630 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2631 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2632 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2633 | rbd_img_request_put(img_request); |
| 2634 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2635 | rbd_assert(orig_request->img_request); |
| 2636 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2637 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2638 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2639 | /* |
| 2640 | * If the overlap has become 0 (most likely because the |
| 2641 | * image has been flattened) we need to free the pages |
| 2642 | * and re-submit the original write request. |
| 2643 | */ |
| 2644 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2645 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2646 | rbd_obj_request_submit(orig_request); |
| 2647 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2651 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2652 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2653 | /* |
| 2654 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2655 | * 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] | 2656 | * request. Allocate the new copyup osd request for the |
| 2657 | * original request, and release the old one. |
| 2658 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2659 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2660 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2661 | if (!osd_req) |
| 2662 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2663 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2664 | orig_request->osd_req = osd_req; |
| 2665 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2666 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2667 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2668 | /* Initialize the copyup op */ |
| 2669 | |
| 2670 | 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] | 2671 | 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] | 2672 | false, false); |
| 2673 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2674 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2675 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2676 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2677 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2678 | |
| 2679 | /* All set, send it off. */ |
| 2680 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2681 | rbd_obj_request_submit(orig_request); |
| 2682 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2683 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2684 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2685 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2686 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2687 | } |
| 2688 | |
| 2689 | /* |
| 2690 | * Read from the parent image the range of data that covers the |
| 2691 | * entire target of the given object request. This is used for |
| 2692 | * satisfying a layered image write request when the target of an |
| 2693 | * object request from the image request does not exist. |
| 2694 | * |
| 2695 | * A page array big enough to hold the returned data is allocated |
| 2696 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2697 | * When the read completes, this page array will be transferred to |
| 2698 | * the original object request for the copyup operation. |
| 2699 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2700 | * If an error occurs, it is recorded as the result of the original |
| 2701 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2702 | */ |
| 2703 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2704 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2705 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2706 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2707 | u64 img_offset; |
| 2708 | u64 length; |
| 2709 | struct page **pages = NULL; |
| 2710 | u32 page_count; |
| 2711 | int result; |
| 2712 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2713 | rbd_assert(rbd_dev->parent != NULL); |
| 2714 | |
| 2715 | /* |
| 2716 | * Determine the byte range covered by the object in the |
| 2717 | * child image to which the original request was to be sent. |
| 2718 | */ |
| 2719 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2720 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2721 | |
| 2722 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2723 | * There is no defined parent data beyond the parent |
| 2724 | * overlap, so limit what we read at that boundary if |
| 2725 | * necessary. |
| 2726 | */ |
| 2727 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2728 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2729 | length = rbd_dev->parent_overlap - img_offset; |
| 2730 | } |
| 2731 | |
| 2732 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2733 | * Allocate a page array big enough to receive the data read |
| 2734 | * from the parent. |
| 2735 | */ |
| 2736 | page_count = (u32)calc_pages_for(0, length); |
| 2737 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2738 | if (IS_ERR(pages)) { |
| 2739 | result = PTR_ERR(pages); |
| 2740 | pages = NULL; |
| 2741 | goto out_err; |
| 2742 | } |
| 2743 | |
| 2744 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2745 | parent_request = rbd_parent_request_create(obj_request, |
| 2746 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2747 | if (!parent_request) |
| 2748 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2749 | |
| 2750 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2751 | if (result) |
| 2752 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2753 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2754 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2755 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2756 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2757 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2758 | result = rbd_img_request_submit(parent_request); |
| 2759 | if (!result) |
| 2760 | return 0; |
| 2761 | |
| 2762 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2763 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2764 | parent_request->obj_request = NULL; |
| 2765 | rbd_obj_request_put(obj_request); |
| 2766 | out_err: |
| 2767 | if (pages) |
| 2768 | ceph_release_page_vector(pages, page_count); |
| 2769 | if (parent_request) |
| 2770 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2771 | return result; |
| 2772 | } |
| 2773 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2774 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2775 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2776 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2777 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2778 | int result; |
| 2779 | |
| 2780 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2781 | |
| 2782 | /* |
| 2783 | * All we need from the object request is the original |
| 2784 | * request and the result of the STAT op. Grab those, then |
| 2785 | * we're done with the request. |
| 2786 | */ |
| 2787 | orig_request = obj_request->obj_request; |
| 2788 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2789 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2790 | rbd_assert(orig_request); |
| 2791 | rbd_assert(orig_request->img_request); |
| 2792 | |
| 2793 | result = obj_request->result; |
| 2794 | obj_request->result = 0; |
| 2795 | |
| 2796 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2797 | obj_request, orig_request, result, |
| 2798 | obj_request->xferred, obj_request->length); |
| 2799 | rbd_obj_request_put(obj_request); |
| 2800 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2801 | /* |
| 2802 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2803 | * image has been flattened) we need to re-submit the |
| 2804 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2805 | */ |
| 2806 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2807 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2808 | rbd_obj_request_submit(orig_request); |
| 2809 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2810 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2811 | |
| 2812 | /* |
| 2813 | * Our only purpose here is to determine whether the object |
| 2814 | * exists, and we don't want to treat the non-existence as |
| 2815 | * an error. If something else comes back, transfer the |
| 2816 | * error to the original request and complete it now. |
| 2817 | */ |
| 2818 | if (!result) { |
| 2819 | obj_request_existence_set(orig_request, true); |
| 2820 | } else if (result == -ENOENT) { |
| 2821 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2822 | } else { |
| 2823 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | /* |
| 2827 | * Resubmit the original request now that we have recorded |
| 2828 | * whether the target object exists. |
| 2829 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2830 | result = rbd_img_obj_request_submit(orig_request); |
| 2831 | if (result) |
| 2832 | goto fail_orig_request; |
| 2833 | |
| 2834 | return; |
| 2835 | |
| 2836 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2837 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2841 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2842 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2843 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2844 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2845 | u32 page_count; |
| 2846 | size_t size; |
| 2847 | int ret; |
| 2848 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2849 | stat_request = rbd_obj_request_create(obj_request->object_name, |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2850 | OBJ_REQUEST_PAGES); |
| 2851 | if (!stat_request) |
| 2852 | return -ENOMEM; |
| 2853 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame^] | 2854 | stat_request->object_no = obj_request->object_no; |
| 2855 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2856 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2857 | stat_request); |
| 2858 | if (!stat_request->osd_req) { |
| 2859 | ret = -ENOMEM; |
| 2860 | goto fail_stat_request; |
| 2861 | } |
| 2862 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2863 | /* |
| 2864 | * The response data for a STAT call consists of: |
| 2865 | * le64 length; |
| 2866 | * struct { |
| 2867 | * le32 tv_sec; |
| 2868 | * le32 tv_nsec; |
| 2869 | * } mtime; |
| 2870 | */ |
| 2871 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2872 | page_count = (u32)calc_pages_for(0, size); |
| 2873 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2874 | if (IS_ERR(pages)) { |
| 2875 | ret = PTR_ERR(pages); |
| 2876 | goto fail_stat_request; |
| 2877 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2878 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2879 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2880 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2881 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2882 | |
| 2883 | rbd_obj_request_get(obj_request); |
| 2884 | stat_request->obj_request = obj_request; |
| 2885 | stat_request->pages = pages; |
| 2886 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2887 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2888 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2889 | rbd_obj_request_submit(stat_request); |
| 2890 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2891 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2892 | fail_stat_request: |
| 2893 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2894 | return ret; |
| 2895 | } |
| 2896 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2897 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2898 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2899 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2900 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
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 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2903 | if (!img_request_write_test(img_request) && |
| 2904 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2905 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2906 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2907 | /* Non-layered writes */ |
| 2908 | if (!img_request_layered_test(img_request)) |
| 2909 | return true; |
| 2910 | |
| 2911 | /* |
| 2912 | * Layered writes outside of the parent overlap range don't |
| 2913 | * share any data with the parent. |
| 2914 | */ |
| 2915 | if (!obj_request_overlaps_parent(obj_request)) |
| 2916 | return true; |
| 2917 | |
| 2918 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2919 | * Entire-object layered writes - we will overwrite whatever |
| 2920 | * parent data there is anyway. |
| 2921 | */ |
| 2922 | if (!obj_request->offset && |
| 2923 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2924 | return true; |
| 2925 | |
| 2926 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2927 | * If the object is known to already exist, its parent data has |
| 2928 | * already been copied. |
| 2929 | */ |
| 2930 | if (obj_request_known_test(obj_request) && |
| 2931 | obj_request_exists_test(obj_request)) |
| 2932 | return true; |
| 2933 | |
| 2934 | return false; |
| 2935 | } |
| 2936 | |
| 2937 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2938 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2939 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2940 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2941 | rbd_assert(obj_request->img_request); |
| 2942 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2943 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2944 | rbd_obj_request_submit(obj_request); |
| 2945 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2949 | * It's a layered write. The target object might exist but |
| 2950 | * we may not know that yet. If we know it doesn't exist, |
| 2951 | * start by reading the data for the full target object from |
| 2952 | * 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] | 2953 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2954 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2955 | return rbd_img_obj_parent_read_full(obj_request); |
| 2956 | |
| 2957 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2958 | |
| 2959 | return rbd_img_obj_exists_submit(obj_request); |
| 2960 | } |
| 2961 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2962 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2963 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2964 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2965 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2966 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2967 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2968 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2969 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2970 | rbd_img_request_get(img_request); |
| 2971 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2972 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2973 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2974 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2975 | } |
| 2976 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2977 | out_put_ireq: |
| 2978 | rbd_img_request_put(img_request); |
| 2979 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2980 | } |
| 2981 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2982 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2983 | { |
| 2984 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2985 | struct rbd_device *rbd_dev; |
| 2986 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2987 | u64 img_xferred; |
| 2988 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2989 | |
| 2990 | rbd_assert(img_request_child_test(img_request)); |
| 2991 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2992 | /* First get what we need from the image request and release it */ |
| 2993 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2994 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2995 | img_xferred = img_request->xferred; |
| 2996 | img_result = img_request->result; |
| 2997 | rbd_img_request_put(img_request); |
| 2998 | |
| 2999 | /* |
| 3000 | * If the overlap has become 0 (most likely because the |
| 3001 | * image has been flattened) we need to re-submit the |
| 3002 | * original request. |
| 3003 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3004 | rbd_assert(obj_request); |
| 3005 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3006 | rbd_dev = obj_request->img_request->rbd_dev; |
| 3007 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 3008 | rbd_obj_request_submit(obj_request); |
| 3009 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3013 | if (obj_request->result) |
| 3014 | goto out; |
| 3015 | |
| 3016 | /* |
| 3017 | * We need to zero anything beyond the parent overlap |
| 3018 | * boundary. Since rbd_img_obj_request_read_callback() |
| 3019 | * will zero anything beyond the end of a short read, an |
| 3020 | * easy way to do this is to pretend the data from the |
| 3021 | * parent came up short--ending at the overlap boundary. |
| 3022 | */ |
| 3023 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 3024 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3025 | if (obj_end > rbd_dev->parent_overlap) { |
| 3026 | u64 xferred = 0; |
| 3027 | |
| 3028 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 3029 | xferred = rbd_dev->parent_overlap - |
| 3030 | obj_request->img_offset; |
| 3031 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3032 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3033 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3034 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3035 | } |
| 3036 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3037 | rbd_img_obj_request_read_callback(obj_request); |
| 3038 | rbd_obj_request_complete(obj_request); |
| 3039 | } |
| 3040 | |
| 3041 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 3042 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3043 | struct rbd_img_request *img_request; |
| 3044 | int result; |
| 3045 | |
| 3046 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 3047 | rbd_assert(obj_request->img_request != NULL); |
| 3048 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3049 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3050 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3051 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3052 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3053 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3054 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3055 | result = -ENOMEM; |
| 3056 | if (!img_request) |
| 3057 | goto out_err; |
| 3058 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3059 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3060 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3061 | obj_request->bio_list); |
| 3062 | else |
| 3063 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3064 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3065 | if (result) |
| 3066 | goto out_err; |
| 3067 | |
| 3068 | img_request->callback = rbd_img_parent_read_callback; |
| 3069 | result = rbd_img_request_submit(img_request); |
| 3070 | if (result) |
| 3071 | goto out_err; |
| 3072 | |
| 3073 | return; |
| 3074 | out_err: |
| 3075 | if (img_request) |
| 3076 | rbd_img_request_put(img_request); |
| 3077 | obj_request->result = result; |
| 3078 | obj_request->xferred = 0; |
| 3079 | obj_request_done_set(obj_request); |
| 3080 | } |
| 3081 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3082 | static const struct rbd_client_id rbd_empty_cid; |
| 3083 | |
| 3084 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3085 | const struct rbd_client_id *rhs) |
| 3086 | { |
| 3087 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3088 | } |
| 3089 | |
| 3090 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3091 | { |
| 3092 | struct rbd_client_id cid; |
| 3093 | |
| 3094 | mutex_lock(&rbd_dev->watch_mutex); |
| 3095 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3096 | cid.handle = rbd_dev->watch_cookie; |
| 3097 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3098 | return cid; |
| 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * lock_rwsem must be held for write |
| 3103 | */ |
| 3104 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3105 | const struct rbd_client_id *cid) |
| 3106 | { |
| 3107 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3108 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3109 | cid->gid, cid->handle); |
| 3110 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3111 | } |
| 3112 | |
| 3113 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3114 | { |
| 3115 | mutex_lock(&rbd_dev->watch_mutex); |
| 3116 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3117 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3118 | } |
| 3119 | |
| 3120 | /* |
| 3121 | * lock_rwsem must be held for write |
| 3122 | */ |
| 3123 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3124 | { |
| 3125 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3126 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3127 | char cookie[32]; |
| 3128 | int ret; |
| 3129 | |
| 3130 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
| 3131 | |
| 3132 | format_lock_cookie(rbd_dev, cookie); |
| 3133 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3134 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3135 | RBD_LOCK_TAG, "", 0); |
| 3136 | if (ret) |
| 3137 | return ret; |
| 3138 | |
| 3139 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
| 3140 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3141 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3142 | return 0; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
| 3146 | * lock_rwsem must be held for write |
| 3147 | */ |
| 3148 | static int rbd_unlock(struct rbd_device *rbd_dev) |
| 3149 | { |
| 3150 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3151 | char cookie[32]; |
| 3152 | int ret; |
| 3153 | |
| 3154 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
| 3155 | |
| 3156 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 3157 | |
| 3158 | format_lock_cookie(rbd_dev, cookie); |
| 3159 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3160 | RBD_LOCK_NAME, cookie); |
| 3161 | if (ret && ret != -ENOENT) { |
| 3162 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); |
| 3163 | return ret; |
| 3164 | } |
| 3165 | |
| 3166 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3167 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
| 3168 | return 0; |
| 3169 | } |
| 3170 | |
| 3171 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3172 | enum rbd_notify_op notify_op, |
| 3173 | struct page ***preply_pages, |
| 3174 | size_t *preply_len) |
| 3175 | { |
| 3176 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3177 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3178 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3179 | char buf[buf_size]; |
| 3180 | void *p = buf; |
| 3181 | |
| 3182 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3183 | |
| 3184 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3185 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3186 | ceph_encode_32(&p, notify_op); |
| 3187 | ceph_encode_64(&p, cid.gid); |
| 3188 | ceph_encode_64(&p, cid.handle); |
| 3189 | |
| 3190 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3191 | &rbd_dev->header_oloc, buf, buf_size, |
| 3192 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3193 | } |
| 3194 | |
| 3195 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3196 | enum rbd_notify_op notify_op) |
| 3197 | { |
| 3198 | struct page **reply_pages; |
| 3199 | size_t reply_len; |
| 3200 | |
| 3201 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3202 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3203 | } |
| 3204 | |
| 3205 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3206 | { |
| 3207 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3208 | acquired_lock_work); |
| 3209 | |
| 3210 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3211 | } |
| 3212 | |
| 3213 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3214 | { |
| 3215 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3216 | released_lock_work); |
| 3217 | |
| 3218 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3219 | } |
| 3220 | |
| 3221 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3222 | { |
| 3223 | struct page **reply_pages; |
| 3224 | size_t reply_len; |
| 3225 | bool lock_owner_responded = false; |
| 3226 | int ret; |
| 3227 | |
| 3228 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3229 | |
| 3230 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3231 | &reply_pages, &reply_len); |
| 3232 | if (ret && ret != -ETIMEDOUT) { |
| 3233 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3234 | goto out; |
| 3235 | } |
| 3236 | |
| 3237 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3238 | void *p = page_address(reply_pages[0]); |
| 3239 | void *const end = p + reply_len; |
| 3240 | u32 n; |
| 3241 | |
| 3242 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3243 | while (n--) { |
| 3244 | u8 struct_v; |
| 3245 | u32 len; |
| 3246 | |
| 3247 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3248 | p += 8 + 8; /* skip gid and cookie */ |
| 3249 | |
| 3250 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3251 | if (!len) |
| 3252 | continue; |
| 3253 | |
| 3254 | if (lock_owner_responded) { |
| 3255 | rbd_warn(rbd_dev, |
| 3256 | "duplicate lock owners detected"); |
| 3257 | ret = -EIO; |
| 3258 | goto out; |
| 3259 | } |
| 3260 | |
| 3261 | lock_owner_responded = true; |
| 3262 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3263 | &struct_v, &len); |
| 3264 | if (ret) { |
| 3265 | rbd_warn(rbd_dev, |
| 3266 | "failed to decode ResponseMessage: %d", |
| 3267 | ret); |
| 3268 | goto e_inval; |
| 3269 | } |
| 3270 | |
| 3271 | ret = ceph_decode_32(&p); |
| 3272 | } |
| 3273 | } |
| 3274 | |
| 3275 | if (!lock_owner_responded) { |
| 3276 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3277 | ret = -ETIMEDOUT; |
| 3278 | } |
| 3279 | |
| 3280 | out: |
| 3281 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3282 | return ret; |
| 3283 | |
| 3284 | e_inval: |
| 3285 | ret = -EINVAL; |
| 3286 | goto out; |
| 3287 | } |
| 3288 | |
| 3289 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3290 | { |
| 3291 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3292 | |
| 3293 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3294 | if (wake_all) |
| 3295 | wake_up_all(&rbd_dev->lock_waitq); |
| 3296 | else |
| 3297 | wake_up(&rbd_dev->lock_waitq); |
| 3298 | } |
| 3299 | |
| 3300 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3301 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3302 | { |
| 3303 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3304 | u8 lock_type; |
| 3305 | char *lock_tag; |
| 3306 | int ret; |
| 3307 | |
| 3308 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3309 | |
| 3310 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3311 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3312 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3313 | if (ret) |
| 3314 | return ret; |
| 3315 | |
| 3316 | if (*num_lockers == 0) { |
| 3317 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3318 | goto out; |
| 3319 | } |
| 3320 | |
| 3321 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3322 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3323 | lock_tag); |
| 3324 | ret = -EBUSY; |
| 3325 | goto out; |
| 3326 | } |
| 3327 | |
| 3328 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3329 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3330 | ret = -EBUSY; |
| 3331 | goto out; |
| 3332 | } |
| 3333 | |
| 3334 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3335 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3336 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3337 | (*lockers)[0].id.cookie); |
| 3338 | ret = -EBUSY; |
| 3339 | goto out; |
| 3340 | } |
| 3341 | |
| 3342 | out: |
| 3343 | kfree(lock_tag); |
| 3344 | return ret; |
| 3345 | } |
| 3346 | |
| 3347 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3348 | const struct ceph_locker *locker) |
| 3349 | { |
| 3350 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3351 | struct ceph_watch_item *watchers; |
| 3352 | u32 num_watchers; |
| 3353 | u64 cookie; |
| 3354 | int i; |
| 3355 | int ret; |
| 3356 | |
| 3357 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3358 | &rbd_dev->header_oloc, &watchers, |
| 3359 | &num_watchers); |
| 3360 | if (ret) |
| 3361 | return ret; |
| 3362 | |
| 3363 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3364 | for (i = 0; i < num_watchers; i++) { |
| 3365 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3366 | sizeof(locker->info.addr)) && |
| 3367 | watchers[i].cookie == cookie) { |
| 3368 | struct rbd_client_id cid = { |
| 3369 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3370 | .handle = cookie, |
| 3371 | }; |
| 3372 | |
| 3373 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3374 | rbd_dev, cid.gid, cid.handle); |
| 3375 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3376 | ret = 1; |
| 3377 | goto out; |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3382 | ret = 0; |
| 3383 | out: |
| 3384 | kfree(watchers); |
| 3385 | return ret; |
| 3386 | } |
| 3387 | |
| 3388 | /* |
| 3389 | * lock_rwsem must be held for write |
| 3390 | */ |
| 3391 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3392 | { |
| 3393 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3394 | struct ceph_locker *lockers; |
| 3395 | u32 num_lockers; |
| 3396 | int ret; |
| 3397 | |
| 3398 | for (;;) { |
| 3399 | ret = rbd_lock(rbd_dev); |
| 3400 | if (ret != -EBUSY) |
| 3401 | return ret; |
| 3402 | |
| 3403 | /* determine if the current lock holder is still alive */ |
| 3404 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3405 | if (ret) |
| 3406 | return ret; |
| 3407 | |
| 3408 | if (num_lockers == 0) |
| 3409 | goto again; |
| 3410 | |
| 3411 | ret = find_watcher(rbd_dev, lockers); |
| 3412 | if (ret) { |
| 3413 | if (ret > 0) |
| 3414 | ret = 0; /* have to request lock */ |
| 3415 | goto out; |
| 3416 | } |
| 3417 | |
| 3418 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3419 | ENTITY_NAME(lockers[0].id.name)); |
| 3420 | |
| 3421 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3422 | &lockers[0].info.addr); |
| 3423 | if (ret) { |
| 3424 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3425 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3426 | goto out; |
| 3427 | } |
| 3428 | |
| 3429 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3430 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3431 | lockers[0].id.cookie, |
| 3432 | &lockers[0].id.name); |
| 3433 | if (ret && ret != -ENOENT) |
| 3434 | goto out; |
| 3435 | |
| 3436 | again: |
| 3437 | ceph_free_lockers(lockers, num_lockers); |
| 3438 | } |
| 3439 | |
| 3440 | out: |
| 3441 | ceph_free_lockers(lockers, num_lockers); |
| 3442 | return ret; |
| 3443 | } |
| 3444 | |
| 3445 | /* |
| 3446 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3447 | */ |
| 3448 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3449 | int *pret) |
| 3450 | { |
| 3451 | enum rbd_lock_state lock_state; |
| 3452 | |
| 3453 | down_read(&rbd_dev->lock_rwsem); |
| 3454 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3455 | rbd_dev->lock_state); |
| 3456 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3457 | lock_state = rbd_dev->lock_state; |
| 3458 | up_read(&rbd_dev->lock_rwsem); |
| 3459 | return lock_state; |
| 3460 | } |
| 3461 | |
| 3462 | up_read(&rbd_dev->lock_rwsem); |
| 3463 | down_write(&rbd_dev->lock_rwsem); |
| 3464 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3465 | rbd_dev->lock_state); |
| 3466 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3467 | *pret = rbd_try_lock(rbd_dev); |
| 3468 | if (*pret) |
| 3469 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3470 | } |
| 3471 | |
| 3472 | lock_state = rbd_dev->lock_state; |
| 3473 | up_write(&rbd_dev->lock_rwsem); |
| 3474 | return lock_state; |
| 3475 | } |
| 3476 | |
| 3477 | static void rbd_acquire_lock(struct work_struct *work) |
| 3478 | { |
| 3479 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3480 | struct rbd_device, lock_dwork); |
| 3481 | enum rbd_lock_state lock_state; |
| 3482 | int ret; |
| 3483 | |
| 3484 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3485 | again: |
| 3486 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3487 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3488 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3489 | wake_requests(rbd_dev, true); |
| 3490 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3491 | rbd_dev, lock_state, ret); |
| 3492 | return; |
| 3493 | } |
| 3494 | |
| 3495 | ret = rbd_request_lock(rbd_dev); |
| 3496 | if (ret == -ETIMEDOUT) { |
| 3497 | goto again; /* treat this as a dead client */ |
| 3498 | } else if (ret < 0) { |
| 3499 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3500 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3501 | RBD_RETRY_DELAY); |
| 3502 | } else { |
| 3503 | /* |
| 3504 | * lock owner acked, but resend if we don't see them |
| 3505 | * release the lock |
| 3506 | */ |
| 3507 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3508 | rbd_dev); |
| 3509 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3510 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3511 | } |
| 3512 | } |
| 3513 | |
| 3514 | /* |
| 3515 | * lock_rwsem must be held for write |
| 3516 | */ |
| 3517 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3518 | { |
| 3519 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3520 | rbd_dev->lock_state); |
| 3521 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3522 | return false; |
| 3523 | |
| 3524 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3525 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3526 | /* |
| 3527 | * Ensure that all in-flight IO is flushed. |
| 3528 | * |
| 3529 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3530 | * may be shared with other devices. |
| 3531 | */ |
| 3532 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3533 | up_read(&rbd_dev->lock_rwsem); |
| 3534 | |
| 3535 | down_write(&rbd_dev->lock_rwsem); |
| 3536 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3537 | rbd_dev->lock_state); |
| 3538 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3539 | return false; |
| 3540 | |
| 3541 | if (!rbd_unlock(rbd_dev)) |
| 3542 | /* |
| 3543 | * Give others a chance to grab the lock - we would re-acquire |
| 3544 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3545 | * otherwise. We need to ack our own notifications, so this |
| 3546 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3547 | * after wake_requests() in rbd_handle_released_lock(). |
| 3548 | */ |
| 3549 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3550 | |
| 3551 | return true; |
| 3552 | } |
| 3553 | |
| 3554 | static void rbd_release_lock_work(struct work_struct *work) |
| 3555 | { |
| 3556 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3557 | unlock_work); |
| 3558 | |
| 3559 | down_write(&rbd_dev->lock_rwsem); |
| 3560 | rbd_release_lock(rbd_dev); |
| 3561 | up_write(&rbd_dev->lock_rwsem); |
| 3562 | } |
| 3563 | |
| 3564 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3565 | void **p) |
| 3566 | { |
| 3567 | struct rbd_client_id cid = { 0 }; |
| 3568 | |
| 3569 | if (struct_v >= 2) { |
| 3570 | cid.gid = ceph_decode_64(p); |
| 3571 | cid.handle = ceph_decode_64(p); |
| 3572 | } |
| 3573 | |
| 3574 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3575 | cid.handle); |
| 3576 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3577 | down_write(&rbd_dev->lock_rwsem); |
| 3578 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3579 | /* |
| 3580 | * we already know that the remote client is |
| 3581 | * the owner |
| 3582 | */ |
| 3583 | up_write(&rbd_dev->lock_rwsem); |
| 3584 | return; |
| 3585 | } |
| 3586 | |
| 3587 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3588 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3589 | } else { |
| 3590 | down_read(&rbd_dev->lock_rwsem); |
| 3591 | } |
| 3592 | |
| 3593 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3594 | wake_requests(rbd_dev, false); |
| 3595 | up_read(&rbd_dev->lock_rwsem); |
| 3596 | } |
| 3597 | |
| 3598 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3599 | void **p) |
| 3600 | { |
| 3601 | struct rbd_client_id cid = { 0 }; |
| 3602 | |
| 3603 | if (struct_v >= 2) { |
| 3604 | cid.gid = ceph_decode_64(p); |
| 3605 | cid.handle = ceph_decode_64(p); |
| 3606 | } |
| 3607 | |
| 3608 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3609 | cid.handle); |
| 3610 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3611 | down_write(&rbd_dev->lock_rwsem); |
| 3612 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3613 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3614 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3615 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3616 | up_write(&rbd_dev->lock_rwsem); |
| 3617 | return; |
| 3618 | } |
| 3619 | |
| 3620 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3621 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3622 | } else { |
| 3623 | down_read(&rbd_dev->lock_rwsem); |
| 3624 | } |
| 3625 | |
| 3626 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3627 | wake_requests(rbd_dev, false); |
| 3628 | up_read(&rbd_dev->lock_rwsem); |
| 3629 | } |
| 3630 | |
| 3631 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3632 | void **p) |
| 3633 | { |
| 3634 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3635 | struct rbd_client_id cid = { 0 }; |
| 3636 | bool need_to_send; |
| 3637 | |
| 3638 | if (struct_v >= 2) { |
| 3639 | cid.gid = ceph_decode_64(p); |
| 3640 | cid.handle = ceph_decode_64(p); |
| 3641 | } |
| 3642 | |
| 3643 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3644 | cid.handle); |
| 3645 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3646 | return false; |
| 3647 | |
| 3648 | down_read(&rbd_dev->lock_rwsem); |
| 3649 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3650 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3651 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3652 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3653 | rbd_dev); |
| 3654 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3655 | } |
| 3656 | } |
| 3657 | up_read(&rbd_dev->lock_rwsem); |
| 3658 | return need_to_send; |
| 3659 | } |
| 3660 | |
| 3661 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3662 | u64 notify_id, u64 cookie, s32 *result) |
| 3663 | { |
| 3664 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3665 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3666 | char buf[buf_size]; |
| 3667 | int ret; |
| 3668 | |
| 3669 | if (result) { |
| 3670 | void *p = buf; |
| 3671 | |
| 3672 | /* encode ResponseMessage */ |
| 3673 | ceph_start_encoding(&p, 1, 1, |
| 3674 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3675 | ceph_encode_32(&p, *result); |
| 3676 | } else { |
| 3677 | buf_size = 0; |
| 3678 | } |
| 3679 | |
| 3680 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3681 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3682 | buf, buf_size); |
| 3683 | if (ret) |
| 3684 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3685 | } |
| 3686 | |
| 3687 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3688 | u64 cookie) |
| 3689 | { |
| 3690 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3691 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3692 | } |
| 3693 | |
| 3694 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3695 | u64 notify_id, u64 cookie, s32 result) |
| 3696 | { |
| 3697 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3698 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3699 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3700 | |
| 3701 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3702 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3703 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3704 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3705 | void *p = data; |
| 3706 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3707 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3708 | u32 len; |
| 3709 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3710 | int ret; |
| 3711 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3712 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3713 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3714 | if (data_len) { |
| 3715 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3716 | &struct_v, &len); |
| 3717 | if (ret) { |
| 3718 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3719 | ret); |
| 3720 | return; |
| 3721 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3722 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3723 | notify_op = ceph_decode_32(&p); |
| 3724 | } else { |
| 3725 | /* legacy notification for header updates */ |
| 3726 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3727 | len = 0; |
| 3728 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3729 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3730 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3731 | switch (notify_op) { |
| 3732 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3733 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3734 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3735 | break; |
| 3736 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3737 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3738 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3739 | break; |
| 3740 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3741 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3742 | /* |
| 3743 | * send ResponseMessage(0) back so the client |
| 3744 | * can detect a missing owner |
| 3745 | */ |
| 3746 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3747 | cookie, 0); |
| 3748 | else |
| 3749 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3750 | break; |
| 3751 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3752 | ret = rbd_dev_refresh(rbd_dev); |
| 3753 | if (ret) |
| 3754 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3755 | |
| 3756 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3757 | break; |
| 3758 | default: |
| 3759 | if (rbd_is_lock_owner(rbd_dev)) |
| 3760 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3761 | cookie, -EOPNOTSUPP); |
| 3762 | else |
| 3763 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3764 | break; |
| 3765 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3766 | } |
| 3767 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3768 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3769 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3770 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3771 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3772 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3773 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3774 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3775 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3776 | down_write(&rbd_dev->lock_rwsem); |
| 3777 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3778 | up_write(&rbd_dev->lock_rwsem); |
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 | mutex_lock(&rbd_dev->watch_mutex); |
| 3781 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3782 | __rbd_unregister_watch(rbd_dev); |
| 3783 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3784 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3785 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3786 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3787 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3791 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3792 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3793 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3794 | { |
| 3795 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3796 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3797 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3798 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3799 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3800 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3801 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3802 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3803 | rbd_watch_errcb, rbd_dev); |
| 3804 | if (IS_ERR(handle)) |
| 3805 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3806 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3807 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3808 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3809 | } |
| 3810 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3811 | /* |
| 3812 | * watch_mutex must be locked |
| 3813 | */ |
| 3814 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3815 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3816 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3817 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3818 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3819 | rbd_assert(rbd_dev->watch_handle); |
| 3820 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3821 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3822 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3823 | if (ret) |
| 3824 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3825 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3826 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3827 | } |
| 3828 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3829 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3830 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3831 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3832 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3833 | mutex_lock(&rbd_dev->watch_mutex); |
| 3834 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3835 | ret = __rbd_register_watch(rbd_dev); |
| 3836 | if (ret) |
| 3837 | goto out; |
| 3838 | |
| 3839 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3840 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3841 | |
| 3842 | out: |
| 3843 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3844 | return ret; |
| 3845 | } |
| 3846 | |
| 3847 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3848 | { |
| 3849 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3850 | |
| 3851 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3852 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3853 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3854 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3855 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3859 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3860 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3861 | cancel_tasks_sync(rbd_dev); |
| 3862 | |
| 3863 | mutex_lock(&rbd_dev->watch_mutex); |
| 3864 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3865 | __rbd_unregister_watch(rbd_dev); |
| 3866 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3867 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3868 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3869 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3870 | } |
| 3871 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3872 | static void rbd_reregister_watch(struct work_struct *work) |
| 3873 | { |
| 3874 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3875 | struct rbd_device, watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3876 | bool was_lock_owner = false; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3877 | bool need_to_wake = false; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3878 | int ret; |
| 3879 | |
| 3880 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3881 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3882 | down_write(&rbd_dev->lock_rwsem); |
| 3883 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3884 | was_lock_owner = rbd_release_lock(rbd_dev); |
| 3885 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3886 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3887 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3888 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3889 | goto out; |
| 3890 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3891 | |
| 3892 | ret = __rbd_register_watch(rbd_dev); |
| 3893 | if (ret) { |
| 3894 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3895 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3896 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3897 | need_to_wake = true; |
| 3898 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3899 | queue_delayed_work(rbd_dev->task_wq, |
| 3900 | &rbd_dev->watch_dwork, |
| 3901 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3902 | } |
| 3903 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3904 | goto out; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3907 | need_to_wake = true; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3908 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3909 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3910 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3911 | |
| 3912 | ret = rbd_dev_refresh(rbd_dev); |
| 3913 | if (ret) |
| 3914 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
| 3915 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3916 | if (was_lock_owner) { |
| 3917 | ret = rbd_try_lock(rbd_dev); |
| 3918 | if (ret) |
| 3919 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", |
| 3920 | ret); |
| 3921 | } |
| 3922 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3923 | out: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3924 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3925 | if (need_to_wake) |
| 3926 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3927 | } |
| 3928 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3929 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3930 | * Synchronous osd object method call. Returns the number of bytes |
| 3931 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3932 | */ |
| 3933 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3934 | struct ceph_object_id *oid, |
| 3935 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3936 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3937 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3938 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3939 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3940 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3941 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3942 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3943 | struct page *req_page = NULL; |
| 3944 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3945 | int ret; |
| 3946 | |
| 3947 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3948 | * Method calls are ultimately read operations. The result |
| 3949 | * should placed into the inbound buffer provided. They |
| 3950 | * also supply outbound data--parameters for the object |
| 3951 | * method. Currently if this is present it will be a |
| 3952 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3953 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3954 | if (outbound) { |
| 3955 | if (outbound_size > PAGE_SIZE) |
| 3956 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3957 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3958 | req_page = alloc_page(GFP_KERNEL); |
| 3959 | if (!req_page) |
| 3960 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3961 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3962 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3963 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3964 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3965 | reply_page = alloc_page(GFP_KERNEL); |
| 3966 | if (!reply_page) { |
| 3967 | if (req_page) |
| 3968 | __free_page(req_page); |
| 3969 | return -ENOMEM; |
| 3970 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3971 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3972 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3973 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3974 | reply_page, &inbound_size); |
| 3975 | if (!ret) { |
| 3976 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3977 | ret = inbound_size; |
| 3978 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3979 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3980 | if (req_page) |
| 3981 | __free_page(req_page); |
| 3982 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3983 | return ret; |
| 3984 | } |
| 3985 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3986 | /* |
| 3987 | * lock_rwsem must be held for read |
| 3988 | */ |
| 3989 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3990 | { |
| 3991 | DEFINE_WAIT(wait); |
| 3992 | |
| 3993 | do { |
| 3994 | /* |
| 3995 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3996 | * and cancel_delayed_work() in wake_requests(). |
| 3997 | */ |
| 3998 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3999 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 4000 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 4001 | TASK_UNINTERRUPTIBLE); |
| 4002 | up_read(&rbd_dev->lock_rwsem); |
| 4003 | schedule(); |
| 4004 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4005 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4006 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 4007 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4008 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 4009 | } |
| 4010 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4011 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4012 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4013 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 4014 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4015 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4016 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4017 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4018 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4019 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4020 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4021 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4022 | int result; |
| 4023 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4024 | if (rq->cmd_type != REQ_TYPE_FS) { |
| 4025 | dout("%s: non-fs request type %d\n", __func__, |
| 4026 | (int) rq->cmd_type); |
| 4027 | result = -EIO; |
| 4028 | goto err; |
| 4029 | } |
| 4030 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4031 | if (req_op(rq) == REQ_OP_DISCARD) |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4032 | op_type = OBJ_OP_DISCARD; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4033 | else if (req_op(rq) == REQ_OP_WRITE) |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4034 | op_type = OBJ_OP_WRITE; |
| 4035 | else |
| 4036 | op_type = OBJ_OP_READ; |
| 4037 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4038 | /* Ignore/skip any zero-length requests */ |
| 4039 | |
| 4040 | if (!length) { |
| 4041 | dout("%s: zero-length request\n", __func__); |
| 4042 | result = 0; |
| 4043 | goto err_rq; |
| 4044 | } |
| 4045 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4046 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4047 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4048 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4049 | if (rbd_dev->mapping.read_only) { |
| 4050 | result = -EROFS; |
| 4051 | goto err_rq; |
| 4052 | } |
| 4053 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4054 | } |
| 4055 | |
| 4056 | /* |
| 4057 | * Quit early if the mapped snapshot no longer exists. It's |
| 4058 | * still possible the snapshot will have disappeared by the |
| 4059 | * time our request arrives at the osd, but there's no sense in |
| 4060 | * sending it if we already know. |
| 4061 | */ |
| 4062 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4063 | dout("request for non-existent snapshot"); |
| 4064 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4065 | result = -ENXIO; |
| 4066 | goto err_rq; |
| 4067 | } |
| 4068 | |
| 4069 | if (offset && length > U64_MAX - offset + 1) { |
| 4070 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4071 | length); |
| 4072 | result = -EINVAL; |
| 4073 | goto err_rq; /* Shouldn't happen */ |
| 4074 | } |
| 4075 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4076 | blk_mq_start_request(rq); |
| 4077 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4078 | down_read(&rbd_dev->header_rwsem); |
| 4079 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4080 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4081 | snapc = rbd_dev->header.snapc; |
| 4082 | ceph_get_snap_context(snapc); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4083 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4084 | } else { |
| 4085 | must_be_locked = rbd_dev->opts->lock_on_read && |
| 4086 | rbd_is_lock_supported(rbd_dev); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4087 | } |
| 4088 | up_read(&rbd_dev->header_rwsem); |
| 4089 | |
| 4090 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4091 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4092 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4093 | result = -EIO; |
| 4094 | goto err_rq; |
| 4095 | } |
| 4096 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4097 | if (must_be_locked) { |
| 4098 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4099 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4100 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4101 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4102 | |
| 4103 | WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^ |
| 4104 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 4105 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4106 | result = -EBLACKLISTED; |
| 4107 | goto err_unlock; |
| 4108 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4109 | } |
| 4110 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4111 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4112 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4113 | if (!img_request) { |
| 4114 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4115 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4116 | } |
| 4117 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4118 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4119 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4120 | if (op_type == OBJ_OP_DISCARD) |
| 4121 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4122 | NULL); |
| 4123 | else |
| 4124 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4125 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4126 | if (result) |
| 4127 | goto err_img_request; |
| 4128 | |
| 4129 | result = rbd_img_request_submit(img_request); |
| 4130 | if (result) |
| 4131 | goto err_img_request; |
| 4132 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4133 | if (must_be_locked) |
| 4134 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4135 | return; |
| 4136 | |
| 4137 | err_img_request: |
| 4138 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4139 | err_unlock: |
| 4140 | if (must_be_locked) |
| 4141 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4142 | err_rq: |
| 4143 | if (result) |
| 4144 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4145 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4146 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4147 | err: |
| 4148 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4149 | } |
| 4150 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4151 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4152 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4153 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4154 | struct request *rq = bd->rq; |
| 4155 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4156 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4157 | queue_work(rbd_wq, work); |
| 4158 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4159 | } |
| 4160 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4161 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4162 | { |
| 4163 | struct gendisk *disk = rbd_dev->disk; |
| 4164 | |
| 4165 | if (!disk) |
| 4166 | return; |
| 4167 | |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4168 | rbd_dev->disk = NULL; |
| 4169 | if (disk->flags & GENHD_FL_UP) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4170 | del_gendisk(disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4171 | if (disk->queue) |
| 4172 | blk_cleanup_queue(disk->queue); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4173 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4174 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4175 | put_disk(disk); |
| 4176 | } |
| 4177 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4178 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4179 | struct ceph_object_id *oid, |
| 4180 | struct ceph_object_locator *oloc, |
| 4181 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4182 | |
| 4183 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4184 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4185 | struct ceph_osd_request *req; |
| 4186 | struct page **pages; |
| 4187 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4188 | int ret; |
| 4189 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4190 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4191 | if (!req) |
| 4192 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4193 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4194 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4195 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4196 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4197 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4198 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4199 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4200 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4201 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4202 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4203 | if (IS_ERR(pages)) { |
| 4204 | ret = PTR_ERR(pages); |
| 4205 | goto out_req; |
| 4206 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4207 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4208 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4209 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4210 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4211 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4212 | ceph_osdc_start_request(osdc, req, false); |
| 4213 | ret = ceph_osdc_wait_request(osdc, req); |
| 4214 | if (ret >= 0) |
| 4215 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4216 | |
| 4217 | out_req: |
| 4218 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4219 | return ret; |
| 4220 | } |
| 4221 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4222 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4223 | * Read the complete header for the given rbd device. On successful |
| 4224 | * return, the rbd_dev->header field will contain up-to-date |
| 4225 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4226 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4227 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4228 | { |
| 4229 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4230 | u32 snap_count = 0; |
| 4231 | u64 names_size = 0; |
| 4232 | u32 want_count; |
| 4233 | int ret; |
| 4234 | |
| 4235 | /* |
| 4236 | * The complete header will include an array of its 64-bit |
| 4237 | * snapshot ids, followed by the names of those snapshots as |
| 4238 | * a contiguous block of NUL-terminated strings. Note that |
| 4239 | * the number of snapshots could change by the time we read |
| 4240 | * it in, in which case we re-read it. |
| 4241 | */ |
| 4242 | do { |
| 4243 | size_t size; |
| 4244 | |
| 4245 | kfree(ondisk); |
| 4246 | |
| 4247 | size = sizeof (*ondisk); |
| 4248 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4249 | size += names_size; |
| 4250 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4251 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4252 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4253 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4254 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4255 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4256 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4257 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4258 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4259 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4260 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4261 | size, ret); |
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 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4265 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4266 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4267 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4271 | want_count = snap_count; |
| 4272 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4273 | } while (snap_count != want_count); |
| 4274 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4275 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4276 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4277 | kfree(ondisk); |
| 4278 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4279 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4282 | /* |
| 4283 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4284 | * has disappeared from the (just updated) snapshot context. |
| 4285 | */ |
| 4286 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4287 | { |
| 4288 | u64 snap_id; |
| 4289 | |
| 4290 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4291 | return; |
| 4292 | |
| 4293 | snap_id = rbd_dev->spec->snap_id; |
| 4294 | if (snap_id == CEPH_NOSNAP) |
| 4295 | return; |
| 4296 | |
| 4297 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4298 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4299 | } |
| 4300 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4301 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4302 | { |
| 4303 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4304 | |
| 4305 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4306 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4307 | * try to update its size. If REMOVING is set, updating size |
| 4308 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4309 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4310 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4311 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4312 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4313 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4314 | set_capacity(rbd_dev->disk, size); |
| 4315 | revalidate_disk(rbd_dev->disk); |
| 4316 | } |
| 4317 | } |
| 4318 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4319 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4320 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4321 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4322 | int ret; |
| 4323 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4324 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4325 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4326 | |
| 4327 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4328 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4329 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4330 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4331 | /* |
| 4332 | * If there is a parent, see if it has disappeared due to the |
| 4333 | * mapped image getting flattened. |
| 4334 | */ |
| 4335 | if (rbd_dev->parent) { |
| 4336 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4337 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4338 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4339 | } |
| 4340 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4341 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4342 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4343 | } else { |
| 4344 | /* validate mapped snapshot's EXISTS flag */ |
| 4345 | rbd_exists_validate(rbd_dev); |
| 4346 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4347 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4348 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4349 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4350 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4351 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4352 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4353 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4354 | } |
| 4355 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4356 | static int rbd_init_request(void *data, struct request *rq, |
| 4357 | unsigned int hctx_idx, unsigned int request_idx, |
| 4358 | unsigned int numa_node) |
| 4359 | { |
| 4360 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4361 | |
| 4362 | INIT_WORK(work, rbd_queue_workfn); |
| 4363 | return 0; |
| 4364 | } |
| 4365 | |
| 4366 | static struct blk_mq_ops rbd_mq_ops = { |
| 4367 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4368 | .init_request = rbd_init_request, |
| 4369 | }; |
| 4370 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4371 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4372 | { |
| 4373 | struct gendisk *disk; |
| 4374 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4375 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4376 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4377 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4378 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4379 | disk = alloc_disk(single_major ? |
| 4380 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4381 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4382 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4383 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4384 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4385 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4386 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4387 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4388 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4389 | if (single_major) |
| 4390 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4391 | disk->fops = &rbd_bd_ops; |
| 4392 | disk->private_data = rbd_dev; |
| 4393 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4394 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4395 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4396 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4397 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4398 | 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] | 4399 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4400 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4401 | |
| 4402 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4403 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4404 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4405 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4406 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4407 | if (IS_ERR(q)) { |
| 4408 | err = PTR_ERR(q); |
| 4409 | goto out_tag_set; |
| 4410 | } |
| 4411 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4412 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4413 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4414 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4415 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4416 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4417 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4418 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4419 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4420 | blk_queue_max_segment_size(q, segment_size); |
| 4421 | blk_queue_io_min(q, segment_size); |
| 4422 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4423 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4424 | /* enable the discard support */ |
| 4425 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4426 | q->limits.discard_granularity = segment_size; |
| 4427 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4428 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4429 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4430 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4431 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
| 4432 | q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 4433 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4434 | disk->queue = q; |
| 4435 | |
| 4436 | q->queuedata = rbd_dev; |
| 4437 | |
| 4438 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4439 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4440 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4441 | out_tag_set: |
| 4442 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4443 | out_disk: |
| 4444 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4445 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4446 | } |
| 4447 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4448 | /* |
| 4449 | sysfs |
| 4450 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4451 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4452 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4453 | { |
| 4454 | return container_of(dev, struct rbd_device, dev); |
| 4455 | } |
| 4456 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4457 | static ssize_t rbd_size_show(struct device *dev, |
| 4458 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4459 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4460 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4461 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4462 | return sprintf(buf, "%llu\n", |
| 4463 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4464 | } |
| 4465 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4466 | /* |
| 4467 | * Note this shows the features for whatever's mapped, which is not |
| 4468 | * necessarily the base image. |
| 4469 | */ |
| 4470 | static ssize_t rbd_features_show(struct device *dev, |
| 4471 | struct device_attribute *attr, char *buf) |
| 4472 | { |
| 4473 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4474 | |
| 4475 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4476 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4477 | } |
| 4478 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4479 | static ssize_t rbd_major_show(struct device *dev, |
| 4480 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4481 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4482 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4483 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4484 | if (rbd_dev->major) |
| 4485 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4486 | |
| 4487 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4488 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4489 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4490 | static ssize_t rbd_minor_show(struct device *dev, |
| 4491 | struct device_attribute *attr, char *buf) |
| 4492 | { |
| 4493 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4494 | |
| 4495 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4496 | } |
| 4497 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4498 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4499 | struct device_attribute *attr, char *buf) |
| 4500 | { |
| 4501 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4502 | struct ceph_entity_addr *client_addr = |
| 4503 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4504 | |
| 4505 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4506 | le32_to_cpu(client_addr->nonce)); |
| 4507 | } |
| 4508 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4509 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4510 | struct device_attribute *attr, char *buf) |
| 4511 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4512 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4513 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4514 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4515 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4516 | } |
| 4517 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4518 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4519 | struct device_attribute *attr, char *buf) |
| 4520 | { |
| 4521 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4522 | |
| 4523 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4524 | } |
| 4525 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4526 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4527 | struct device_attribute *attr, char *buf) |
| 4528 | { |
| 4529 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4530 | |
| 4531 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | static ssize_t rbd_pool_show(struct device *dev, |
| 4535 | struct device_attribute *attr, char *buf) |
| 4536 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4537 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4538 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4539 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4540 | } |
| 4541 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4542 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4543 | struct device_attribute *attr, char *buf) |
| 4544 | { |
| 4545 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4546 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4547 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4548 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4549 | } |
| 4550 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4551 | static ssize_t rbd_name_show(struct device *dev, |
| 4552 | struct device_attribute *attr, char *buf) |
| 4553 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4554 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4555 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4556 | if (rbd_dev->spec->image_name) |
| 4557 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4558 | |
| 4559 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4562 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4563 | struct device_attribute *attr, char *buf) |
| 4564 | { |
| 4565 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4566 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4567 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4568 | } |
| 4569 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4570 | /* |
| 4571 | * Shows the name of the currently-mapped snapshot (or |
| 4572 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4573 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4574 | static ssize_t rbd_snap_show(struct device *dev, |
| 4575 | struct device_attribute *attr, |
| 4576 | char *buf) |
| 4577 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4578 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4579 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4580 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4581 | } |
| 4582 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4583 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4584 | struct device_attribute *attr, char *buf) |
| 4585 | { |
| 4586 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4587 | |
| 4588 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4589 | } |
| 4590 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4591 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4592 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4593 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4594 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4595 | */ |
| 4596 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4597 | struct device_attribute *attr, |
| 4598 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4599 | { |
| 4600 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4601 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4602 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4603 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4604 | return sprintf(buf, "(no parent image)\n"); |
| 4605 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4606 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4607 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4608 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4609 | count += sprintf(&buf[count], "%s" |
| 4610 | "pool_id %llu\npool_name %s\n" |
| 4611 | "image_id %s\nimage_name %s\n" |
| 4612 | "snap_id %llu\nsnap_name %s\n" |
| 4613 | "overlap %llu\n", |
| 4614 | !count ? "" : "\n", /* first? */ |
| 4615 | spec->pool_id, spec->pool_name, |
| 4616 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4617 | spec->snap_id, spec->snap_name, |
| 4618 | rbd_dev->parent_overlap); |
| 4619 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4620 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4621 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4622 | } |
| 4623 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4624 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4625 | struct device_attribute *attr, |
| 4626 | const char *buf, |
| 4627 | size_t size) |
| 4628 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4629 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4630 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4631 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4632 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4633 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4634 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4635 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4636 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4637 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4638 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4639 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4640 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4641 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4642 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4643 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4644 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4645 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4646 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4647 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4648 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4649 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4650 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4651 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4652 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4653 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4654 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4655 | |
| 4656 | static struct attribute *rbd_attrs[] = { |
| 4657 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4658 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4659 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4660 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4661 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4662 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4663 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4664 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4665 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4666 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4667 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4668 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4669 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4670 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4671 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4672 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4673 | NULL |
| 4674 | }; |
| 4675 | |
| 4676 | static struct attribute_group rbd_attr_group = { |
| 4677 | .attrs = rbd_attrs, |
| 4678 | }; |
| 4679 | |
| 4680 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4681 | &rbd_attr_group, |
| 4682 | NULL |
| 4683 | }; |
| 4684 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4685 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4686 | |
| 4687 | static struct device_type rbd_device_type = { |
| 4688 | .name = "rbd", |
| 4689 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4690 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4691 | }; |
| 4692 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4693 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4694 | { |
| 4695 | kref_get(&spec->kref); |
| 4696 | |
| 4697 | return spec; |
| 4698 | } |
| 4699 | |
| 4700 | static void rbd_spec_free(struct kref *kref); |
| 4701 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4702 | { |
| 4703 | if (spec) |
| 4704 | kref_put(&spec->kref, rbd_spec_free); |
| 4705 | } |
| 4706 | |
| 4707 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4708 | { |
| 4709 | struct rbd_spec *spec; |
| 4710 | |
| 4711 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4712 | if (!spec) |
| 4713 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4714 | |
| 4715 | spec->pool_id = CEPH_NOPOOL; |
| 4716 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4717 | kref_init(&spec->kref); |
| 4718 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4719 | return spec; |
| 4720 | } |
| 4721 | |
| 4722 | static void rbd_spec_free(struct kref *kref) |
| 4723 | { |
| 4724 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4725 | |
| 4726 | kfree(spec->pool_name); |
| 4727 | kfree(spec->image_id); |
| 4728 | kfree(spec->image_name); |
| 4729 | kfree(spec->snap_name); |
| 4730 | kfree(spec); |
| 4731 | } |
| 4732 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4733 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4734 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4735 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4736 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4737 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4738 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4739 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4740 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4741 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4742 | rbd_put_client(rbd_dev->rbd_client); |
| 4743 | rbd_spec_put(rbd_dev->spec); |
| 4744 | kfree(rbd_dev->opts); |
| 4745 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | static void rbd_dev_release(struct device *dev) |
| 4749 | { |
| 4750 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4751 | bool need_put = !!rbd_dev->opts; |
| 4752 | |
| 4753 | if (need_put) { |
| 4754 | destroy_workqueue(rbd_dev->task_wq); |
| 4755 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4756 | } |
| 4757 | |
| 4758 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4759 | |
| 4760 | /* |
| 4761 | * This is racy, but way better than putting module outside of |
| 4762 | * the release callback. The race window is pretty small, so |
| 4763 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4764 | */ |
| 4765 | if (need_put) |
| 4766 | module_put(THIS_MODULE); |
| 4767 | } |
| 4768 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4769 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4770 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4771 | { |
| 4772 | struct rbd_device *rbd_dev; |
| 4773 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4774 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4775 | if (!rbd_dev) |
| 4776 | return NULL; |
| 4777 | |
| 4778 | spin_lock_init(&rbd_dev->lock); |
| 4779 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4780 | init_rwsem(&rbd_dev->header_rwsem); |
| 4781 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4782 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4783 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4784 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4785 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4786 | mutex_init(&rbd_dev->watch_mutex); |
| 4787 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4788 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4789 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4790 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4791 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4792 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4793 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4794 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4795 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4796 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4797 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4798 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4799 | rbd_dev->dev.type = &rbd_device_type; |
| 4800 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4801 | device_initialize(&rbd_dev->dev); |
| 4802 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4803 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4804 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4805 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4806 | return rbd_dev; |
| 4807 | } |
| 4808 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4809 | /* |
| 4810 | * Create a mapping rbd_dev. |
| 4811 | */ |
| 4812 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4813 | struct rbd_spec *spec, |
| 4814 | struct rbd_options *opts) |
| 4815 | { |
| 4816 | struct rbd_device *rbd_dev; |
| 4817 | |
| 4818 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4819 | if (!rbd_dev) |
| 4820 | return NULL; |
| 4821 | |
| 4822 | rbd_dev->opts = opts; |
| 4823 | |
| 4824 | /* get an id and fill in device name */ |
| 4825 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4826 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4827 | GFP_KERNEL); |
| 4828 | if (rbd_dev->dev_id < 0) |
| 4829 | goto fail_rbd_dev; |
| 4830 | |
| 4831 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4832 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4833 | rbd_dev->name); |
| 4834 | if (!rbd_dev->task_wq) |
| 4835 | goto fail_dev_id; |
| 4836 | |
| 4837 | /* we have a ref from do_rbd_add() */ |
| 4838 | __module_get(THIS_MODULE); |
| 4839 | |
| 4840 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4841 | return rbd_dev; |
| 4842 | |
| 4843 | fail_dev_id: |
| 4844 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4845 | fail_rbd_dev: |
| 4846 | rbd_dev_free(rbd_dev); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4850 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4851 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4852 | if (rbd_dev) |
| 4853 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4854 | } |
| 4855 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4856 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4857 | * Get the size and object order for an image snapshot, or if |
| 4858 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4859 | * image. |
| 4860 | */ |
| 4861 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4862 | u8 *order, u64 *snap_size) |
| 4863 | { |
| 4864 | __le64 snapid = cpu_to_le64(snap_id); |
| 4865 | int ret; |
| 4866 | struct { |
| 4867 | u8 order; |
| 4868 | __le64 size; |
| 4869 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4870 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4871 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4872 | &rbd_dev->header_oloc, "get_size", |
| 4873 | &snapid, sizeof(snapid), |
| 4874 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4875 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4876 | if (ret < 0) |
| 4877 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4878 | if (ret < sizeof (size_buf)) |
| 4879 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4880 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4881 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4882 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4883 | dout(" order %u", (unsigned int)*order); |
| 4884 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4885 | *snap_size = le64_to_cpu(size_buf.size); |
| 4886 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4887 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4888 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4889 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4890 | |
| 4891 | return 0; |
| 4892 | } |
| 4893 | |
| 4894 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4895 | { |
| 4896 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4897 | &rbd_dev->header.obj_order, |
| 4898 | &rbd_dev->header.image_size); |
| 4899 | } |
| 4900 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4901 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4902 | { |
| 4903 | void *reply_buf; |
| 4904 | int ret; |
| 4905 | void *p; |
| 4906 | |
| 4907 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4908 | if (!reply_buf) |
| 4909 | return -ENOMEM; |
| 4910 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4911 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4912 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4913 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4914 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4915 | if (ret < 0) |
| 4916 | goto out; |
| 4917 | |
| 4918 | p = reply_buf; |
| 4919 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4920 | p + ret, NULL, GFP_NOIO); |
| 4921 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4922 | |
| 4923 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4924 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4925 | rbd_dev->header.object_prefix = NULL; |
| 4926 | } else { |
| 4927 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4928 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4929 | out: |
| 4930 | kfree(reply_buf); |
| 4931 | |
| 4932 | return ret; |
| 4933 | } |
| 4934 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4935 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4936 | u64 *snap_features) |
| 4937 | { |
| 4938 | __le64 snapid = cpu_to_le64(snap_id); |
| 4939 | struct { |
| 4940 | __le64 features; |
| 4941 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4942 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4943 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4944 | int ret; |
| 4945 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4946 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4947 | &rbd_dev->header_oloc, "get_features", |
| 4948 | &snapid, sizeof(snapid), |
| 4949 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4950 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4951 | if (ret < 0) |
| 4952 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4953 | if (ret < sizeof (features_buf)) |
| 4954 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4955 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4956 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4957 | if (unsup) { |
| 4958 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4959 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4960 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4961 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4962 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4963 | *snap_features = le64_to_cpu(features_buf.features); |
| 4964 | |
| 4965 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4966 | (unsigned long long)snap_id, |
| 4967 | (unsigned long long)*snap_features, |
| 4968 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4969 | |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4974 | { |
| 4975 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4976 | &rbd_dev->header.features); |
| 4977 | } |
| 4978 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4979 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4980 | { |
| 4981 | struct rbd_spec *parent_spec; |
| 4982 | size_t size; |
| 4983 | void *reply_buf = NULL; |
| 4984 | __le64 snapid; |
| 4985 | void *p; |
| 4986 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4987 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4988 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4989 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4990 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4991 | int ret; |
| 4992 | |
| 4993 | parent_spec = rbd_spec_alloc(); |
| 4994 | if (!parent_spec) |
| 4995 | return -ENOMEM; |
| 4996 | |
| 4997 | size = sizeof (__le64) + /* pool_id */ |
| 4998 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4999 | sizeof (__le64) + /* snap_id */ |
| 5000 | sizeof (__le64); /* overlap */ |
| 5001 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5002 | if (!reply_buf) { |
| 5003 | ret = -ENOMEM; |
| 5004 | goto out_err; |
| 5005 | } |
| 5006 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5007 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5008 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5009 | &rbd_dev->header_oloc, "get_parent", |
| 5010 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5011 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5012 | if (ret < 0) |
| 5013 | goto out_err; |
| 5014 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5015 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5016 | end = reply_buf + ret; |
| 5017 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5018 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5019 | if (pool_id == CEPH_NOPOOL) { |
| 5020 | /* |
| 5021 | * Either the parent never existed, or we have |
| 5022 | * record of it but the image got flattened so it no |
| 5023 | * longer has a parent. When the parent of a |
| 5024 | * layered image disappears we immediately set the |
| 5025 | * overlap to 0. The effect of this is that all new |
| 5026 | * requests will be treated as if the image had no |
| 5027 | * parent. |
| 5028 | */ |
| 5029 | if (rbd_dev->parent_overlap) { |
| 5030 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5031 | rbd_dev_parent_put(rbd_dev); |
| 5032 | pr_info("%s: clone image has been flattened\n", |
| 5033 | rbd_dev->disk->disk_name); |
| 5034 | } |
| 5035 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5036 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5037 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5038 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5039 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5040 | |
| 5041 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5042 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5043 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5044 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5045 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5046 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5047 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5048 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5049 | if (IS_ERR(image_id)) { |
| 5050 | ret = PTR_ERR(image_id); |
| 5051 | goto out_err; |
| 5052 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5053 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5054 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5055 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5056 | /* |
| 5057 | * The parent won't change (except when the clone is |
| 5058 | * flattened, already handled that). So we only need to |
| 5059 | * record the parent spec we have not already done so. |
| 5060 | */ |
| 5061 | if (!rbd_dev->parent_spec) { |
| 5062 | parent_spec->pool_id = pool_id; |
| 5063 | parent_spec->image_id = image_id; |
| 5064 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5065 | rbd_dev->parent_spec = parent_spec; |
| 5066 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5067 | } else { |
| 5068 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5072 | * We always update the parent overlap. If it's zero we issue |
| 5073 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5074 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5075 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5076 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5077 | /* refresh, careful to warn just once */ |
| 5078 | if (rbd_dev->parent_overlap) |
| 5079 | rbd_warn(rbd_dev, |
| 5080 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5081 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5082 | /* initial probe */ |
| 5083 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5084 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5085 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5086 | rbd_dev->parent_overlap = overlap; |
| 5087 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5088 | out: |
| 5089 | ret = 0; |
| 5090 | out_err: |
| 5091 | kfree(reply_buf); |
| 5092 | rbd_spec_put(parent_spec); |
| 5093 | |
| 5094 | return ret; |
| 5095 | } |
| 5096 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5097 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5098 | { |
| 5099 | struct { |
| 5100 | __le64 stripe_unit; |
| 5101 | __le64 stripe_count; |
| 5102 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5103 | size_t size = sizeof (striping_info_buf); |
| 5104 | void *p; |
| 5105 | u64 obj_size; |
| 5106 | u64 stripe_unit; |
| 5107 | u64 stripe_count; |
| 5108 | int ret; |
| 5109 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5110 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5111 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5112 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5113 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5114 | if (ret < 0) |
| 5115 | return ret; |
| 5116 | if (ret < size) |
| 5117 | return -ERANGE; |
| 5118 | |
| 5119 | /* |
| 5120 | * We don't actually support the "fancy striping" feature |
| 5121 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5122 | * defaults the behavior is the same as before. So find |
| 5123 | * out, and only fail if the image has non-default values. |
| 5124 | */ |
| 5125 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5126 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5127 | p = &striping_info_buf; |
| 5128 | stripe_unit = ceph_decode_64(&p); |
| 5129 | if (stripe_unit != obj_size) { |
| 5130 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5131 | "(got %llu want %llu)", |
| 5132 | stripe_unit, obj_size); |
| 5133 | return -EINVAL; |
| 5134 | } |
| 5135 | stripe_count = ceph_decode_64(&p); |
| 5136 | if (stripe_count != 1) { |
| 5137 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5138 | "(got %llu want 1)", stripe_count); |
| 5139 | return -EINVAL; |
| 5140 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5141 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5142 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5143 | |
| 5144 | return 0; |
| 5145 | } |
| 5146 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5147 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5148 | { |
| 5149 | __le64 data_pool_id; |
| 5150 | int ret; |
| 5151 | |
| 5152 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5153 | &rbd_dev->header_oloc, "get_data_pool", |
| 5154 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5155 | if (ret < 0) |
| 5156 | return ret; |
| 5157 | if (ret < sizeof(data_pool_id)) |
| 5158 | return -EBADMSG; |
| 5159 | |
| 5160 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5161 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5162 | return 0; |
| 5163 | } |
| 5164 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5165 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5166 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5167 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5168 | size_t image_id_size; |
| 5169 | char *image_id; |
| 5170 | void *p; |
| 5171 | void *end; |
| 5172 | size_t size; |
| 5173 | void *reply_buf = NULL; |
| 5174 | size_t len = 0; |
| 5175 | char *image_name = NULL; |
| 5176 | int ret; |
| 5177 | |
| 5178 | rbd_assert(!rbd_dev->spec->image_name); |
| 5179 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5180 | len = strlen(rbd_dev->spec->image_id); |
| 5181 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5182 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5183 | if (!image_id) |
| 5184 | return NULL; |
| 5185 | |
| 5186 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5187 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5188 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5189 | |
| 5190 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5191 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5192 | if (!reply_buf) |
| 5193 | goto out; |
| 5194 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5195 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5196 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5197 | "dir_get_name", image_id, image_id_size, |
| 5198 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5199 | if (ret < 0) |
| 5200 | goto out; |
| 5201 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5202 | end = reply_buf + ret; |
| 5203 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5204 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5205 | if (IS_ERR(image_name)) |
| 5206 | image_name = NULL; |
| 5207 | else |
| 5208 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5209 | out: |
| 5210 | kfree(reply_buf); |
| 5211 | kfree(image_id); |
| 5212 | |
| 5213 | return image_name; |
| 5214 | } |
| 5215 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5216 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5217 | { |
| 5218 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5219 | const char *snap_name; |
| 5220 | u32 which = 0; |
| 5221 | |
| 5222 | /* Skip over names until we find the one we are looking for */ |
| 5223 | |
| 5224 | snap_name = rbd_dev->header.snap_names; |
| 5225 | while (which < snapc->num_snaps) { |
| 5226 | if (!strcmp(name, snap_name)) |
| 5227 | return snapc->snaps[which]; |
| 5228 | snap_name += strlen(snap_name) + 1; |
| 5229 | which++; |
| 5230 | } |
| 5231 | return CEPH_NOSNAP; |
| 5232 | } |
| 5233 | |
| 5234 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5235 | { |
| 5236 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5237 | u32 which; |
| 5238 | bool found = false; |
| 5239 | u64 snap_id; |
| 5240 | |
| 5241 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5242 | const char *snap_name; |
| 5243 | |
| 5244 | snap_id = snapc->snaps[which]; |
| 5245 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5246 | if (IS_ERR(snap_name)) { |
| 5247 | /* ignore no-longer existing snapshots */ |
| 5248 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5249 | continue; |
| 5250 | else |
| 5251 | break; |
| 5252 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5253 | found = !strcmp(name, snap_name); |
| 5254 | kfree(snap_name); |
| 5255 | } |
| 5256 | return found ? snap_id : CEPH_NOSNAP; |
| 5257 | } |
| 5258 | |
| 5259 | /* |
| 5260 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5261 | * no snapshot by that name is found, or if an error occurs. |
| 5262 | */ |
| 5263 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5264 | { |
| 5265 | if (rbd_dev->image_format == 1) |
| 5266 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5267 | |
| 5268 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5269 | } |
| 5270 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5271 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5272 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5273 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5274 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5275 | { |
| 5276 | struct rbd_spec *spec = rbd_dev->spec; |
| 5277 | |
| 5278 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5279 | rbd_assert(spec->image_id && spec->image_name); |
| 5280 | rbd_assert(spec->snap_name); |
| 5281 | |
| 5282 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5283 | u64 snap_id; |
| 5284 | |
| 5285 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5286 | if (snap_id == CEPH_NOSNAP) |
| 5287 | return -ENOENT; |
| 5288 | |
| 5289 | spec->snap_id = snap_id; |
| 5290 | } else { |
| 5291 | spec->snap_id = CEPH_NOSNAP; |
| 5292 | } |
| 5293 | |
| 5294 | return 0; |
| 5295 | } |
| 5296 | |
| 5297 | /* |
| 5298 | * A parent image will have all ids but none of the names. |
| 5299 | * |
| 5300 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5301 | * can't figure out the name for an image id. |
| 5302 | */ |
| 5303 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5304 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5305 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5306 | struct rbd_spec *spec = rbd_dev->spec; |
| 5307 | const char *pool_name; |
| 5308 | const char *image_name; |
| 5309 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5310 | int ret; |
| 5311 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5312 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5313 | rbd_assert(spec->image_id); |
| 5314 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5315 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5316 | /* 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] | 5317 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5318 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5319 | if (!pool_name) { |
| 5320 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5321 | return -EIO; |
| 5322 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5323 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5324 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5325 | return -ENOMEM; |
| 5326 | |
| 5327 | /* Fetch the image name; tolerate failure here */ |
| 5328 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5329 | image_name = rbd_dev_image_name(rbd_dev); |
| 5330 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5331 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5332 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5333 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5334 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5335 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5336 | if (IS_ERR(snap_name)) { |
| 5337 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5338 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5339 | } |
| 5340 | |
| 5341 | spec->pool_name = pool_name; |
| 5342 | spec->image_name = image_name; |
| 5343 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5344 | |
| 5345 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5346 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5347 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5348 | kfree(image_name); |
| 5349 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5350 | return ret; |
| 5351 | } |
| 5352 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5353 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5354 | { |
| 5355 | size_t size; |
| 5356 | int ret; |
| 5357 | void *reply_buf; |
| 5358 | void *p; |
| 5359 | void *end; |
| 5360 | u64 seq; |
| 5361 | u32 snap_count; |
| 5362 | struct ceph_snap_context *snapc; |
| 5363 | u32 i; |
| 5364 | |
| 5365 | /* |
| 5366 | * We'll need room for the seq value (maximum snapshot id), |
| 5367 | * snapshot count, and array of that many snapshot ids. |
| 5368 | * For now we have a fixed upper limit on the number we're |
| 5369 | * prepared to receive. |
| 5370 | */ |
| 5371 | size = sizeof (__le64) + sizeof (__le32) + |
| 5372 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5373 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5374 | if (!reply_buf) |
| 5375 | return -ENOMEM; |
| 5376 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5377 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5378 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5379 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5380 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5381 | if (ret < 0) |
| 5382 | goto out; |
| 5383 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5384 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5385 | end = reply_buf + ret; |
| 5386 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5387 | ceph_decode_64_safe(&p, end, seq, out); |
| 5388 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5389 | |
| 5390 | /* |
| 5391 | * Make sure the reported number of snapshot ids wouldn't go |
| 5392 | * beyond the end of our buffer. But before checking that, |
| 5393 | * make sure the computed size of the snapshot context we |
| 5394 | * allocate is representable in a size_t. |
| 5395 | */ |
| 5396 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5397 | / sizeof (u64)) { |
| 5398 | ret = -EINVAL; |
| 5399 | goto out; |
| 5400 | } |
| 5401 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5402 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5403 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5404 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5405 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5406 | if (!snapc) { |
| 5407 | ret = -ENOMEM; |
| 5408 | goto out; |
| 5409 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5410 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5411 | for (i = 0; i < snap_count; i++) |
| 5412 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5413 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5414 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5415 | rbd_dev->header.snapc = snapc; |
| 5416 | |
| 5417 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5418 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5419 | out: |
| 5420 | kfree(reply_buf); |
| 5421 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5422 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5423 | } |
| 5424 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5425 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5426 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5427 | { |
| 5428 | size_t size; |
| 5429 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5430 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5431 | int ret; |
| 5432 | void *p; |
| 5433 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5434 | char *snap_name; |
| 5435 | |
| 5436 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5437 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5438 | if (!reply_buf) |
| 5439 | return ERR_PTR(-ENOMEM); |
| 5440 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5441 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5442 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5443 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5444 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5445 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5446 | if (ret < 0) { |
| 5447 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5448 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5449 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5450 | |
| 5451 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5452 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5453 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5454 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5455 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5456 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5457 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5458 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5459 | out: |
| 5460 | kfree(reply_buf); |
| 5461 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5462 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5463 | } |
| 5464 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5465 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5466 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5467 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5468 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5469 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5470 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5471 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5472 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5473 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5474 | if (first_time) { |
| 5475 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5476 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5477 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5478 | } |
| 5479 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5480 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5481 | if (ret && first_time) { |
| 5482 | kfree(rbd_dev->header.object_prefix); |
| 5483 | rbd_dev->header.object_prefix = NULL; |
| 5484 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5485 | |
| 5486 | return ret; |
| 5487 | } |
| 5488 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5489 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5490 | { |
| 5491 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5492 | |
| 5493 | if (rbd_dev->image_format == 1) |
| 5494 | return rbd_dev_v1_header_info(rbd_dev); |
| 5495 | |
| 5496 | return rbd_dev_v2_header_info(rbd_dev); |
| 5497 | } |
| 5498 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5499 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5500 | * Skips over white space at *buf, and updates *buf to point to the |
| 5501 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5502 | * the token (string of non-white space characters) found. Note |
| 5503 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5504 | */ |
| 5505 | static inline size_t next_token(const char **buf) |
| 5506 | { |
| 5507 | /* |
| 5508 | * These are the characters that produce nonzero for |
| 5509 | * isspace() in the "C" and "POSIX" locales. |
| 5510 | */ |
| 5511 | const char *spaces = " \f\n\r\t\v"; |
| 5512 | |
| 5513 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5514 | |
| 5515 | return strcspn(*buf, spaces); /* Return token length */ |
| 5516 | } |
| 5517 | |
| 5518 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5519 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5520 | * enough to hold a copy of it, and copies the token into the new |
| 5521 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5522 | * that a duplicate buffer is created even for a zero-length token. |
| 5523 | * |
| 5524 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5525 | * pointer if memory for the duplicate was not available. If |
| 5526 | * the lenp argument is a non-null pointer, the length of the token |
| 5527 | * (not including the '\0') is returned in *lenp. |
| 5528 | * |
| 5529 | * If successful, the *buf pointer will be updated to point beyond |
| 5530 | * the end of the found token. |
| 5531 | * |
| 5532 | * Note: uses GFP_KERNEL for allocation. |
| 5533 | */ |
| 5534 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5535 | { |
| 5536 | char *dup; |
| 5537 | size_t len; |
| 5538 | |
| 5539 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5540 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5541 | if (!dup) |
| 5542 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5543 | *(dup + len) = '\0'; |
| 5544 | *buf += len; |
| 5545 | |
| 5546 | if (lenp) |
| 5547 | *lenp = len; |
| 5548 | |
| 5549 | return dup; |
| 5550 | } |
| 5551 | |
| 5552 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5553 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5554 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5555 | * and the data written is passed here via a NUL-terminated buffer. |
| 5556 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5557 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5558 | * The information extracted from these options is recorded in |
| 5559 | * the other parameters which return dynamically-allocated |
| 5560 | * structures: |
| 5561 | * ceph_opts |
| 5562 | * The address of a pointer that will refer to a ceph options |
| 5563 | * structure. Caller must release the returned pointer using |
| 5564 | * ceph_destroy_options() when it is no longer needed. |
| 5565 | * rbd_opts |
| 5566 | * Address of an rbd options pointer. Fully initialized by |
| 5567 | * this function; caller must release with kfree(). |
| 5568 | * spec |
| 5569 | * Address of an rbd image specification pointer. Fully |
| 5570 | * initialized by this function based on parsed options. |
| 5571 | * Caller must release with rbd_spec_put(). |
| 5572 | * |
| 5573 | * The options passed take this form: |
| 5574 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5575 | * where: |
| 5576 | * <mon_addrs> |
| 5577 | * A comma-separated list of one or more monitor addresses. |
| 5578 | * A monitor address is an ip address, optionally followed |
| 5579 | * by a port number (separated by a colon). |
| 5580 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5581 | * <options> |
| 5582 | * A comma-separated list of ceph and/or rbd options. |
| 5583 | * <pool_name> |
| 5584 | * The name of the rados pool containing the rbd image. |
| 5585 | * <image_name> |
| 5586 | * The name of the image in that pool to map. |
| 5587 | * <snap_id> |
| 5588 | * An optional snapshot id. If provided, the mapping will |
| 5589 | * present data from the image at the time that snapshot was |
| 5590 | * created. The image head is used if no snapshot id is |
| 5591 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5592 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5593 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5594 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5595 | struct rbd_options **opts, |
| 5596 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5597 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5598 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5599 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5600 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5601 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5602 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5603 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5604 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5605 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5606 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5607 | |
| 5608 | /* The first four tokens are required */ |
| 5609 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5610 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5611 | if (!len) { |
| 5612 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5613 | return -EINVAL; |
| 5614 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5615 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5616 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5617 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5618 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5619 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5620 | options = dup_token(&buf, NULL); |
| 5621 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5622 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5623 | if (!*options) { |
| 5624 | rbd_warn(NULL, "no options provided"); |
| 5625 | goto out_err; |
| 5626 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5627 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5628 | spec = rbd_spec_alloc(); |
| 5629 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5630 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5631 | |
| 5632 | spec->pool_name = dup_token(&buf, NULL); |
| 5633 | if (!spec->pool_name) |
| 5634 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5635 | if (!*spec->pool_name) { |
| 5636 | rbd_warn(NULL, "no pool name provided"); |
| 5637 | goto out_err; |
| 5638 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5639 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5640 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5641 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5642 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5643 | if (!*spec->image_name) { |
| 5644 | rbd_warn(NULL, "no image name provided"); |
| 5645 | goto out_err; |
| 5646 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5647 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5648 | /* |
| 5649 | * Snapshot name is optional; default is to use "-" |
| 5650 | * (indicating the head/no snapshot). |
| 5651 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5652 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5653 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5654 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5655 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5656 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5657 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5658 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5659 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5660 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5661 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5662 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5663 | *(snap_name + len) = '\0'; |
| 5664 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5665 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5666 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5667 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5668 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5669 | if (!rbd_opts) |
| 5670 | goto out_mem; |
| 5671 | |
| 5672 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5673 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5674 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5675 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5676 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5677 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5678 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5679 | if (IS_ERR(copts)) { |
| 5680 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5681 | goto out_err; |
| 5682 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5683 | kfree(options); |
| 5684 | |
| 5685 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5686 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5687 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5688 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5689 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5690 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5691 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5692 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5693 | kfree(rbd_opts); |
| 5694 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5695 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5696 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5697 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5698 | } |
| 5699 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5700 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5701 | * Return pool id (>= 0) or a negative error code. |
| 5702 | */ |
| 5703 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5704 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5705 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5706 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5707 | int tries = 0; |
| 5708 | int ret; |
| 5709 | |
| 5710 | again: |
| 5711 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5712 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5713 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5714 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5715 | if (ret < 0) |
| 5716 | return ret; |
| 5717 | |
| 5718 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5719 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5720 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5721 | newest_epoch, |
| 5722 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5723 | goto again; |
| 5724 | } else { |
| 5725 | /* the osdmap we have is new enough */ |
| 5726 | return -ENOENT; |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | return ret; |
| 5731 | } |
| 5732 | |
| 5733 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5734 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5735 | * name given to it by the user. Internally, that identifier is |
| 5736 | * what's used to specify the names of objects related to the image. |
| 5737 | * |
| 5738 | * A special "rbd id" object is used to map an rbd image name to its |
| 5739 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5740 | * with the supplied name. |
| 5741 | * |
| 5742 | * This function will record the given rbd_dev's image_id field if |
| 5743 | * it can be determined, and in that case will return 0. If any |
| 5744 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5745 | * image_id field will be unchanged (and should be NULL). |
| 5746 | */ |
| 5747 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5748 | { |
| 5749 | int ret; |
| 5750 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5751 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5752 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5753 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5754 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5755 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5756 | * When probing a parent image, the image id is already |
| 5757 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5758 | * need to fetch the image id again in this case. We |
| 5759 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5760 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5761 | if (rbd_dev->spec->image_id) { |
| 5762 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5763 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5764 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5765 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5766 | |
| 5767 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5768 | * First, see if the format 2 image id file exists, and if |
| 5769 | * so, get the image's persistent id from it. |
| 5770 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5771 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5772 | rbd_dev->spec->image_name); |
| 5773 | if (ret) |
| 5774 | return ret; |
| 5775 | |
| 5776 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5777 | |
| 5778 | /* Response will be an encoded string, which includes a length */ |
| 5779 | |
| 5780 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5781 | response = kzalloc(size, GFP_NOIO); |
| 5782 | if (!response) { |
| 5783 | ret = -ENOMEM; |
| 5784 | goto out; |
| 5785 | } |
| 5786 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5787 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5788 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5789 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5790 | "get_id", NULL, 0, |
| 5791 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5792 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5793 | if (ret == -ENOENT) { |
| 5794 | image_id = kstrdup("", GFP_KERNEL); |
| 5795 | ret = image_id ? 0 : -ENOMEM; |
| 5796 | if (!ret) |
| 5797 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5798 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5799 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5800 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5801 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5802 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5803 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5804 | if (!ret) |
| 5805 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | if (!ret) { |
| 5809 | rbd_dev->spec->image_id = image_id; |
| 5810 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5811 | } |
| 5812 | out: |
| 5813 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5814 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5815 | return ret; |
| 5816 | } |
| 5817 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5818 | /* |
| 5819 | * Undo whatever state changes are made by v1 or v2 header info |
| 5820 | * call. |
| 5821 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5822 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5823 | { |
| 5824 | struct rbd_image_header *header; |
| 5825 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5826 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5827 | |
| 5828 | /* Free dynamic fields from the header, then zero it out */ |
| 5829 | |
| 5830 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5831 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5832 | kfree(header->snap_sizes); |
| 5833 | kfree(header->snap_names); |
| 5834 | kfree(header->object_prefix); |
| 5835 | memset(header, 0, sizeof (*header)); |
| 5836 | } |
| 5837 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5838 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5839 | { |
| 5840 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5841 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5842 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5843 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5844 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5845 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5846 | /* |
| 5847 | * Get the and check features for the image. Currently the |
| 5848 | * features are assumed to never change. |
| 5849 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5850 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5851 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5852 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5853 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5854 | /* If the image supports fancy striping, get its parameters */ |
| 5855 | |
| 5856 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5857 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5858 | if (ret < 0) |
| 5859 | goto out_err; |
| 5860 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5861 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5862 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5863 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5864 | if (ret) |
| 5865 | goto out_err; |
| 5866 | } |
| 5867 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5868 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5869 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5870 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5871 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5872 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5873 | kfree(rbd_dev->header.object_prefix); |
| 5874 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5875 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5876 | } |
| 5877 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5878 | /* |
| 5879 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5880 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5881 | * length of the already discovered part of the parent chain. |
| 5882 | */ |
| 5883 | 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] | 5884 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5885 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5886 | int ret; |
| 5887 | |
| 5888 | if (!rbd_dev->parent_spec) |
| 5889 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5890 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5891 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5892 | pr_info("parent chain is too long (%d)\n", depth); |
| 5893 | ret = -EINVAL; |
| 5894 | goto out_err; |
| 5895 | } |
| 5896 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5897 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5898 | if (!parent) { |
| 5899 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5900 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | /* |
| 5904 | * Images related by parent/child relationships always share |
| 5905 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5906 | */ |
| 5907 | __rbd_get_client(rbd_dev->rbd_client); |
| 5908 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5909 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5910 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5911 | if (ret < 0) |
| 5912 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5913 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5914 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5915 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5916 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5917 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5918 | out_err: |
| 5919 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5920 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5921 | return ret; |
| 5922 | } |
| 5923 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5924 | /* |
| 5925 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5926 | * upon return. |
| 5927 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5928 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5929 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5930 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5931 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5932 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5933 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5934 | if (!single_major) { |
| 5935 | ret = register_blkdev(0, rbd_dev->name); |
| 5936 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5937 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5938 | |
| 5939 | rbd_dev->major = ret; |
| 5940 | rbd_dev->minor = 0; |
| 5941 | } else { |
| 5942 | rbd_dev->major = rbd_major; |
| 5943 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5944 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5945 | |
| 5946 | /* Set up the blkdev mapping. */ |
| 5947 | |
| 5948 | ret = rbd_init_disk(rbd_dev); |
| 5949 | if (ret) |
| 5950 | goto err_out_blkdev; |
| 5951 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5952 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5953 | if (ret) |
| 5954 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5955 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5956 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 5957 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5958 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5959 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
| 5960 | ret = device_add(&rbd_dev->dev); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5961 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5962 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5963 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5964 | /* Everything's ready. Announce the disk to the world. */ |
| 5965 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5966 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5967 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5968 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5969 | spin_lock(&rbd_dev_list_lock); |
| 5970 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5971 | spin_unlock(&rbd_dev_list_lock); |
| 5972 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5973 | add_disk(rbd_dev->disk); |
Ilya Dryomov | ca7909e | 2016-08-18 18:38:41 +0200 | [diff] [blame] | 5974 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5975 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5976 | rbd_dev->header.features); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5977 | |
| 5978 | return ret; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5979 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5980 | err_out_mapping: |
| 5981 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5982 | err_out_disk: |
| 5983 | rbd_free_disk(rbd_dev); |
| 5984 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5985 | if (!single_major) |
| 5986 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5987 | err_out_unlock: |
| 5988 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5989 | return ret; |
| 5990 | } |
| 5991 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5992 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5993 | { |
| 5994 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5995 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5996 | |
| 5997 | /* Record the header object name for this rbd image. */ |
| 5998 | |
| 5999 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6000 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6001 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6002 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6003 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6004 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6005 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6006 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6007 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6008 | } |
| 6009 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6010 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6011 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6012 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6013 | rbd_dev->image_format = 0; |
| 6014 | kfree(rbd_dev->spec->image_id); |
| 6015 | rbd_dev->spec->image_id = NULL; |
| 6016 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6017 | rbd_dev_destroy(rbd_dev); |
| 6018 | } |
| 6019 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6020 | /* |
| 6021 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6022 | * device. If this image is the one being mapped (i.e., not a |
| 6023 | * parent), initiate a watch on its header object before using that |
| 6024 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6025 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6026 | 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] | 6027 | { |
| 6028 | int ret; |
| 6029 | |
| 6030 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6031 | * Get the id from the image id object. Unless there's an |
| 6032 | * error, rbd_dev->spec->image_id will be filled in with |
| 6033 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6034 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6035 | */ |
| 6036 | ret = rbd_dev_image_id(rbd_dev); |
| 6037 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6038 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6039 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6040 | ret = rbd_dev_header_name(rbd_dev); |
| 6041 | if (ret) |
| 6042 | goto err_out_format; |
| 6043 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6044 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6045 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6046 | if (ret) { |
| 6047 | if (ret == -ENOENT) |
| 6048 | pr_info("image %s/%s does not exist\n", |
| 6049 | rbd_dev->spec->pool_name, |
| 6050 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6051 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6052 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6053 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6054 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6055 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6056 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6057 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6058 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6059 | /* |
| 6060 | * If this image is the one being mapped, we have pool name and |
| 6061 | * id, image name and id, and snap name - need to fill snap id. |
| 6062 | * Otherwise this is a parent image, identified by pool, image |
| 6063 | * and snap ids - need to fill in names for those ids. |
| 6064 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6065 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6066 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6067 | else |
| 6068 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6069 | if (ret) { |
| 6070 | if (ret == -ENOENT) |
| 6071 | pr_info("snap %s/%s@%s does not exist\n", |
| 6072 | rbd_dev->spec->pool_name, |
| 6073 | rbd_dev->spec->image_name, |
| 6074 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6075 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6076 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6077 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6078 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6079 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6080 | if (ret) |
| 6081 | goto err_out_probe; |
| 6082 | |
| 6083 | /* |
| 6084 | * Need to warn users if this image is the one being |
| 6085 | * mapped and has a parent. |
| 6086 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6087 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6088 | rbd_warn(rbd_dev, |
| 6089 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6090 | } |
| 6091 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6092 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6093 | if (ret) |
| 6094 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6095 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6096 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6097 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6098 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6099 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6100 | err_out_probe: |
| 6101 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6102 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6103 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6104 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6105 | err_out_format: |
| 6106 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6107 | kfree(rbd_dev->spec->image_id); |
| 6108 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6109 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6110 | } |
| 6111 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6112 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6113 | const char *buf, |
| 6114 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6115 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6116 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6117 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6118 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6119 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6120 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6121 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6122 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6123 | |
| 6124 | if (!try_module_get(THIS_MODULE)) |
| 6125 | return -ENODEV; |
| 6126 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6127 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6128 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6129 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6130 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6131 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6132 | rbdc = rbd_get_client(ceph_opts); |
| 6133 | if (IS_ERR(rbdc)) { |
| 6134 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6135 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6136 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6137 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6138 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6139 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6140 | if (rc < 0) { |
| 6141 | if (rc == -ENOENT) |
| 6142 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6143 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6144 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6145 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6146 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6147 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6148 | if (!rbd_dev) { |
| 6149 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6150 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6151 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6152 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6153 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6154 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6155 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6156 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6157 | if (!rbd_dev->config_info) { |
| 6158 | rc = -ENOMEM; |
| 6159 | goto err_out_rbd_dev; |
| 6160 | } |
| 6161 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6162 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6163 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6164 | if (rc < 0) { |
| 6165 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6166 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6167 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6168 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6169 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6170 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6171 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6172 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6173 | read_only = true; |
| 6174 | rbd_dev->mapping.read_only = read_only; |
| 6175 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6176 | rc = rbd_dev_device_setup(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6177 | if (rc) { |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6178 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6179 | * rbd_unregister_watch() can't be moved into |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6180 | * rbd_dev_image_release() without refactoring, see |
| 6181 | * commit 1f3ef78861ac. |
| 6182 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6183 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6184 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6185 | goto out; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6186 | } |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6187 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6188 | rc = count; |
| 6189 | out: |
| 6190 | module_put(THIS_MODULE); |
| 6191 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6192 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6193 | err_out_rbd_dev: |
| 6194 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6195 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6196 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6197 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6198 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6199 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6200 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6201 | } |
| 6202 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6203 | static ssize_t rbd_add(struct bus_type *bus, |
| 6204 | const char *buf, |
| 6205 | size_t count) |
| 6206 | { |
| 6207 | if (single_major) |
| 6208 | return -EINVAL; |
| 6209 | |
| 6210 | return do_rbd_add(bus, buf, count); |
| 6211 | } |
| 6212 | |
| 6213 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6214 | const char *buf, |
| 6215 | size_t count) |
| 6216 | { |
| 6217 | return do_rbd_add(bus, buf, count); |
| 6218 | } |
| 6219 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6220 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6221 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6222 | rbd_free_disk(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6223 | |
| 6224 | spin_lock(&rbd_dev_list_lock); |
| 6225 | list_del_init(&rbd_dev->node); |
| 6226 | spin_unlock(&rbd_dev_list_lock); |
| 6227 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6228 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6229 | device_del(&rbd_dev->dev); |
Alex Elder | 6d80b13 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6230 | rbd_dev_mapping_clear(rbd_dev); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6231 | if (!single_major) |
| 6232 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6233 | } |
| 6234 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6235 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6236 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6237 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6238 | struct rbd_device *first = rbd_dev; |
| 6239 | struct rbd_device *second = first->parent; |
| 6240 | struct rbd_device *third; |
| 6241 | |
| 6242 | /* |
| 6243 | * Follow to the parent with no grandparent and |
| 6244 | * remove it. |
| 6245 | */ |
| 6246 | while (second && (third = second->parent)) { |
| 6247 | first = second; |
| 6248 | second = third; |
| 6249 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6250 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6251 | rbd_dev_image_release(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6252 | first->parent = NULL; |
| 6253 | first->parent_overlap = 0; |
| 6254 | |
| 6255 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6256 | rbd_spec_put(first->parent_spec); |
| 6257 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6258 | } |
| 6259 | } |
| 6260 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6261 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6262 | const char *buf, |
| 6263 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6264 | { |
| 6265 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6266 | struct list_head *tmp; |
| 6267 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6268 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6269 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6270 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6271 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6272 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6273 | dev_id = -1; |
| 6274 | opt_buf[0] = '\0'; |
| 6275 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6276 | if (dev_id < 0) { |
| 6277 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6278 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6279 | } |
| 6280 | if (opt_buf[0] != '\0') { |
| 6281 | if (!strcmp(opt_buf, "force")) { |
| 6282 | force = true; |
| 6283 | } else { |
| 6284 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6285 | return -EINVAL; |
| 6286 | } |
| 6287 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6288 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6289 | ret = -ENOENT; |
| 6290 | spin_lock(&rbd_dev_list_lock); |
| 6291 | list_for_each(tmp, &rbd_dev_list) { |
| 6292 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6293 | if (rbd_dev->dev_id == dev_id) { |
| 6294 | ret = 0; |
| 6295 | break; |
| 6296 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6297 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6298 | if (!ret) { |
| 6299 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6300 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6301 | ret = -EBUSY; |
| 6302 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6303 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6304 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6305 | spin_unlock_irq(&rbd_dev->lock); |
| 6306 | } |
| 6307 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6308 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6309 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6310 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6311 | if (force) { |
| 6312 | /* |
| 6313 | * Prevent new IO from being queued and wait for existing |
| 6314 | * IO to complete/fail. |
| 6315 | */ |
| 6316 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6317 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6318 | } |
| 6319 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6320 | down_write(&rbd_dev->lock_rwsem); |
| 6321 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6322 | rbd_unlock(rbd_dev); |
| 6323 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6324 | rbd_unregister_watch(rbd_dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6325 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 6326 | /* |
| 6327 | * Don't free anything from rbd_dev->disk until after all |
| 6328 | * notifies are completely processed. Otherwise |
| 6329 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting |
| 6330 | * in a potential use after free of rbd_dev->disk or rbd_dev. |
| 6331 | */ |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6332 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6333 | rbd_dev_image_release(rbd_dev); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 6334 | |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6335 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6336 | } |
| 6337 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6338 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6339 | const char *buf, |
| 6340 | size_t count) |
| 6341 | { |
| 6342 | if (single_major) |
| 6343 | return -EINVAL; |
| 6344 | |
| 6345 | return do_rbd_remove(bus, buf, count); |
| 6346 | } |
| 6347 | |
| 6348 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6349 | const char *buf, |
| 6350 | size_t count) |
| 6351 | { |
| 6352 | return do_rbd_remove(bus, buf, count); |
| 6353 | } |
| 6354 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6355 | /* |
| 6356 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6357 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6358 | */ |
| 6359 | static int rbd_sysfs_init(void) |
| 6360 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6361 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6362 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6363 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6364 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6365 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6366 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6367 | ret = bus_register(&rbd_bus_type); |
| 6368 | if (ret < 0) |
| 6369 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6370 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6371 | return ret; |
| 6372 | } |
| 6373 | |
| 6374 | static void rbd_sysfs_cleanup(void) |
| 6375 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6376 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6377 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6378 | } |
| 6379 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6380 | static int rbd_slab_init(void) |
| 6381 | { |
| 6382 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6383 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6384 | if (!rbd_img_request_cache) |
| 6385 | return -ENOMEM; |
| 6386 | |
| 6387 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6388 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6389 | if (!rbd_obj_request_cache) |
| 6390 | goto out_err; |
| 6391 | |
| 6392 | rbd_assert(!rbd_segment_name_cache); |
| 6393 | rbd_segment_name_cache = kmem_cache_create("rbd_segment_name", |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 6394 | CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6395 | if (rbd_segment_name_cache) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6396 | return 0; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6397 | out_err: |
Julia Lawall | 13bf283 | 2015-09-13 14:15:26 +0200 | [diff] [blame] | 6398 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6399 | rbd_obj_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6400 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6401 | kmem_cache_destroy(rbd_img_request_cache); |
| 6402 | rbd_img_request_cache = NULL; |
| 6403 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6404 | return -ENOMEM; |
| 6405 | } |
| 6406 | |
| 6407 | static void rbd_slab_exit(void) |
| 6408 | { |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6409 | rbd_assert(rbd_segment_name_cache); |
| 6410 | kmem_cache_destroy(rbd_segment_name_cache); |
| 6411 | rbd_segment_name_cache = NULL; |
| 6412 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6413 | rbd_assert(rbd_obj_request_cache); |
| 6414 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6415 | rbd_obj_request_cache = NULL; |
| 6416 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6417 | rbd_assert(rbd_img_request_cache); |
| 6418 | kmem_cache_destroy(rbd_img_request_cache); |
| 6419 | rbd_img_request_cache = NULL; |
| 6420 | } |
| 6421 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6422 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6423 | { |
| 6424 | int rc; |
| 6425 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6426 | if (!libceph_compatible(NULL)) { |
| 6427 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6428 | return -EINVAL; |
| 6429 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6430 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6431 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6432 | if (rc) |
| 6433 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6434 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6435 | /* |
| 6436 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6437 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6438 | */ |
| 6439 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6440 | if (!rbd_wq) { |
| 6441 | rc = -ENOMEM; |
| 6442 | goto err_out_slab; |
| 6443 | } |
| 6444 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6445 | if (single_major) { |
| 6446 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6447 | if (rbd_major < 0) { |
| 6448 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6449 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6450 | } |
| 6451 | } |
| 6452 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6453 | rc = rbd_sysfs_init(); |
| 6454 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6455 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6456 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6457 | if (single_major) |
| 6458 | pr_info("loaded (major %d)\n", rbd_major); |
| 6459 | else |
| 6460 | pr_info("loaded\n"); |
| 6461 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6462 | return 0; |
| 6463 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6464 | err_out_blkdev: |
| 6465 | if (single_major) |
| 6466 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6467 | err_out_wq: |
| 6468 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6469 | err_out_slab: |
| 6470 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6471 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6472 | } |
| 6473 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6474 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6475 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6476 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6477 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6478 | if (single_major) |
| 6479 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6480 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6481 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6482 | } |
| 6483 | |
| 6484 | module_init(rbd_init); |
| 6485 | module_exit(rbd_exit); |
| 6486 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6487 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6488 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6489 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6490 | /* following authorship retained from original osdblk.c */ |
| 6491 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6492 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6493 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6494 | MODULE_LICENSE("GPL"); |