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 { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 234 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 235 | u64 offset; /* object start byte */ |
| 236 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 237 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 238 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 239 | /* |
| 240 | * An object request associated with an image will have its |
| 241 | * img_data flag set; a standalone object request will not. |
| 242 | * |
| 243 | * A standalone object request will have which == BAD_WHICH |
| 244 | * and a null obj_request pointer. |
| 245 | * |
| 246 | * An object request initiated in support of a layered image |
| 247 | * object (to check for its existence before a write) will |
| 248 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 249 | * |
| 250 | * Finally, an object request for rbd image data will have |
| 251 | * which != BAD_WHICH, and will have a non-null img_request |
| 252 | * pointer. The value of which will be in the range |
| 253 | * 0..(img_request->obj_request_count-1). |
| 254 | */ |
| 255 | union { |
| 256 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 257 | struct { |
| 258 | struct rbd_img_request *img_request; |
| 259 | u64 img_offset; |
| 260 | /* links for img_request->obj_requests list */ |
| 261 | struct list_head links; |
| 262 | }; |
| 263 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 264 | u32 which; /* posn image request list */ |
| 265 | |
| 266 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 267 | union { |
| 268 | struct bio *bio_list; |
| 269 | struct { |
| 270 | struct page **pages; |
| 271 | u32 page_count; |
| 272 | }; |
| 273 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 274 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 275 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | |
| 277 | struct ceph_osd_request *osd_req; |
| 278 | |
| 279 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 280 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | |
| 282 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 283 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 284 | |
| 285 | struct kref kref; |
| 286 | }; |
| 287 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 288 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 289 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 290 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 291 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 292 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 293 | }; |
| 294 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 295 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | struct rbd_device *rbd_dev; |
| 297 | u64 offset; /* starting image byte offset */ |
| 298 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 299 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 300 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 301 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 303 | }; |
| 304 | union { |
| 305 | struct request *rq; /* block request */ |
| 306 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 307 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 308 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 309 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 310 | spinlock_t completion_lock;/* protects next_completion */ |
| 311 | u32 next_completion; |
| 312 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 313 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 314 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 315 | |
| 316 | u32 obj_request_count; |
| 317 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 318 | |
| 319 | struct kref kref; |
| 320 | }; |
| 321 | |
| 322 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 323 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 324 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 325 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 326 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 327 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 328 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 329 | enum rbd_watch_state { |
| 330 | RBD_WATCH_STATE_UNREGISTERED, |
| 331 | RBD_WATCH_STATE_REGISTERED, |
| 332 | RBD_WATCH_STATE_ERROR, |
| 333 | }; |
| 334 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 335 | enum rbd_lock_state { |
| 336 | RBD_LOCK_STATE_UNLOCKED, |
| 337 | RBD_LOCK_STATE_LOCKED, |
| 338 | RBD_LOCK_STATE_RELEASING, |
| 339 | }; |
| 340 | |
| 341 | /* WatchNotify::ClientId */ |
| 342 | struct rbd_client_id { |
| 343 | u64 gid; |
| 344 | u64 handle; |
| 345 | }; |
| 346 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 347 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 348 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 349 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 350 | bool read_only; |
| 351 | }; |
| 352 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 353 | /* |
| 354 | * a single device |
| 355 | */ |
| 356 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 357 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 358 | |
| 359 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 360 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 362 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 363 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 364 | struct rbd_client *rbd_client; |
| 365 | |
| 366 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 367 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 368 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 369 | |
| 370 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 371 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 372 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 373 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 374 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 375 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 376 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 377 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 378 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 379 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 380 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 381 | struct mutex watch_mutex; |
| 382 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 383 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 384 | u64 watch_cookie; |
| 385 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 386 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 387 | struct rw_semaphore lock_rwsem; |
| 388 | enum rbd_lock_state lock_state; |
| 389 | struct rbd_client_id owner_cid; |
| 390 | struct work_struct acquired_lock_work; |
| 391 | struct work_struct released_lock_work; |
| 392 | struct delayed_work lock_dwork; |
| 393 | struct work_struct unlock_work; |
| 394 | wait_queue_head_t lock_waitq; |
| 395 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 396 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 397 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 398 | struct rbd_spec *parent_spec; |
| 399 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 400 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 401 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 402 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 403 | /* Block layer tags. */ |
| 404 | struct blk_mq_tag_set tag_set; |
| 405 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 406 | /* protects updating the header */ |
| 407 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 408 | |
| 409 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 410 | |
| 411 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 412 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 413 | /* sysfs related */ |
| 414 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 415 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 416 | }; |
| 417 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 418 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 419 | * Flag bits for rbd_dev->flags: |
| 420 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 421 | * by rbd_dev->lock |
| 422 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 423 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 424 | enum rbd_dev_flags { |
| 425 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 426 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 427 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 428 | }; |
| 429 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 430 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 431 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 432 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 433 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 434 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 435 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 436 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 437 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 438 | /* Slab caches for frequently-allocated structures */ |
| 439 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 440 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 441 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 442 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 443 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 444 | static DEFINE_IDA(rbd_dev_id_ida); |
| 445 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 446 | static struct workqueue_struct *rbd_wq; |
| 447 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 448 | /* |
| 449 | * Default to false for now, as single-major requires >= 0.75 version of |
| 450 | * userspace rbd utility. |
| 451 | */ |
| 452 | static bool single_major = false; |
| 453 | module_param(single_major, bool, S_IRUGO); |
| 454 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 455 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 456 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 457 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 458 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 459 | size_t count); |
| 460 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 461 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 462 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 463 | size_t count); |
| 464 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 465 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 466 | 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] | 467 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 468 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 469 | static int rbd_dev_id_to_minor(int dev_id) |
| 470 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 471 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static int minor_to_rbd_dev_id(int minor) |
| 475 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 476 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 479 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
| 480 | { |
| 481 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 482 | rbd_dev->spec->snap_id == CEPH_NOSNAP && |
| 483 | !rbd_dev->mapping.read_only; |
| 484 | } |
| 485 | |
| 486 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 487 | { |
| 488 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 489 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 490 | } |
| 491 | |
| 492 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 493 | { |
| 494 | bool is_lock_owner; |
| 495 | |
| 496 | down_read(&rbd_dev->lock_rwsem); |
| 497 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 498 | up_read(&rbd_dev->lock_rwsem); |
| 499 | return is_lock_owner; |
| 500 | } |
| 501 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 502 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 503 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 504 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 505 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 506 | |
| 507 | static struct attribute *rbd_bus_attrs[] = { |
| 508 | &bus_attr_add.attr, |
| 509 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 510 | &bus_attr_add_single_major.attr, |
| 511 | &bus_attr_remove_single_major.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 512 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 513 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 514 | |
| 515 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 516 | struct attribute *attr, int index) |
| 517 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 518 | if (!single_major && |
| 519 | (attr == &bus_attr_add_single_major.attr || |
| 520 | attr == &bus_attr_remove_single_major.attr)) |
| 521 | return 0; |
| 522 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 523 | return attr->mode; |
| 524 | } |
| 525 | |
| 526 | static const struct attribute_group rbd_bus_group = { |
| 527 | .attrs = rbd_bus_attrs, |
| 528 | .is_visible = rbd_bus_is_visible, |
| 529 | }; |
| 530 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 531 | |
| 532 | static struct bus_type rbd_bus_type = { |
| 533 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 534 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 535 | }; |
| 536 | |
| 537 | static void rbd_root_dev_release(struct device *dev) |
| 538 | { |
| 539 | } |
| 540 | |
| 541 | static struct device rbd_root_dev = { |
| 542 | .init_name = "rbd", |
| 543 | .release = rbd_root_dev_release, |
| 544 | }; |
| 545 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 546 | static __printf(2, 3) |
| 547 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 548 | { |
| 549 | struct va_format vaf; |
| 550 | va_list args; |
| 551 | |
| 552 | va_start(args, fmt); |
| 553 | vaf.fmt = fmt; |
| 554 | vaf.va = &args; |
| 555 | |
| 556 | if (!rbd_dev) |
| 557 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 558 | else if (rbd_dev->disk) |
| 559 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 560 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 561 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 562 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 563 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 564 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 565 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 566 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 567 | else /* punt */ |
| 568 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 569 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 570 | va_end(args); |
| 571 | } |
| 572 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 573 | #ifdef RBD_DEBUG |
| 574 | #define rbd_assert(expr) \ |
| 575 | if (unlikely(!(expr))) { \ |
| 576 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 577 | "at line %d:\n\n" \ |
| 578 | "\trbd_assert(%s);\n\n", \ |
| 579 | __func__, __LINE__, #expr); \ |
| 580 | BUG(); \ |
| 581 | } |
| 582 | #else /* !RBD_DEBUG */ |
| 583 | # define rbd_assert(expr) ((void) 0) |
| 584 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 585 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 586 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 587 | 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] | 588 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 589 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 590 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 591 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 592 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 593 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 594 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 595 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 596 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 597 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 598 | u8 *order, u64 *snap_size); |
| 599 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 600 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 601 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 602 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 603 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 604 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 605 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 606 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 607 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 608 | return -EROFS; |
| 609 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 610 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 611 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 612 | removing = true; |
| 613 | else |
| 614 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 615 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 616 | if (removing) |
| 617 | return -ENOENT; |
| 618 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 619 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 620 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 624 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 625 | { |
| 626 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 627 | unsigned long open_count_before; |
| 628 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 629 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 630 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 633 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 634 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 637 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 638 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 639 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 640 | int val; |
| 641 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 642 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 643 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 644 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | if (get_user(val, (int __user *)(arg))) |
| 646 | return -EFAULT; |
| 647 | |
| 648 | ro = val ? true : false; |
| 649 | /* Snapshot doesn't allow to write*/ |
| 650 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 651 | return -EROFS; |
| 652 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 653 | spin_lock_irq(&rbd_dev->lock); |
| 654 | /* prevent others open this device */ |
| 655 | if (rbd_dev->open_count > 1) { |
| 656 | ret = -EBUSY; |
| 657 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 658 | } |
| 659 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 660 | if (rbd_dev->mapping.read_only != ro) { |
| 661 | rbd_dev->mapping.read_only = ro; |
| 662 | ro_changed = true; |
| 663 | } |
| 664 | |
| 665 | out: |
| 666 | spin_unlock_irq(&rbd_dev->lock); |
| 667 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 668 | if (ret == 0 && ro_changed) |
| 669 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 670 | |
| 671 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 675 | unsigned int cmd, unsigned long arg) |
| 676 | { |
| 677 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 678 | int ret = 0; |
| 679 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 680 | switch (cmd) { |
| 681 | case BLKROSET: |
| 682 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 683 | break; |
| 684 | default: |
| 685 | ret = -ENOTTY; |
| 686 | } |
| 687 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | #ifdef CONFIG_COMPAT |
| 692 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 693 | unsigned int cmd, unsigned long arg) |
| 694 | { |
| 695 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 696 | } |
| 697 | #endif /* CONFIG_COMPAT */ |
| 698 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 699 | static const struct block_device_operations rbd_bd_ops = { |
| 700 | .owner = THIS_MODULE, |
| 701 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 702 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 703 | .ioctl = rbd_ioctl, |
| 704 | #ifdef CONFIG_COMPAT |
| 705 | .compat_ioctl = rbd_compat_ioctl, |
| 706 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 707 | }; |
| 708 | |
| 709 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 710 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 711 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 712 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 713 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | { |
| 715 | struct rbd_client *rbdc; |
| 716 | int ret = -ENOMEM; |
| 717 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 718 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 720 | if (!rbdc) |
| 721 | goto out_opt; |
| 722 | |
| 723 | kref_init(&rbdc->kref); |
| 724 | INIT_LIST_HEAD(&rbdc->node); |
| 725 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 726 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 727 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 728 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 729 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 730 | |
| 731 | ret = ceph_open_session(rbdc->client); |
| 732 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 733 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 734 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 735 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 736 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 737 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 738 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 739 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 740 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 741 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 742 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 743 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 744 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 745 | kfree(rbdc); |
| 746 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 747 | if (ceph_opts) |
| 748 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 749 | dout("%s: error %d\n", __func__, ret); |
| 750 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 751 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 754 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 755 | { |
| 756 | kref_get(&rbdc->kref); |
| 757 | |
| 758 | return rbdc; |
| 759 | } |
| 760 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 761 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 762 | * Find a ceph client with specific addr and configuration. If |
| 763 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 764 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 765 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 766 | { |
| 767 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 768 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 769 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 770 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 771 | return NULL; |
| 772 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 773 | spin_lock(&rbd_client_list_lock); |
| 774 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 775 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 776 | __rbd_get_client(client_node); |
| 777 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 778 | found = true; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | spin_unlock(&rbd_client_list_lock); |
| 783 | |
| 784 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 788 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 789 | */ |
| 790 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 791 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 792 | Opt_last_int, |
| 793 | /* int args above */ |
| 794 | Opt_last_string, |
| 795 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 796 | Opt_read_only, |
| 797 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 798 | Opt_lock_on_read, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 799 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 802 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 803 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 804 | /* int args above */ |
| 805 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 806 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 807 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 808 | {Opt_read_write, "read_write"}, |
| 809 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 810 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 811 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 812 | }; |
| 813 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 814 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 815 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 816 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 817 | bool lock_on_read; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 818 | }; |
| 819 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 820 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 821 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 822 | #define RBD_LOCK_ON_READ_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 823 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 824 | static int parse_rbd_opts_token(char *c, void *private) |
| 825 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 826 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 827 | substring_t argstr[MAX_OPT_ARGS]; |
| 828 | int token, intval, ret; |
| 829 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 830 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 831 | if (token < Opt_last_int) { |
| 832 | ret = match_int(&argstr[0], &intval); |
| 833 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 834 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 835 | return ret; |
| 836 | } |
| 837 | dout("got int token %d val %d\n", token, intval); |
| 838 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 839 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 840 | } else { |
| 841 | dout("got token %d\n", token); |
| 842 | } |
| 843 | |
| 844 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 845 | case Opt_queue_depth: |
| 846 | if (intval < 1) { |
| 847 | pr_err("queue_depth out of range\n"); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | rbd_opts->queue_depth = intval; |
| 851 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 852 | case Opt_read_only: |
| 853 | rbd_opts->read_only = true; |
| 854 | break; |
| 855 | case Opt_read_write: |
| 856 | rbd_opts->read_only = false; |
| 857 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 858 | case Opt_lock_on_read: |
| 859 | rbd_opts->lock_on_read = true; |
| 860 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 861 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 862 | /* libceph prints "bad option" msg */ |
| 863 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 864 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 865 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 866 | return 0; |
| 867 | } |
| 868 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 869 | static char* obj_op_name(enum obj_operation_type op_type) |
| 870 | { |
| 871 | switch (op_type) { |
| 872 | case OBJ_OP_READ: |
| 873 | return "read"; |
| 874 | case OBJ_OP_WRITE: |
| 875 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 876 | case OBJ_OP_DISCARD: |
| 877 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 878 | default: |
| 879 | return "???"; |
| 880 | } |
| 881 | } |
| 882 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 883 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 884 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 885 | * not exist create it. Either way, ceph_opts is consumed by this |
| 886 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 887 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 888 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 890 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 891 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 892 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 893 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 894 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 895 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 896 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 897 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 898 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 899 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 900 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 905 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 906 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 907 | */ |
| 908 | static void rbd_client_release(struct kref *kref) |
| 909 | { |
| 910 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 911 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 912 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 913 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 914 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 915 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | |
| 917 | ceph_destroy_client(rbdc->client); |
| 918 | kfree(rbdc); |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 923 | * it. |
| 924 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 925 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 926 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 927 | if (rbdc) |
| 928 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 931 | static bool rbd_image_format_valid(u32 image_format) |
| 932 | { |
| 933 | return image_format == 1 || image_format == 2; |
| 934 | } |
| 935 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 936 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 937 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 938 | size_t size; |
| 939 | u32 snap_count; |
| 940 | |
| 941 | /* The header has to start with the magic rbd header text */ |
| 942 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 943 | return false; |
| 944 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 945 | /* The bio layer requires at least sector-sized I/O */ |
| 946 | |
| 947 | if (ondisk->options.order < SECTOR_SHIFT) |
| 948 | return false; |
| 949 | |
| 950 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 951 | |
| 952 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 953 | return false; |
| 954 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 955 | /* |
| 956 | * The size of a snapshot header has to fit in a size_t, and |
| 957 | * that limits the number of snapshots. |
| 958 | */ |
| 959 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 960 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 961 | if (snap_count > size / sizeof (__le64)) |
| 962 | return false; |
| 963 | |
| 964 | /* |
| 965 | * Not only that, but the size of the entire the snapshot |
| 966 | * header must also be representable in a size_t. |
| 967 | */ |
| 968 | size -= snap_count * sizeof (__le64); |
| 969 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 970 | return false; |
| 971 | |
| 972 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 973 | } |
| 974 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 975 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 976 | * returns the size of an object in the image |
| 977 | */ |
| 978 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 979 | { |
| 980 | return 1U << header->obj_order; |
| 981 | } |
| 982 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 983 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 984 | { |
| 985 | if (rbd_dev->header.stripe_unit == 0 || |
| 986 | rbd_dev->header.stripe_count == 0) { |
| 987 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 988 | rbd_dev->header.stripe_count = 1; |
| 989 | } |
| 990 | |
| 991 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 992 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 993 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 994 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 995 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 996 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 997 | } |
| 998 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 999 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1000 | * Fill an rbd image header with information from the given format 1 |
| 1001 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1002 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1003 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1004 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1005 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1006 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1007 | bool first_time = header->object_prefix == NULL; |
| 1008 | struct ceph_snap_context *snapc; |
| 1009 | char *object_prefix = NULL; |
| 1010 | char *snap_names = NULL; |
| 1011 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1012 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1013 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1014 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1015 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1016 | /* Allocate this now to avoid having to handle failure below */ |
| 1017 | |
| 1018 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1019 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1020 | sizeof(ondisk->object_prefix), |
| 1021 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1022 | if (!object_prefix) |
| 1023 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1027 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1028 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1030 | if (!snapc) |
| 1031 | goto out_err; |
| 1032 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1033 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1034 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1035 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1036 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1038 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1039 | if (snap_names_len > (u64)SIZE_MAX) |
| 1040 | goto out_2big; |
| 1041 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1042 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1043 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1044 | |
| 1045 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1046 | snap_sizes = kmalloc_array(snap_count, |
| 1047 | sizeof(*header->snap_sizes), |
| 1048 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1049 | if (!snap_sizes) |
| 1050 | goto out_err; |
| 1051 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1052 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1053 | * Copy the names, and fill in each snapshot's id |
| 1054 | * and size. |
| 1055 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1056 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1057 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1058 | * snap_names_len bytes beyond the end of the |
| 1059 | * snapshot id array, this memcpy() is safe. |
| 1060 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1061 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1062 | snaps = ondisk->snaps; |
| 1063 | for (i = 0; i < snap_count; i++) { |
| 1064 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1065 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1066 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1067 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1068 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1070 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1071 | if (first_time) { |
| 1072 | header->object_prefix = object_prefix; |
| 1073 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1074 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1075 | } else { |
| 1076 | ceph_put_snap_context(header->snapc); |
| 1077 | kfree(header->snap_names); |
| 1078 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1082 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1083 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1084 | header->snapc = snapc; |
| 1085 | header->snap_names = snap_names; |
| 1086 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1087 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1088 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1089 | out_2big: |
| 1090 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1091 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1092 | kfree(snap_sizes); |
| 1093 | kfree(snap_names); |
| 1094 | ceph_put_snap_context(snapc); |
| 1095 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1096 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1097 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1100 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1101 | { |
| 1102 | const char *snap_name; |
| 1103 | |
| 1104 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1105 | |
| 1106 | /* Skip over names until we find the one we are looking for */ |
| 1107 | |
| 1108 | snap_name = rbd_dev->header.snap_names; |
| 1109 | while (which--) |
| 1110 | snap_name += strlen(snap_name) + 1; |
| 1111 | |
| 1112 | return kstrdup(snap_name, GFP_KERNEL); |
| 1113 | } |
| 1114 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1115 | /* |
| 1116 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1117 | * Note that result is for snapshots in *descending* order. |
| 1118 | */ |
| 1119 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1120 | { |
| 1121 | u64 snap_id1 = *(u64 *)s1; |
| 1122 | u64 snap_id2 = *(u64 *)s2; |
| 1123 | |
| 1124 | if (snap_id1 < snap_id2) |
| 1125 | return 1; |
| 1126 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1127 | } |
| 1128 | |
| 1129 | /* |
| 1130 | * Search a snapshot context to see if the given snapshot id is |
| 1131 | * present. |
| 1132 | * |
| 1133 | * Returns the position of the snapshot id in the array if it's found, |
| 1134 | * or BAD_SNAP_INDEX otherwise. |
| 1135 | * |
| 1136 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1137 | * reverse order, highest snapshot id first. |
| 1138 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1139 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1140 | { |
| 1141 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1142 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1143 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1144 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1145 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1146 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1147 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1148 | } |
| 1149 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1150 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1151 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1152 | { |
| 1153 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1154 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1155 | |
| 1156 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1157 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1158 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1159 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1160 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1161 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1162 | } |
| 1163 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1164 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1165 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1166 | if (snap_id == CEPH_NOSNAP) |
| 1167 | return RBD_SNAP_HEAD_NAME; |
| 1168 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1169 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1170 | if (rbd_dev->image_format == 1) |
| 1171 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1172 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1173 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1174 | } |
| 1175 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1176 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1177 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1178 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1179 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1180 | if (snap_id == CEPH_NOSNAP) { |
| 1181 | *snap_size = rbd_dev->header.image_size; |
| 1182 | } else if (rbd_dev->image_format == 1) { |
| 1183 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1184 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1185 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1186 | if (which == BAD_SNAP_INDEX) |
| 1187 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1188 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1189 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1190 | } else { |
| 1191 | u64 size = 0; |
| 1192 | int ret; |
| 1193 | |
| 1194 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1195 | if (ret) |
| 1196 | return ret; |
| 1197 | |
| 1198 | *snap_size = size; |
| 1199 | } |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
| 1203 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1204 | u64 *snap_features) |
| 1205 | { |
| 1206 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1207 | if (snap_id == CEPH_NOSNAP) { |
| 1208 | *snap_features = rbd_dev->header.features; |
| 1209 | } else if (rbd_dev->image_format == 1) { |
| 1210 | *snap_features = 0; /* No features for format 1 */ |
| 1211 | } else { |
| 1212 | u64 features = 0; |
| 1213 | int ret; |
| 1214 | |
| 1215 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1216 | if (ret) |
| 1217 | return ret; |
| 1218 | |
| 1219 | *snap_features = features; |
| 1220 | } |
| 1221 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1222 | } |
| 1223 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1224 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1225 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1226 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1227 | u64 size = 0; |
| 1228 | u64 features = 0; |
| 1229 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1230 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1231 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1232 | if (ret) |
| 1233 | return ret; |
| 1234 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1235 | if (ret) |
| 1236 | return ret; |
| 1237 | |
| 1238 | rbd_dev->mapping.size = size; |
| 1239 | rbd_dev->mapping.features = features; |
| 1240 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1241 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1244 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1245 | { |
| 1246 | rbd_dev->mapping.size = 0; |
| 1247 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1250 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1251 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1252 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1253 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1254 | return offset & (segment_size - 1); |
| 1255 | } |
| 1256 | |
| 1257 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1258 | u64 offset, u64 length) |
| 1259 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1260 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1261 | |
| 1262 | offset &= segment_size - 1; |
| 1263 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1264 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1265 | if (offset + length > segment_size) |
| 1266 | length = segment_size - offset; |
| 1267 | |
| 1268 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | /* |
| 1272 | * bio helpers |
| 1273 | */ |
| 1274 | |
| 1275 | static void bio_chain_put(struct bio *chain) |
| 1276 | { |
| 1277 | struct bio *tmp; |
| 1278 | |
| 1279 | while (chain) { |
| 1280 | tmp = chain; |
| 1281 | chain = chain->bi_next; |
| 1282 | bio_put(tmp); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * zeros a bio chain, starting at specific offset |
| 1288 | */ |
| 1289 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1290 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1291 | struct bio_vec bv; |
| 1292 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1293 | unsigned long flags; |
| 1294 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1295 | int pos = 0; |
| 1296 | |
| 1297 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1298 | bio_for_each_segment(bv, chain, iter) { |
| 1299 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1300 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1301 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1302 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1303 | bv.bv_len - remainder); |
| 1304 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1305 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1306 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1307 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | chain = chain->bi_next; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1315 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1316 | * starting at the given byte offset from the start of the array and |
| 1317 | * continuing up to the given end offset. The pages array is |
| 1318 | * assumed to be big enough to hold all bytes up to the end. |
| 1319 | */ |
| 1320 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1321 | { |
| 1322 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1323 | |
| 1324 | rbd_assert(end > offset); |
| 1325 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1326 | while (offset < end) { |
| 1327 | size_t page_offset; |
| 1328 | size_t length; |
| 1329 | unsigned long flags; |
| 1330 | void *kaddr; |
| 1331 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1332 | page_offset = offset & ~PAGE_MASK; |
| 1333 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1334 | local_irq_save(flags); |
| 1335 | kaddr = kmap_atomic(*page); |
| 1336 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1337 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1338 | kunmap_atomic(kaddr); |
| 1339 | local_irq_restore(flags); |
| 1340 | |
| 1341 | offset += length; |
| 1342 | page++; |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1347 | * Clone a portion of a bio, starting at the given byte offset |
| 1348 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1349 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1350 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1351 | unsigned int offset, |
| 1352 | unsigned int len, |
| 1353 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1354 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1355 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1356 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1357 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1358 | if (!bio) |
| 1359 | return NULL; /* ENOMEM */ |
| 1360 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1361 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1362 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1363 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1364 | return bio; |
| 1365 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1366 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1367 | /* |
| 1368 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1369 | * into the first bio in the source chain and continuing for the |
| 1370 | * number of bytes indicated. The result is another bio chain of |
| 1371 | * exactly the given length, or a null pointer on error. |
| 1372 | * |
| 1373 | * The bio_src and offset parameters are both in-out. On entry they |
| 1374 | * refer to the first source bio and the offset into that bio where |
| 1375 | * the start of data to be cloned is located. |
| 1376 | * |
| 1377 | * On return, bio_src is updated to refer to the bio in the source |
| 1378 | * chain that contains first un-cloned byte, and *offset will |
| 1379 | * contain the offset of that byte within that bio. |
| 1380 | */ |
| 1381 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1382 | unsigned int *offset, |
| 1383 | unsigned int len, |
| 1384 | gfp_t gfpmask) |
| 1385 | { |
| 1386 | struct bio *bi = *bio_src; |
| 1387 | unsigned int off = *offset; |
| 1388 | struct bio *chain = NULL; |
| 1389 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1390 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1391 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1392 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1393 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1394 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1395 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1396 | end = &chain; |
| 1397 | while (len) { |
| 1398 | unsigned int bi_size; |
| 1399 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1400 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1401 | if (!bi) { |
| 1402 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1403 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1404 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1405 | 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] | 1406 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1407 | if (!bio) |
| 1408 | goto out_err; /* ENOMEM */ |
| 1409 | |
| 1410 | *end = bio; |
| 1411 | end = &bio->bi_next; |
| 1412 | |
| 1413 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1414 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1415 | bi = bi->bi_next; |
| 1416 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1417 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1418 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1419 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1420 | *bio_src = bi; |
| 1421 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1422 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1423 | return chain; |
| 1424 | out_err: |
| 1425 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1426 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1427 | return NULL; |
| 1428 | } |
| 1429 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1430 | /* |
| 1431 | * The default/initial value for all object request flags is 0. For |
| 1432 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1433 | * again. |
| 1434 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1435 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1436 | { |
| 1437 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1438 | struct rbd_device *rbd_dev; |
| 1439 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1440 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1441 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1442 | obj_request); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1447 | { |
| 1448 | smp_mb(); |
| 1449 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1450 | } |
| 1451 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1452 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1453 | { |
| 1454 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1455 | struct rbd_device *rbd_dev = NULL; |
| 1456 | |
| 1457 | if (obj_request_img_data_test(obj_request)) |
| 1458 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1459 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1460 | obj_request); |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1465 | { |
| 1466 | smp_mb(); |
| 1467 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1468 | } |
| 1469 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1470 | /* |
| 1471 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1472 | * flag. The latter is set based on the "exists" value provided. |
| 1473 | * |
| 1474 | * Note that for our purposes once an object exists it never goes |
| 1475 | * away again. It's possible that the response from two existence |
| 1476 | * checks are separated by the creation of the target object, and |
| 1477 | * the first ("doesn't exist") response arrives *after* the second |
| 1478 | * ("does exist"). In that case we ignore the second one. |
| 1479 | */ |
| 1480 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1481 | bool exists) |
| 1482 | { |
| 1483 | if (exists) |
| 1484 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1485 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1486 | smp_mb(); |
| 1487 | } |
| 1488 | |
| 1489 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1490 | { |
| 1491 | smp_mb(); |
| 1492 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1493 | } |
| 1494 | |
| 1495 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1496 | { |
| 1497 | smp_mb(); |
| 1498 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1499 | } |
| 1500 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1501 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1502 | { |
| 1503 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1504 | |
| 1505 | return obj_request->img_offset < |
| 1506 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1507 | } |
| 1508 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1509 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1510 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1511 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1512 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1513 | kref_get(&obj_request->kref); |
| 1514 | } |
| 1515 | |
| 1516 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1517 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1518 | { |
| 1519 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1520 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1521 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1522 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1523 | } |
| 1524 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1525 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1526 | { |
| 1527 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1528 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1529 | kref_get(&img_request->kref); |
| 1530 | } |
| 1531 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1532 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1533 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1534 | static void rbd_img_request_destroy(struct kref *kref); |
| 1535 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1536 | { |
| 1537 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1538 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1539 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1540 | if (img_request_child_test(img_request)) |
| 1541 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1542 | else |
| 1543 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1547 | struct rbd_obj_request *obj_request) |
| 1548 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1549 | rbd_assert(obj_request->img_request == NULL); |
| 1550 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1551 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1552 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1553 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1554 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1555 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1556 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1557 | img_request->obj_request_count++; |
| 1558 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1559 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1560 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1561 | } |
| 1562 | |
| 1563 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1564 | struct rbd_obj_request *obj_request) |
| 1565 | { |
| 1566 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1567 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1568 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1569 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1570 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1571 | rbd_assert(img_request->obj_request_count > 0); |
| 1572 | img_request->obj_request_count--; |
| 1573 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1574 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1575 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1576 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1577 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1578 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1579 | rbd_obj_request_put(obj_request); |
| 1580 | } |
| 1581 | |
| 1582 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1583 | { |
| 1584 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1585 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1586 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1587 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1588 | return true; |
| 1589 | default: |
| 1590 | return false; |
| 1591 | } |
| 1592 | } |
| 1593 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1594 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1595 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1596 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1597 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1598 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1599 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1600 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1601 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1602 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1603 | if (obj_request_img_data_test(obj_request)) { |
| 1604 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1605 | rbd_img_request_get(obj_request->img_request); |
| 1606 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1607 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1611 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1612 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1613 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1614 | |
| 1615 | /* |
| 1616 | * If no error occurred, compute the aggregate transfer |
| 1617 | * count for the image request. We could instead use |
| 1618 | * atomic64_cmpxchg() to update it as each object request |
| 1619 | * completes; not clear which way is better off hand. |
| 1620 | */ |
| 1621 | if (!img_request->result) { |
| 1622 | struct rbd_obj_request *obj_request; |
| 1623 | u64 xferred = 0; |
| 1624 | |
| 1625 | for_each_obj_request(img_request, obj_request) |
| 1626 | xferred += obj_request->xferred; |
| 1627 | img_request->xferred = xferred; |
| 1628 | } |
| 1629 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1630 | if (img_request->callback) |
| 1631 | img_request->callback(img_request); |
| 1632 | else |
| 1633 | rbd_img_request_put(img_request); |
| 1634 | } |
| 1635 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1636 | /* |
| 1637 | * The default/initial value for all image request flags is 0. Each |
| 1638 | * is conditionally set to 1 at image request initialization time |
| 1639 | * and currently never change thereafter. |
| 1640 | */ |
| 1641 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1642 | { |
| 1643 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1644 | smp_mb(); |
| 1645 | } |
| 1646 | |
| 1647 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1648 | { |
| 1649 | smp_mb(); |
| 1650 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1651 | } |
| 1652 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1653 | /* |
| 1654 | * Set the discard flag when the img_request is an discard request |
| 1655 | */ |
| 1656 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1657 | { |
| 1658 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1659 | smp_mb(); |
| 1660 | } |
| 1661 | |
| 1662 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1663 | { |
| 1664 | smp_mb(); |
| 1665 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1666 | } |
| 1667 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1668 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1669 | { |
| 1670 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1671 | smp_mb(); |
| 1672 | } |
| 1673 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1674 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1675 | { |
| 1676 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1677 | smp_mb(); |
| 1678 | } |
| 1679 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1680 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1681 | { |
| 1682 | smp_mb(); |
| 1683 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1684 | } |
| 1685 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1686 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1687 | { |
| 1688 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1689 | smp_mb(); |
| 1690 | } |
| 1691 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1692 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1693 | { |
| 1694 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1695 | smp_mb(); |
| 1696 | } |
| 1697 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1698 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1699 | { |
| 1700 | smp_mb(); |
| 1701 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1702 | } |
| 1703 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1704 | static enum obj_operation_type |
| 1705 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1706 | { |
| 1707 | if (img_request_write_test(img_request)) |
| 1708 | return OBJ_OP_WRITE; |
| 1709 | else if (img_request_discard_test(img_request)) |
| 1710 | return OBJ_OP_DISCARD; |
| 1711 | else |
| 1712 | return OBJ_OP_READ; |
| 1713 | } |
| 1714 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1715 | static void |
| 1716 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1717 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1718 | u64 xferred = obj_request->xferred; |
| 1719 | u64 length = obj_request->length; |
| 1720 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1721 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1722 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1723 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1724 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1725 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1726 | * length of the request. A short read also implies zero-fill |
| 1727 | * to the end of the request. An error requires the whole |
| 1728 | * length of the request to be reported finished with an error |
| 1729 | * to the block layer. In each case we update the xferred |
| 1730 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1731 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1732 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1733 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1734 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1735 | zero_bio_chain(obj_request->bio_list, 0); |
| 1736 | else |
| 1737 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1738 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1739 | } else if (xferred < length && !obj_request->result) { |
| 1740 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1741 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1742 | else |
| 1743 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1744 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1745 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1746 | obj_request_done_set(obj_request); |
| 1747 | } |
| 1748 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1749 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1750 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1751 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1752 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1753 | if (obj_request->callback) |
| 1754 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1755 | else |
| 1756 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1757 | } |
| 1758 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1759 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1760 | { |
| 1761 | obj_request->result = err; |
| 1762 | obj_request->xferred = 0; |
| 1763 | /* |
| 1764 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1765 | * rbd_img_obj_callback() |
| 1766 | */ |
| 1767 | if (obj_request_img_data_test(obj_request)) { |
| 1768 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1769 | rbd_img_request_get(obj_request->img_request); |
| 1770 | } |
| 1771 | obj_request_done_set(obj_request); |
| 1772 | rbd_obj_request_complete(obj_request); |
| 1773 | } |
| 1774 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1775 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1776 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1777 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1778 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1779 | bool layered = false; |
| 1780 | |
| 1781 | if (obj_request_img_data_test(obj_request)) { |
| 1782 | img_request = obj_request->img_request; |
| 1783 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1784 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1785 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1786 | |
| 1787 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1788 | obj_request, img_request, obj_request->result, |
| 1789 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1790 | if (layered && obj_request->result == -ENOENT && |
| 1791 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1792 | rbd_img_parent_read(obj_request); |
| 1793 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1794 | rbd_img_obj_request_read_callback(obj_request); |
| 1795 | else |
| 1796 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1797 | } |
| 1798 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1799 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1800 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1801 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1802 | obj_request->result, obj_request->length); |
| 1803 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1804 | * There is no such thing as a successful short write. Set |
| 1805 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1806 | */ |
| 1807 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1808 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1809 | } |
| 1810 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1811 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1812 | { |
| 1813 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1814 | obj_request->result, obj_request->length); |
| 1815 | /* |
| 1816 | * There is no such thing as a successful short discard. Set |
| 1817 | * it to our originally-requested length. |
| 1818 | */ |
| 1819 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1820 | /* discarding a non-existent object is not a problem */ |
| 1821 | if (obj_request->result == -ENOENT) |
| 1822 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1823 | obj_request_done_set(obj_request); |
| 1824 | } |
| 1825 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1826 | /* |
| 1827 | * For a simple stat call there's nothing to do. We'll do more if |
| 1828 | * this is part of a write sequence for a layered image. |
| 1829 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1830 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1831 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1832 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1833 | obj_request_done_set(obj_request); |
| 1834 | } |
| 1835 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1836 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1837 | { |
| 1838 | dout("%s: obj %p\n", __func__, obj_request); |
| 1839 | |
| 1840 | if (obj_request_img_data_test(obj_request)) |
| 1841 | rbd_osd_copyup_callback(obj_request); |
| 1842 | else |
| 1843 | obj_request_done_set(obj_request); |
| 1844 | } |
| 1845 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1846 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1847 | { |
| 1848 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1849 | u16 opcode; |
| 1850 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1851 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1852 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1853 | if (obj_request_img_data_test(obj_request)) { |
| 1854 | rbd_assert(obj_request->img_request); |
| 1855 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1856 | } else { |
| 1857 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1858 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1859 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1860 | if (osd_req->r_result < 0) |
| 1861 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1862 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1863 | /* |
| 1864 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1865 | * passed to the block layer, which just supports a 32-bit |
| 1866 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1867 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1868 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1869 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1870 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1871 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1872 | switch (opcode) { |
| 1873 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1874 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1875 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1876 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1877 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1878 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1879 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1880 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1881 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1882 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1883 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1884 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1885 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1886 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1887 | case CEPH_OSD_OP_DELETE: |
| 1888 | case CEPH_OSD_OP_TRUNCATE: |
| 1889 | case CEPH_OSD_OP_ZERO: |
| 1890 | rbd_osd_discard_callback(obj_request); |
| 1891 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1892 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1893 | rbd_osd_call_callback(obj_request); |
| 1894 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1895 | default: |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1896 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
| 1897 | obj_request->object_no, opcode); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1898 | break; |
| 1899 | } |
| 1900 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1901 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1902 | rbd_obj_request_complete(obj_request); |
| 1903 | } |
| 1904 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1905 | 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] | 1906 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1907 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1908 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1909 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1910 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1914 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1915 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1916 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1917 | osd_req->r_mtime = CURRENT_TIME; |
| 1918 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1919 | } |
| 1920 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1921 | static struct ceph_osd_request * |
| 1922 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1923 | struct ceph_snap_context *snapc, |
| 1924 | int num_ops, unsigned int flags, |
| 1925 | struct rbd_obj_request *obj_request) |
| 1926 | { |
| 1927 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1928 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1929 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1930 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1931 | |
| 1932 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1933 | if (!req) |
| 1934 | return NULL; |
| 1935 | |
| 1936 | req->r_flags = flags; |
| 1937 | req->r_callback = rbd_osd_req_callback; |
| 1938 | req->r_priv = obj_request; |
| 1939 | |
| 1940 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1941 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1942 | rbd_dev->header.object_prefix, obj_request->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1943 | goto err_req; |
| 1944 | |
| 1945 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1946 | goto err_req; |
| 1947 | |
| 1948 | return req; |
| 1949 | |
| 1950 | err_req: |
| 1951 | ceph_osdc_put_request(req); |
| 1952 | return NULL; |
| 1953 | } |
| 1954 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1955 | /* |
| 1956 | * Create an osd request. A read request has one osd op (read). |
| 1957 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1958 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1959 | * technically osd watch is a write request, hence this distinction.) |
| 1960 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1961 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1962 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1963 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1964 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1965 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1966 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1967 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1968 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1969 | if (obj_request_img_data_test(obj_request) && |
| 1970 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1971 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1972 | if (op_type == OBJ_OP_WRITE) { |
| 1973 | rbd_assert(img_request_write_test(img_request)); |
| 1974 | } else { |
| 1975 | rbd_assert(img_request_discard_test(img_request)); |
| 1976 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1977 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1978 | } |
| 1979 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1980 | 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] | 1981 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1982 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 1983 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 1984 | CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1985 | } |
| 1986 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1987 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1988 | * Create a copyup osd request based on the information in the object |
| 1989 | * request supplied. A copyup request has two or three osd ops, a |
| 1990 | * copyup method call, potentially a hint op, and a write or truncate |
| 1991 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1992 | */ |
| 1993 | static struct ceph_osd_request * |
| 1994 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 1995 | { |
| 1996 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1997 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1998 | |
| 1999 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2000 | img_request = obj_request->img_request; |
| 2001 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2002 | rbd_assert(img_request_write_test(img_request) || |
| 2003 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2004 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2005 | if (img_request_discard_test(img_request)) |
| 2006 | num_osd_ops = 2; |
| 2007 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2008 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 2009 | img_request->snapc, num_osd_ops, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 2010 | CEPH_OSD_FLAG_WRITE, obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2011 | } |
| 2012 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2013 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2014 | { |
| 2015 | ceph_osdc_put_request(osd_req); |
| 2016 | } |
| 2017 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2018 | static struct rbd_obj_request * |
| 2019 | rbd_obj_request_create(enum obj_request_type type) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2020 | { |
| 2021 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2022 | |
| 2023 | rbd_assert(obj_request_type_valid(type)); |
| 2024 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2025 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2026 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2027 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2028 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2029 | obj_request->which = BAD_WHICH; |
| 2030 | obj_request->type = type; |
| 2031 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2032 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2033 | kref_init(&obj_request->kref); |
| 2034 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2035 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2036 | return obj_request; |
| 2037 | } |
| 2038 | |
| 2039 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2040 | { |
| 2041 | struct rbd_obj_request *obj_request; |
| 2042 | |
| 2043 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2044 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2045 | dout("%s: obj %p\n", __func__, obj_request); |
| 2046 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2047 | rbd_assert(obj_request->img_request == NULL); |
| 2048 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2049 | |
| 2050 | if (obj_request->osd_req) |
| 2051 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2052 | |
| 2053 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2054 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2055 | case OBJ_REQUEST_NODATA: |
| 2056 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2057 | case OBJ_REQUEST_BIO: |
| 2058 | if (obj_request->bio_list) |
| 2059 | bio_chain_put(obj_request->bio_list); |
| 2060 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2061 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2062 | /* img_data requests don't own their page array */ |
| 2063 | if (obj_request->pages && |
| 2064 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2065 | ceph_release_page_vector(obj_request->pages, |
| 2066 | obj_request->page_count); |
| 2067 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2068 | } |
| 2069 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2070 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2071 | } |
| 2072 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2073 | /* It's OK to call this for a device with no parent */ |
| 2074 | |
| 2075 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2076 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2077 | { |
| 2078 | rbd_dev_remove_parent(rbd_dev); |
| 2079 | rbd_spec_put(rbd_dev->parent_spec); |
| 2080 | rbd_dev->parent_spec = NULL; |
| 2081 | rbd_dev->parent_overlap = 0; |
| 2082 | } |
| 2083 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2084 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2085 | * Parent image reference counting is used to determine when an |
| 2086 | * image's parent fields can be safely torn down--after there are no |
| 2087 | * more in-flight requests to the parent image. When the last |
| 2088 | * reference is dropped, cleaning them up is safe. |
| 2089 | */ |
| 2090 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2091 | { |
| 2092 | int counter; |
| 2093 | |
| 2094 | if (!rbd_dev->parent_spec) |
| 2095 | return; |
| 2096 | |
| 2097 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2098 | if (counter > 0) |
| 2099 | return; |
| 2100 | |
| 2101 | /* Last reference; clean up parent data structures */ |
| 2102 | |
| 2103 | if (!counter) |
| 2104 | rbd_dev_unparent(rbd_dev); |
| 2105 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2106 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * If an image has a non-zero parent overlap, get a reference to its |
| 2111 | * parent. |
| 2112 | * |
| 2113 | * Returns true if the rbd device has a parent with a non-zero |
| 2114 | * overlap and a reference for it was successfully taken, or |
| 2115 | * false otherwise. |
| 2116 | */ |
| 2117 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2118 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2119 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2120 | |
| 2121 | if (!rbd_dev->parent_spec) |
| 2122 | return false; |
| 2123 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2124 | down_read(&rbd_dev->header_rwsem); |
| 2125 | if (rbd_dev->parent_overlap) |
| 2126 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2127 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2128 | |
| 2129 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2130 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2131 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2132 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2133 | } |
| 2134 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2135 | /* |
| 2136 | * Caller is responsible for filling in the list of object requests |
| 2137 | * that comprises the image request, and the Linux request pointer |
| 2138 | * (if there is one). |
| 2139 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2140 | static struct rbd_img_request *rbd_img_request_create( |
| 2141 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2142 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2143 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2144 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2145 | { |
| 2146 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2147 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2148 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2149 | if (!img_request) |
| 2150 | return NULL; |
| 2151 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2152 | img_request->rq = NULL; |
| 2153 | img_request->rbd_dev = rbd_dev; |
| 2154 | img_request->offset = offset; |
| 2155 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2156 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2157 | if (op_type == OBJ_OP_DISCARD) { |
| 2158 | img_request_discard_set(img_request); |
| 2159 | img_request->snapc = snapc; |
| 2160 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2161 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2162 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2163 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2164 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2165 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2166 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2167 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2168 | spin_lock_init(&img_request->completion_lock); |
| 2169 | img_request->next_completion = 0; |
| 2170 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2171 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2172 | img_request->obj_request_count = 0; |
| 2173 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2174 | kref_init(&img_request->kref); |
| 2175 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2176 | 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] | 2177 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2178 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2179 | return img_request; |
| 2180 | } |
| 2181 | |
| 2182 | static void rbd_img_request_destroy(struct kref *kref) |
| 2183 | { |
| 2184 | struct rbd_img_request *img_request; |
| 2185 | struct rbd_obj_request *obj_request; |
| 2186 | struct rbd_obj_request *next_obj_request; |
| 2187 | |
| 2188 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2189 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2190 | dout("%s: img %p\n", __func__, img_request); |
| 2191 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2192 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2193 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2194 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2195 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2196 | if (img_request_layered_test(img_request)) { |
| 2197 | img_request_layered_clear(img_request); |
| 2198 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2199 | } |
| 2200 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2201 | if (img_request_write_test(img_request) || |
| 2202 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2203 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2204 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2205 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2206 | } |
| 2207 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2208 | static struct rbd_img_request *rbd_parent_request_create( |
| 2209 | struct rbd_obj_request *obj_request, |
| 2210 | u64 img_offset, u64 length) |
| 2211 | { |
| 2212 | struct rbd_img_request *parent_request; |
| 2213 | struct rbd_device *rbd_dev; |
| 2214 | |
| 2215 | rbd_assert(obj_request->img_request); |
| 2216 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2217 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2218 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2219 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2220 | if (!parent_request) |
| 2221 | return NULL; |
| 2222 | |
| 2223 | img_request_child_set(parent_request); |
| 2224 | rbd_obj_request_get(obj_request); |
| 2225 | parent_request->obj_request = obj_request; |
| 2226 | |
| 2227 | return parent_request; |
| 2228 | } |
| 2229 | |
| 2230 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2231 | { |
| 2232 | struct rbd_img_request *parent_request; |
| 2233 | struct rbd_obj_request *orig_request; |
| 2234 | |
| 2235 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2236 | orig_request = parent_request->obj_request; |
| 2237 | |
| 2238 | parent_request->obj_request = NULL; |
| 2239 | rbd_obj_request_put(orig_request); |
| 2240 | img_request_child_clear(parent_request); |
| 2241 | |
| 2242 | rbd_img_request_destroy(kref); |
| 2243 | } |
| 2244 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2245 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2246 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2247 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2248 | unsigned int xferred; |
| 2249 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2250 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2251 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2252 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2253 | img_request = obj_request->img_request; |
| 2254 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2255 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2256 | xferred = (unsigned int)obj_request->xferred; |
| 2257 | result = obj_request->result; |
| 2258 | if (result) { |
| 2259 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2260 | enum obj_operation_type op_type; |
| 2261 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2262 | if (img_request_discard_test(img_request)) |
| 2263 | op_type = OBJ_OP_DISCARD; |
| 2264 | else if (img_request_write_test(img_request)) |
| 2265 | op_type = OBJ_OP_WRITE; |
| 2266 | else |
| 2267 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2268 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2269 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2270 | obj_op_name(op_type), obj_request->length, |
| 2271 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2272 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2273 | result, xferred); |
| 2274 | if (!img_request->result) |
| 2275 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2276 | /* |
| 2277 | * Need to end I/O on the entire obj_request worth of |
| 2278 | * bytes in case of error. |
| 2279 | */ |
| 2280 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2281 | } |
| 2282 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2283 | if (img_request_child_test(img_request)) { |
| 2284 | rbd_assert(img_request->obj_request != NULL); |
| 2285 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2286 | } else { |
| 2287 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2288 | |
| 2289 | more = blk_update_request(img_request->rq, result, xferred); |
| 2290 | if (!more) |
| 2291 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2295 | } |
| 2296 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2297 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2298 | { |
| 2299 | struct rbd_img_request *img_request; |
| 2300 | u32 which = obj_request->which; |
| 2301 | bool more = true; |
| 2302 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2303 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2304 | img_request = obj_request->img_request; |
| 2305 | |
| 2306 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2307 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2308 | rbd_assert(img_request->obj_request_count > 0); |
| 2309 | rbd_assert(which != BAD_WHICH); |
| 2310 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2311 | |
| 2312 | spin_lock_irq(&img_request->completion_lock); |
| 2313 | if (which != img_request->next_completion) |
| 2314 | goto out; |
| 2315 | |
| 2316 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2317 | rbd_assert(more); |
| 2318 | rbd_assert(which < img_request->obj_request_count); |
| 2319 | |
| 2320 | if (!obj_request_done_test(obj_request)) |
| 2321 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2322 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2323 | which++; |
| 2324 | } |
| 2325 | |
| 2326 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2327 | img_request->next_completion = which; |
| 2328 | out: |
| 2329 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2330 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2331 | |
| 2332 | if (!more) |
| 2333 | rbd_img_request_complete(img_request); |
| 2334 | } |
| 2335 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2336 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2337 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2338 | * them for submission. num_ops is the current number of |
| 2339 | * osd operations already to the object request. |
| 2340 | */ |
| 2341 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2342 | struct ceph_osd_request *osd_request, |
| 2343 | enum obj_operation_type op_type, |
| 2344 | unsigned int num_ops) |
| 2345 | { |
| 2346 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2347 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2348 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2349 | u64 offset = obj_request->offset; |
| 2350 | u64 length = obj_request->length; |
| 2351 | u64 img_end; |
| 2352 | u16 opcode; |
| 2353 | |
| 2354 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2355 | if (!offset && length == object_size && |
| 2356 | (!img_request_layered_test(img_request) || |
| 2357 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2358 | opcode = CEPH_OSD_OP_DELETE; |
| 2359 | } else if ((offset + length == object_size)) { |
| 2360 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2361 | } else { |
| 2362 | down_read(&rbd_dev->header_rwsem); |
| 2363 | img_end = rbd_dev->header.image_size; |
| 2364 | up_read(&rbd_dev->header_rwsem); |
| 2365 | |
| 2366 | if (obj_request->img_offset + length == img_end) |
| 2367 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2368 | else |
| 2369 | opcode = CEPH_OSD_OP_ZERO; |
| 2370 | } |
| 2371 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2372 | if (!offset && length == object_size) |
| 2373 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2374 | else |
| 2375 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2376 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2377 | object_size, object_size); |
| 2378 | num_ops++; |
| 2379 | } else { |
| 2380 | opcode = CEPH_OSD_OP_READ; |
| 2381 | } |
| 2382 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2383 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2384 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2385 | else |
| 2386 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2387 | offset, length, 0, 0); |
| 2388 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2389 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2390 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2391 | obj_request->bio_list, length); |
| 2392 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2393 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2394 | obj_request->pages, length, |
| 2395 | offset & ~PAGE_MASK, false, false); |
| 2396 | |
| 2397 | /* Discards are also writes */ |
| 2398 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2399 | rbd_osd_req_format_write(obj_request); |
| 2400 | else |
| 2401 | rbd_osd_req_format_read(obj_request); |
| 2402 | } |
| 2403 | |
| 2404 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2405 | * Split up an image request into one or more object requests, each |
| 2406 | * to a different object. The "type" parameter indicates whether |
| 2407 | * "data_desc" is the pointer to the head of a list of bio |
| 2408 | * structures, or the base of a page array. In either case this |
| 2409 | * function assumes data_desc describes memory sufficient to hold |
| 2410 | * all data described by the image request. |
| 2411 | */ |
| 2412 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2413 | enum obj_request_type type, |
| 2414 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2415 | { |
| 2416 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2417 | struct rbd_obj_request *obj_request = NULL; |
| 2418 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2419 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2420 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2421 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2422 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2423 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2424 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2425 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2426 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2427 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2428 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2429 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2430 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2431 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2432 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2433 | |
| 2434 | if (type == OBJ_REQUEST_BIO) { |
| 2435 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2436 | rbd_assert(img_offset == |
| 2437 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2438 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2439 | pages = data_desc; |
| 2440 | } |
| 2441 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2442 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2443 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2444 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2445 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2446 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2447 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2448 | obj_request = rbd_obj_request_create(type); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2449 | if (!obj_request) |
| 2450 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2451 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2452 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2453 | obj_request->offset = offset; |
| 2454 | obj_request->length = length; |
| 2455 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2456 | /* |
| 2457 | * set obj_request->img_request before creating the |
| 2458 | * osd_request so that it gets the right snapc |
| 2459 | */ |
| 2460 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2461 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2462 | if (type == OBJ_REQUEST_BIO) { |
| 2463 | unsigned int clone_size; |
| 2464 | |
| 2465 | rbd_assert(length <= (u64)UINT_MAX); |
| 2466 | clone_size = (unsigned int)length; |
| 2467 | obj_request->bio_list = |
| 2468 | bio_chain_clone_range(&bio_list, |
| 2469 | &bio_offset, |
| 2470 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2471 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2472 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2473 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2474 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2475 | unsigned int page_count; |
| 2476 | |
| 2477 | obj_request->pages = pages; |
| 2478 | page_count = (u32)calc_pages_for(offset, length); |
| 2479 | obj_request->page_count = page_count; |
| 2480 | if ((offset + length) & ~PAGE_MASK) |
| 2481 | page_count--; /* more on last page */ |
| 2482 | pages += page_count; |
| 2483 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2484 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2485 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2486 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2487 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2488 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2489 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2490 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2491 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2492 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2493 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2494 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2495 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2496 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2497 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2498 | resid -= length; |
| 2499 | } |
| 2500 | |
| 2501 | return 0; |
| 2502 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2503 | out_unwind: |
| 2504 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2505 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2506 | |
| 2507 | return -ENOMEM; |
| 2508 | } |
| 2509 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2510 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2511 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2512 | { |
| 2513 | struct rbd_img_request *img_request; |
| 2514 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2515 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2516 | u32 page_count; |
| 2517 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2518 | dout("%s: obj %p\n", __func__, obj_request); |
| 2519 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2520 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2521 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2522 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2523 | img_request = obj_request->img_request; |
| 2524 | rbd_assert(img_request); |
| 2525 | |
| 2526 | rbd_dev = img_request->rbd_dev; |
| 2527 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2528 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2529 | pages = obj_request->copyup_pages; |
| 2530 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2531 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2532 | page_count = obj_request->copyup_page_count; |
| 2533 | rbd_assert(page_count); |
| 2534 | obj_request->copyup_page_count = 0; |
| 2535 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2536 | |
| 2537 | /* |
| 2538 | * We want the transfer count to reflect the size of the |
| 2539 | * original write request. There is no such thing as a |
| 2540 | * successful short write, so if the request was successful |
| 2541 | * we can just set it to the originally-requested length. |
| 2542 | */ |
| 2543 | if (!obj_request->result) |
| 2544 | obj_request->xferred = obj_request->length; |
| 2545 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2546 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2547 | } |
| 2548 | |
| 2549 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2550 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2551 | { |
| 2552 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2553 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2554 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2555 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2556 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2557 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2558 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2559 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2560 | |
| 2561 | rbd_assert(img_request_child_test(img_request)); |
| 2562 | |
| 2563 | /* First get what we need from the image request */ |
| 2564 | |
| 2565 | pages = img_request->copyup_pages; |
| 2566 | rbd_assert(pages != NULL); |
| 2567 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2568 | page_count = img_request->copyup_page_count; |
| 2569 | rbd_assert(page_count); |
| 2570 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2571 | |
| 2572 | orig_request = img_request->obj_request; |
| 2573 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2574 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2575 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2576 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2577 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2578 | rbd_img_request_put(img_request); |
| 2579 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2580 | rbd_assert(orig_request->img_request); |
| 2581 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2582 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2583 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2584 | /* |
| 2585 | * If the overlap has become 0 (most likely because the |
| 2586 | * image has been flattened) we need to free the pages |
| 2587 | * and re-submit the original write request. |
| 2588 | */ |
| 2589 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2590 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2591 | rbd_obj_request_submit(orig_request); |
| 2592 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2596 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2597 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2598 | /* |
| 2599 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2600 | * 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] | 2601 | * request. Allocate the new copyup osd request for the |
| 2602 | * original request, and release the old one. |
| 2603 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2604 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2605 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2606 | if (!osd_req) |
| 2607 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2608 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2609 | orig_request->osd_req = osd_req; |
| 2610 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2611 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2612 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2613 | /* Initialize the copyup op */ |
| 2614 | |
| 2615 | 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] | 2616 | 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] | 2617 | false, false); |
| 2618 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2619 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2620 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2621 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2622 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2623 | |
| 2624 | /* All set, send it off. */ |
| 2625 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2626 | rbd_obj_request_submit(orig_request); |
| 2627 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2628 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2629 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2630 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2631 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | /* |
| 2635 | * Read from the parent image the range of data that covers the |
| 2636 | * entire target of the given object request. This is used for |
| 2637 | * satisfying a layered image write request when the target of an |
| 2638 | * object request from the image request does not exist. |
| 2639 | * |
| 2640 | * A page array big enough to hold the returned data is allocated |
| 2641 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2642 | * When the read completes, this page array will be transferred to |
| 2643 | * the original object request for the copyup operation. |
| 2644 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2645 | * If an error occurs, it is recorded as the result of the original |
| 2646 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2647 | */ |
| 2648 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2649 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2650 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2651 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2652 | u64 img_offset; |
| 2653 | u64 length; |
| 2654 | struct page **pages = NULL; |
| 2655 | u32 page_count; |
| 2656 | int result; |
| 2657 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2658 | rbd_assert(rbd_dev->parent != NULL); |
| 2659 | |
| 2660 | /* |
| 2661 | * Determine the byte range covered by the object in the |
| 2662 | * child image to which the original request was to be sent. |
| 2663 | */ |
| 2664 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2665 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2666 | |
| 2667 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2668 | * There is no defined parent data beyond the parent |
| 2669 | * overlap, so limit what we read at that boundary if |
| 2670 | * necessary. |
| 2671 | */ |
| 2672 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2673 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2674 | length = rbd_dev->parent_overlap - img_offset; |
| 2675 | } |
| 2676 | |
| 2677 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2678 | * Allocate a page array big enough to receive the data read |
| 2679 | * from the parent. |
| 2680 | */ |
| 2681 | page_count = (u32)calc_pages_for(0, length); |
| 2682 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2683 | if (IS_ERR(pages)) { |
| 2684 | result = PTR_ERR(pages); |
| 2685 | pages = NULL; |
| 2686 | goto out_err; |
| 2687 | } |
| 2688 | |
| 2689 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2690 | parent_request = rbd_parent_request_create(obj_request, |
| 2691 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2692 | if (!parent_request) |
| 2693 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2694 | |
| 2695 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2696 | if (result) |
| 2697 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2698 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2699 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2700 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2701 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2702 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2703 | result = rbd_img_request_submit(parent_request); |
| 2704 | if (!result) |
| 2705 | return 0; |
| 2706 | |
| 2707 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2708 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2709 | parent_request->obj_request = NULL; |
| 2710 | rbd_obj_request_put(obj_request); |
| 2711 | out_err: |
| 2712 | if (pages) |
| 2713 | ceph_release_page_vector(pages, page_count); |
| 2714 | if (parent_request) |
| 2715 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2716 | return result; |
| 2717 | } |
| 2718 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2719 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2720 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2721 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2722 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2723 | int result; |
| 2724 | |
| 2725 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2726 | |
| 2727 | /* |
| 2728 | * All we need from the object request is the original |
| 2729 | * request and the result of the STAT op. Grab those, then |
| 2730 | * we're done with the request. |
| 2731 | */ |
| 2732 | orig_request = obj_request->obj_request; |
| 2733 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2734 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2735 | rbd_assert(orig_request); |
| 2736 | rbd_assert(orig_request->img_request); |
| 2737 | |
| 2738 | result = obj_request->result; |
| 2739 | obj_request->result = 0; |
| 2740 | |
| 2741 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2742 | obj_request, orig_request, result, |
| 2743 | obj_request->xferred, obj_request->length); |
| 2744 | rbd_obj_request_put(obj_request); |
| 2745 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2746 | /* |
| 2747 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2748 | * image has been flattened) we need to re-submit the |
| 2749 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2750 | */ |
| 2751 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2752 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2753 | rbd_obj_request_submit(orig_request); |
| 2754 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2755 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2756 | |
| 2757 | /* |
| 2758 | * Our only purpose here is to determine whether the object |
| 2759 | * exists, and we don't want to treat the non-existence as |
| 2760 | * an error. If something else comes back, transfer the |
| 2761 | * error to the original request and complete it now. |
| 2762 | */ |
| 2763 | if (!result) { |
| 2764 | obj_request_existence_set(orig_request, true); |
| 2765 | } else if (result == -ENOENT) { |
| 2766 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2767 | } else { |
| 2768 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | /* |
| 2772 | * Resubmit the original request now that we have recorded |
| 2773 | * whether the target object exists. |
| 2774 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2775 | result = rbd_img_obj_request_submit(orig_request); |
| 2776 | if (result) |
| 2777 | goto fail_orig_request; |
| 2778 | |
| 2779 | return; |
| 2780 | |
| 2781 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2782 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2783 | } |
| 2784 | |
| 2785 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2786 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2787 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2788 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2789 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2790 | u32 page_count; |
| 2791 | size_t size; |
| 2792 | int ret; |
| 2793 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2794 | stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2795 | if (!stat_request) |
| 2796 | return -ENOMEM; |
| 2797 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2798 | stat_request->object_no = obj_request->object_no; |
| 2799 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2800 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2801 | stat_request); |
| 2802 | if (!stat_request->osd_req) { |
| 2803 | ret = -ENOMEM; |
| 2804 | goto fail_stat_request; |
| 2805 | } |
| 2806 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2807 | /* |
| 2808 | * The response data for a STAT call consists of: |
| 2809 | * le64 length; |
| 2810 | * struct { |
| 2811 | * le32 tv_sec; |
| 2812 | * le32 tv_nsec; |
| 2813 | * } mtime; |
| 2814 | */ |
| 2815 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2816 | page_count = (u32)calc_pages_for(0, size); |
| 2817 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2818 | if (IS_ERR(pages)) { |
| 2819 | ret = PTR_ERR(pages); |
| 2820 | goto fail_stat_request; |
| 2821 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2822 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2823 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2824 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2825 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2826 | |
| 2827 | rbd_obj_request_get(obj_request); |
| 2828 | stat_request->obj_request = obj_request; |
| 2829 | stat_request->pages = pages; |
| 2830 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2831 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2832 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2833 | rbd_obj_request_submit(stat_request); |
| 2834 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2835 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2836 | fail_stat_request: |
| 2837 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2838 | return ret; |
| 2839 | } |
| 2840 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2841 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2842 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2843 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2844 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2845 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2846 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2847 | if (!img_request_write_test(img_request) && |
| 2848 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2849 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2850 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2851 | /* Non-layered writes */ |
| 2852 | if (!img_request_layered_test(img_request)) |
| 2853 | return true; |
| 2854 | |
| 2855 | /* |
| 2856 | * Layered writes outside of the parent overlap range don't |
| 2857 | * share any data with the parent. |
| 2858 | */ |
| 2859 | if (!obj_request_overlaps_parent(obj_request)) |
| 2860 | return true; |
| 2861 | |
| 2862 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2863 | * Entire-object layered writes - we will overwrite whatever |
| 2864 | * parent data there is anyway. |
| 2865 | */ |
| 2866 | if (!obj_request->offset && |
| 2867 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2868 | return true; |
| 2869 | |
| 2870 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2871 | * If the object is known to already exist, its parent data has |
| 2872 | * already been copied. |
| 2873 | */ |
| 2874 | if (obj_request_known_test(obj_request) && |
| 2875 | obj_request_exists_test(obj_request)) |
| 2876 | return true; |
| 2877 | |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
| 2881 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2882 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2883 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2884 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2885 | rbd_assert(obj_request->img_request); |
| 2886 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2887 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2888 | rbd_obj_request_submit(obj_request); |
| 2889 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2893 | * It's a layered write. The target object might exist but |
| 2894 | * we may not know that yet. If we know it doesn't exist, |
| 2895 | * start by reading the data for the full target object from |
| 2896 | * 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] | 2897 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2898 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2899 | return rbd_img_obj_parent_read_full(obj_request); |
| 2900 | |
| 2901 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2902 | |
| 2903 | return rbd_img_obj_exists_submit(obj_request); |
| 2904 | } |
| 2905 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2906 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2907 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2908 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2909 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2910 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2911 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2912 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2913 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2914 | rbd_img_request_get(img_request); |
| 2915 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2916 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2917 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2918 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2919 | } |
| 2920 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2921 | out_put_ireq: |
| 2922 | rbd_img_request_put(img_request); |
| 2923 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2924 | } |
| 2925 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2926 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2927 | { |
| 2928 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2929 | struct rbd_device *rbd_dev; |
| 2930 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2931 | u64 img_xferred; |
| 2932 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2933 | |
| 2934 | rbd_assert(img_request_child_test(img_request)); |
| 2935 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2936 | /* First get what we need from the image request and release it */ |
| 2937 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2938 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2939 | img_xferred = img_request->xferred; |
| 2940 | img_result = img_request->result; |
| 2941 | rbd_img_request_put(img_request); |
| 2942 | |
| 2943 | /* |
| 2944 | * If the overlap has become 0 (most likely because the |
| 2945 | * image has been flattened) we need to re-submit the |
| 2946 | * original request. |
| 2947 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2948 | rbd_assert(obj_request); |
| 2949 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2950 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2951 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2952 | rbd_obj_request_submit(obj_request); |
| 2953 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2957 | if (obj_request->result) |
| 2958 | goto out; |
| 2959 | |
| 2960 | /* |
| 2961 | * We need to zero anything beyond the parent overlap |
| 2962 | * boundary. Since rbd_img_obj_request_read_callback() |
| 2963 | * will zero anything beyond the end of a short read, an |
| 2964 | * easy way to do this is to pretend the data from the |
| 2965 | * parent came up short--ending at the overlap boundary. |
| 2966 | */ |
| 2967 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 2968 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2969 | if (obj_end > rbd_dev->parent_overlap) { |
| 2970 | u64 xferred = 0; |
| 2971 | |
| 2972 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 2973 | xferred = rbd_dev->parent_overlap - |
| 2974 | obj_request->img_offset; |
| 2975 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2976 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2977 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2978 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2979 | } |
| 2980 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2981 | rbd_img_obj_request_read_callback(obj_request); |
| 2982 | rbd_obj_request_complete(obj_request); |
| 2983 | } |
| 2984 | |
| 2985 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 2986 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2987 | struct rbd_img_request *img_request; |
| 2988 | int result; |
| 2989 | |
| 2990 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2991 | rbd_assert(obj_request->img_request != NULL); |
| 2992 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2993 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2994 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2995 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2996 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2997 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2998 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2999 | result = -ENOMEM; |
| 3000 | if (!img_request) |
| 3001 | goto out_err; |
| 3002 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3003 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3004 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3005 | obj_request->bio_list); |
| 3006 | else |
| 3007 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3008 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3009 | if (result) |
| 3010 | goto out_err; |
| 3011 | |
| 3012 | img_request->callback = rbd_img_parent_read_callback; |
| 3013 | result = rbd_img_request_submit(img_request); |
| 3014 | if (result) |
| 3015 | goto out_err; |
| 3016 | |
| 3017 | return; |
| 3018 | out_err: |
| 3019 | if (img_request) |
| 3020 | rbd_img_request_put(img_request); |
| 3021 | obj_request->result = result; |
| 3022 | obj_request->xferred = 0; |
| 3023 | obj_request_done_set(obj_request); |
| 3024 | } |
| 3025 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3026 | static const struct rbd_client_id rbd_empty_cid; |
| 3027 | |
| 3028 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3029 | const struct rbd_client_id *rhs) |
| 3030 | { |
| 3031 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3032 | } |
| 3033 | |
| 3034 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3035 | { |
| 3036 | struct rbd_client_id cid; |
| 3037 | |
| 3038 | mutex_lock(&rbd_dev->watch_mutex); |
| 3039 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3040 | cid.handle = rbd_dev->watch_cookie; |
| 3041 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3042 | return cid; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * lock_rwsem must be held for write |
| 3047 | */ |
| 3048 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3049 | const struct rbd_client_id *cid) |
| 3050 | { |
| 3051 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3052 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3053 | cid->gid, cid->handle); |
| 3054 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3055 | } |
| 3056 | |
| 3057 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3058 | { |
| 3059 | mutex_lock(&rbd_dev->watch_mutex); |
| 3060 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3061 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3062 | } |
| 3063 | |
| 3064 | /* |
| 3065 | * lock_rwsem must be held for write |
| 3066 | */ |
| 3067 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3068 | { |
| 3069 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3070 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3071 | char cookie[32]; |
| 3072 | int ret; |
| 3073 | |
| 3074 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
| 3075 | |
| 3076 | format_lock_cookie(rbd_dev, cookie); |
| 3077 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3078 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3079 | RBD_LOCK_TAG, "", 0); |
| 3080 | if (ret) |
| 3081 | return ret; |
| 3082 | |
| 3083 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
| 3084 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3085 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
| 3089 | /* |
| 3090 | * lock_rwsem must be held for write |
| 3091 | */ |
| 3092 | static int rbd_unlock(struct rbd_device *rbd_dev) |
| 3093 | { |
| 3094 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3095 | char cookie[32]; |
| 3096 | int ret; |
| 3097 | |
| 3098 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
| 3099 | |
| 3100 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 3101 | |
| 3102 | format_lock_cookie(rbd_dev, cookie); |
| 3103 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3104 | RBD_LOCK_NAME, cookie); |
| 3105 | if (ret && ret != -ENOENT) { |
| 3106 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); |
| 3107 | return ret; |
| 3108 | } |
| 3109 | |
| 3110 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3111 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
| 3112 | return 0; |
| 3113 | } |
| 3114 | |
| 3115 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3116 | enum rbd_notify_op notify_op, |
| 3117 | struct page ***preply_pages, |
| 3118 | size_t *preply_len) |
| 3119 | { |
| 3120 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3121 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3122 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3123 | char buf[buf_size]; |
| 3124 | void *p = buf; |
| 3125 | |
| 3126 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3127 | |
| 3128 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3129 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3130 | ceph_encode_32(&p, notify_op); |
| 3131 | ceph_encode_64(&p, cid.gid); |
| 3132 | ceph_encode_64(&p, cid.handle); |
| 3133 | |
| 3134 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3135 | &rbd_dev->header_oloc, buf, buf_size, |
| 3136 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3137 | } |
| 3138 | |
| 3139 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3140 | enum rbd_notify_op notify_op) |
| 3141 | { |
| 3142 | struct page **reply_pages; |
| 3143 | size_t reply_len; |
| 3144 | |
| 3145 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3146 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3147 | } |
| 3148 | |
| 3149 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3150 | { |
| 3151 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3152 | acquired_lock_work); |
| 3153 | |
| 3154 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3155 | } |
| 3156 | |
| 3157 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3158 | { |
| 3159 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3160 | released_lock_work); |
| 3161 | |
| 3162 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3163 | } |
| 3164 | |
| 3165 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3166 | { |
| 3167 | struct page **reply_pages; |
| 3168 | size_t reply_len; |
| 3169 | bool lock_owner_responded = false; |
| 3170 | int ret; |
| 3171 | |
| 3172 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3173 | |
| 3174 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3175 | &reply_pages, &reply_len); |
| 3176 | if (ret && ret != -ETIMEDOUT) { |
| 3177 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3178 | goto out; |
| 3179 | } |
| 3180 | |
| 3181 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3182 | void *p = page_address(reply_pages[0]); |
| 3183 | void *const end = p + reply_len; |
| 3184 | u32 n; |
| 3185 | |
| 3186 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3187 | while (n--) { |
| 3188 | u8 struct_v; |
| 3189 | u32 len; |
| 3190 | |
| 3191 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3192 | p += 8 + 8; /* skip gid and cookie */ |
| 3193 | |
| 3194 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3195 | if (!len) |
| 3196 | continue; |
| 3197 | |
| 3198 | if (lock_owner_responded) { |
| 3199 | rbd_warn(rbd_dev, |
| 3200 | "duplicate lock owners detected"); |
| 3201 | ret = -EIO; |
| 3202 | goto out; |
| 3203 | } |
| 3204 | |
| 3205 | lock_owner_responded = true; |
| 3206 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3207 | &struct_v, &len); |
| 3208 | if (ret) { |
| 3209 | rbd_warn(rbd_dev, |
| 3210 | "failed to decode ResponseMessage: %d", |
| 3211 | ret); |
| 3212 | goto e_inval; |
| 3213 | } |
| 3214 | |
| 3215 | ret = ceph_decode_32(&p); |
| 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | if (!lock_owner_responded) { |
| 3220 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3221 | ret = -ETIMEDOUT; |
| 3222 | } |
| 3223 | |
| 3224 | out: |
| 3225 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3226 | return ret; |
| 3227 | |
| 3228 | e_inval: |
| 3229 | ret = -EINVAL; |
| 3230 | goto out; |
| 3231 | } |
| 3232 | |
| 3233 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3234 | { |
| 3235 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3236 | |
| 3237 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3238 | if (wake_all) |
| 3239 | wake_up_all(&rbd_dev->lock_waitq); |
| 3240 | else |
| 3241 | wake_up(&rbd_dev->lock_waitq); |
| 3242 | } |
| 3243 | |
| 3244 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3245 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3246 | { |
| 3247 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3248 | u8 lock_type; |
| 3249 | char *lock_tag; |
| 3250 | int ret; |
| 3251 | |
| 3252 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3253 | |
| 3254 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3255 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3256 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3257 | if (ret) |
| 3258 | return ret; |
| 3259 | |
| 3260 | if (*num_lockers == 0) { |
| 3261 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3262 | goto out; |
| 3263 | } |
| 3264 | |
| 3265 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3266 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3267 | lock_tag); |
| 3268 | ret = -EBUSY; |
| 3269 | goto out; |
| 3270 | } |
| 3271 | |
| 3272 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3273 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3274 | ret = -EBUSY; |
| 3275 | goto out; |
| 3276 | } |
| 3277 | |
| 3278 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3279 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3280 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3281 | (*lockers)[0].id.cookie); |
| 3282 | ret = -EBUSY; |
| 3283 | goto out; |
| 3284 | } |
| 3285 | |
| 3286 | out: |
| 3287 | kfree(lock_tag); |
| 3288 | return ret; |
| 3289 | } |
| 3290 | |
| 3291 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3292 | const struct ceph_locker *locker) |
| 3293 | { |
| 3294 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3295 | struct ceph_watch_item *watchers; |
| 3296 | u32 num_watchers; |
| 3297 | u64 cookie; |
| 3298 | int i; |
| 3299 | int ret; |
| 3300 | |
| 3301 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3302 | &rbd_dev->header_oloc, &watchers, |
| 3303 | &num_watchers); |
| 3304 | if (ret) |
| 3305 | return ret; |
| 3306 | |
| 3307 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3308 | for (i = 0; i < num_watchers; i++) { |
| 3309 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3310 | sizeof(locker->info.addr)) && |
| 3311 | watchers[i].cookie == cookie) { |
| 3312 | struct rbd_client_id cid = { |
| 3313 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3314 | .handle = cookie, |
| 3315 | }; |
| 3316 | |
| 3317 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3318 | rbd_dev, cid.gid, cid.handle); |
| 3319 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3320 | ret = 1; |
| 3321 | goto out; |
| 3322 | } |
| 3323 | } |
| 3324 | |
| 3325 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3326 | ret = 0; |
| 3327 | out: |
| 3328 | kfree(watchers); |
| 3329 | return ret; |
| 3330 | } |
| 3331 | |
| 3332 | /* |
| 3333 | * lock_rwsem must be held for write |
| 3334 | */ |
| 3335 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3336 | { |
| 3337 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3338 | struct ceph_locker *lockers; |
| 3339 | u32 num_lockers; |
| 3340 | int ret; |
| 3341 | |
| 3342 | for (;;) { |
| 3343 | ret = rbd_lock(rbd_dev); |
| 3344 | if (ret != -EBUSY) |
| 3345 | return ret; |
| 3346 | |
| 3347 | /* determine if the current lock holder is still alive */ |
| 3348 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3349 | if (ret) |
| 3350 | return ret; |
| 3351 | |
| 3352 | if (num_lockers == 0) |
| 3353 | goto again; |
| 3354 | |
| 3355 | ret = find_watcher(rbd_dev, lockers); |
| 3356 | if (ret) { |
| 3357 | if (ret > 0) |
| 3358 | ret = 0; /* have to request lock */ |
| 3359 | goto out; |
| 3360 | } |
| 3361 | |
| 3362 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3363 | ENTITY_NAME(lockers[0].id.name)); |
| 3364 | |
| 3365 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3366 | &lockers[0].info.addr); |
| 3367 | if (ret) { |
| 3368 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3369 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3370 | goto out; |
| 3371 | } |
| 3372 | |
| 3373 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3374 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3375 | lockers[0].id.cookie, |
| 3376 | &lockers[0].id.name); |
| 3377 | if (ret && ret != -ENOENT) |
| 3378 | goto out; |
| 3379 | |
| 3380 | again: |
| 3381 | ceph_free_lockers(lockers, num_lockers); |
| 3382 | } |
| 3383 | |
| 3384 | out: |
| 3385 | ceph_free_lockers(lockers, num_lockers); |
| 3386 | return ret; |
| 3387 | } |
| 3388 | |
| 3389 | /* |
| 3390 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3391 | */ |
| 3392 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3393 | int *pret) |
| 3394 | { |
| 3395 | enum rbd_lock_state lock_state; |
| 3396 | |
| 3397 | down_read(&rbd_dev->lock_rwsem); |
| 3398 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3399 | rbd_dev->lock_state); |
| 3400 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3401 | lock_state = rbd_dev->lock_state; |
| 3402 | up_read(&rbd_dev->lock_rwsem); |
| 3403 | return lock_state; |
| 3404 | } |
| 3405 | |
| 3406 | up_read(&rbd_dev->lock_rwsem); |
| 3407 | down_write(&rbd_dev->lock_rwsem); |
| 3408 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3409 | rbd_dev->lock_state); |
| 3410 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3411 | *pret = rbd_try_lock(rbd_dev); |
| 3412 | if (*pret) |
| 3413 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3414 | } |
| 3415 | |
| 3416 | lock_state = rbd_dev->lock_state; |
| 3417 | up_write(&rbd_dev->lock_rwsem); |
| 3418 | return lock_state; |
| 3419 | } |
| 3420 | |
| 3421 | static void rbd_acquire_lock(struct work_struct *work) |
| 3422 | { |
| 3423 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3424 | struct rbd_device, lock_dwork); |
| 3425 | enum rbd_lock_state lock_state; |
| 3426 | int ret; |
| 3427 | |
| 3428 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3429 | again: |
| 3430 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3431 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3432 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3433 | wake_requests(rbd_dev, true); |
| 3434 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3435 | rbd_dev, lock_state, ret); |
| 3436 | return; |
| 3437 | } |
| 3438 | |
| 3439 | ret = rbd_request_lock(rbd_dev); |
| 3440 | if (ret == -ETIMEDOUT) { |
| 3441 | goto again; /* treat this as a dead client */ |
| 3442 | } else if (ret < 0) { |
| 3443 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3444 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3445 | RBD_RETRY_DELAY); |
| 3446 | } else { |
| 3447 | /* |
| 3448 | * lock owner acked, but resend if we don't see them |
| 3449 | * release the lock |
| 3450 | */ |
| 3451 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3452 | rbd_dev); |
| 3453 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3454 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | /* |
| 3459 | * lock_rwsem must be held for write |
| 3460 | */ |
| 3461 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3462 | { |
| 3463 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3464 | rbd_dev->lock_state); |
| 3465 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3466 | return false; |
| 3467 | |
| 3468 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3469 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3470 | /* |
| 3471 | * Ensure that all in-flight IO is flushed. |
| 3472 | * |
| 3473 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3474 | * may be shared with other devices. |
| 3475 | */ |
| 3476 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3477 | up_read(&rbd_dev->lock_rwsem); |
| 3478 | |
| 3479 | down_write(&rbd_dev->lock_rwsem); |
| 3480 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3481 | rbd_dev->lock_state); |
| 3482 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3483 | return false; |
| 3484 | |
| 3485 | if (!rbd_unlock(rbd_dev)) |
| 3486 | /* |
| 3487 | * Give others a chance to grab the lock - we would re-acquire |
| 3488 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3489 | * otherwise. We need to ack our own notifications, so this |
| 3490 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3491 | * after wake_requests() in rbd_handle_released_lock(). |
| 3492 | */ |
| 3493 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3494 | |
| 3495 | return true; |
| 3496 | } |
| 3497 | |
| 3498 | static void rbd_release_lock_work(struct work_struct *work) |
| 3499 | { |
| 3500 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3501 | unlock_work); |
| 3502 | |
| 3503 | down_write(&rbd_dev->lock_rwsem); |
| 3504 | rbd_release_lock(rbd_dev); |
| 3505 | up_write(&rbd_dev->lock_rwsem); |
| 3506 | } |
| 3507 | |
| 3508 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3509 | void **p) |
| 3510 | { |
| 3511 | struct rbd_client_id cid = { 0 }; |
| 3512 | |
| 3513 | if (struct_v >= 2) { |
| 3514 | cid.gid = ceph_decode_64(p); |
| 3515 | cid.handle = ceph_decode_64(p); |
| 3516 | } |
| 3517 | |
| 3518 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3519 | cid.handle); |
| 3520 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3521 | down_write(&rbd_dev->lock_rwsem); |
| 3522 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3523 | /* |
| 3524 | * we already know that the remote client is |
| 3525 | * the owner |
| 3526 | */ |
| 3527 | up_write(&rbd_dev->lock_rwsem); |
| 3528 | return; |
| 3529 | } |
| 3530 | |
| 3531 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3532 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3533 | } else { |
| 3534 | down_read(&rbd_dev->lock_rwsem); |
| 3535 | } |
| 3536 | |
| 3537 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3538 | wake_requests(rbd_dev, false); |
| 3539 | up_read(&rbd_dev->lock_rwsem); |
| 3540 | } |
| 3541 | |
| 3542 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3543 | void **p) |
| 3544 | { |
| 3545 | struct rbd_client_id cid = { 0 }; |
| 3546 | |
| 3547 | if (struct_v >= 2) { |
| 3548 | cid.gid = ceph_decode_64(p); |
| 3549 | cid.handle = ceph_decode_64(p); |
| 3550 | } |
| 3551 | |
| 3552 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3553 | cid.handle); |
| 3554 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3555 | down_write(&rbd_dev->lock_rwsem); |
| 3556 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3557 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3558 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3559 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3560 | up_write(&rbd_dev->lock_rwsem); |
| 3561 | return; |
| 3562 | } |
| 3563 | |
| 3564 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3565 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3566 | } else { |
| 3567 | down_read(&rbd_dev->lock_rwsem); |
| 3568 | } |
| 3569 | |
| 3570 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3571 | wake_requests(rbd_dev, false); |
| 3572 | up_read(&rbd_dev->lock_rwsem); |
| 3573 | } |
| 3574 | |
| 3575 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3576 | void **p) |
| 3577 | { |
| 3578 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3579 | struct rbd_client_id cid = { 0 }; |
| 3580 | bool need_to_send; |
| 3581 | |
| 3582 | if (struct_v >= 2) { |
| 3583 | cid.gid = ceph_decode_64(p); |
| 3584 | cid.handle = ceph_decode_64(p); |
| 3585 | } |
| 3586 | |
| 3587 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3588 | cid.handle); |
| 3589 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3590 | return false; |
| 3591 | |
| 3592 | down_read(&rbd_dev->lock_rwsem); |
| 3593 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3594 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3595 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3596 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3597 | rbd_dev); |
| 3598 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3599 | } |
| 3600 | } |
| 3601 | up_read(&rbd_dev->lock_rwsem); |
| 3602 | return need_to_send; |
| 3603 | } |
| 3604 | |
| 3605 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3606 | u64 notify_id, u64 cookie, s32 *result) |
| 3607 | { |
| 3608 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3609 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3610 | char buf[buf_size]; |
| 3611 | int ret; |
| 3612 | |
| 3613 | if (result) { |
| 3614 | void *p = buf; |
| 3615 | |
| 3616 | /* encode ResponseMessage */ |
| 3617 | ceph_start_encoding(&p, 1, 1, |
| 3618 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3619 | ceph_encode_32(&p, *result); |
| 3620 | } else { |
| 3621 | buf_size = 0; |
| 3622 | } |
| 3623 | |
| 3624 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3625 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3626 | buf, buf_size); |
| 3627 | if (ret) |
| 3628 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3629 | } |
| 3630 | |
| 3631 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3632 | u64 cookie) |
| 3633 | { |
| 3634 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3635 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3636 | } |
| 3637 | |
| 3638 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3639 | u64 notify_id, u64 cookie, s32 result) |
| 3640 | { |
| 3641 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3642 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3643 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3644 | |
| 3645 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3646 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3647 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3648 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3649 | void *p = data; |
| 3650 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3651 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3652 | u32 len; |
| 3653 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3654 | int ret; |
| 3655 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3656 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3657 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3658 | if (data_len) { |
| 3659 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3660 | &struct_v, &len); |
| 3661 | if (ret) { |
| 3662 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3663 | ret); |
| 3664 | return; |
| 3665 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3666 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3667 | notify_op = ceph_decode_32(&p); |
| 3668 | } else { |
| 3669 | /* legacy notification for header updates */ |
| 3670 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3671 | len = 0; |
| 3672 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3673 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3674 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3675 | switch (notify_op) { |
| 3676 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3677 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3678 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3679 | break; |
| 3680 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3681 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3682 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3683 | break; |
| 3684 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3685 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3686 | /* |
| 3687 | * send ResponseMessage(0) back so the client |
| 3688 | * can detect a missing owner |
| 3689 | */ |
| 3690 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3691 | cookie, 0); |
| 3692 | else |
| 3693 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3694 | break; |
| 3695 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3696 | ret = rbd_dev_refresh(rbd_dev); |
| 3697 | if (ret) |
| 3698 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3699 | |
| 3700 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3701 | break; |
| 3702 | default: |
| 3703 | if (rbd_is_lock_owner(rbd_dev)) |
| 3704 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3705 | cookie, -EOPNOTSUPP); |
| 3706 | else |
| 3707 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3708 | break; |
| 3709 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3710 | } |
| 3711 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3712 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3713 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3714 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3715 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3716 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3717 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3718 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3719 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3720 | down_write(&rbd_dev->lock_rwsem); |
| 3721 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3722 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3723 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3724 | mutex_lock(&rbd_dev->watch_mutex); |
| 3725 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3726 | __rbd_unregister_watch(rbd_dev); |
| 3727 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3728 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3729 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3730 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3731 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3735 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3736 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3737 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3738 | { |
| 3739 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3740 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3741 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3742 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3743 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3744 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3745 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3746 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3747 | rbd_watch_errcb, rbd_dev); |
| 3748 | if (IS_ERR(handle)) |
| 3749 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3750 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3751 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3752 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3753 | } |
| 3754 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3755 | /* |
| 3756 | * watch_mutex must be locked |
| 3757 | */ |
| 3758 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3759 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3760 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3761 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3762 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3763 | rbd_assert(rbd_dev->watch_handle); |
| 3764 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3765 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3766 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3767 | if (ret) |
| 3768 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3769 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3770 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3771 | } |
| 3772 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3773 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3774 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3775 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3776 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3777 | mutex_lock(&rbd_dev->watch_mutex); |
| 3778 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3779 | ret = __rbd_register_watch(rbd_dev); |
| 3780 | if (ret) |
| 3781 | goto out; |
| 3782 | |
| 3783 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3784 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3785 | |
| 3786 | out: |
| 3787 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3788 | return ret; |
| 3789 | } |
| 3790 | |
| 3791 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3792 | { |
| 3793 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3794 | |
| 3795 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3796 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3797 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3798 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3799 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3803 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3804 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3805 | cancel_tasks_sync(rbd_dev); |
| 3806 | |
| 3807 | mutex_lock(&rbd_dev->watch_mutex); |
| 3808 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3809 | __rbd_unregister_watch(rbd_dev); |
| 3810 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3811 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3812 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3813 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3814 | } |
| 3815 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3816 | static void rbd_reregister_watch(struct work_struct *work) |
| 3817 | { |
| 3818 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3819 | struct rbd_device, watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3820 | bool was_lock_owner = false; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3821 | bool need_to_wake = false; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3822 | int ret; |
| 3823 | |
| 3824 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3825 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3826 | down_write(&rbd_dev->lock_rwsem); |
| 3827 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3828 | was_lock_owner = rbd_release_lock(rbd_dev); |
| 3829 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3830 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3831 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3832 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3833 | goto out; |
| 3834 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3835 | |
| 3836 | ret = __rbd_register_watch(rbd_dev); |
| 3837 | if (ret) { |
| 3838 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3839 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3840 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3841 | need_to_wake = true; |
| 3842 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3843 | queue_delayed_work(rbd_dev->task_wq, |
| 3844 | &rbd_dev->watch_dwork, |
| 3845 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3846 | } |
| 3847 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3848 | goto out; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3849 | } |
| 3850 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3851 | need_to_wake = true; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3852 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3853 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3854 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3855 | |
| 3856 | ret = rbd_dev_refresh(rbd_dev); |
| 3857 | if (ret) |
| 3858 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
| 3859 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3860 | if (was_lock_owner) { |
| 3861 | ret = rbd_try_lock(rbd_dev); |
| 3862 | if (ret) |
| 3863 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", |
| 3864 | ret); |
| 3865 | } |
| 3866 | |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3867 | out: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3868 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3869 | if (need_to_wake) |
| 3870 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3871 | } |
| 3872 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3873 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3874 | * Synchronous osd object method call. Returns the number of bytes |
| 3875 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3876 | */ |
| 3877 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3878 | struct ceph_object_id *oid, |
| 3879 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3880 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3881 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3882 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3883 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3884 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3885 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3886 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3887 | struct page *req_page = NULL; |
| 3888 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3889 | int ret; |
| 3890 | |
| 3891 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3892 | * Method calls are ultimately read operations. The result |
| 3893 | * should placed into the inbound buffer provided. They |
| 3894 | * also supply outbound data--parameters for the object |
| 3895 | * method. Currently if this is present it will be a |
| 3896 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3897 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3898 | if (outbound) { |
| 3899 | if (outbound_size > PAGE_SIZE) |
| 3900 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3901 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3902 | req_page = alloc_page(GFP_KERNEL); |
| 3903 | if (!req_page) |
| 3904 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3905 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3906 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3907 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3908 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3909 | reply_page = alloc_page(GFP_KERNEL); |
| 3910 | if (!reply_page) { |
| 3911 | if (req_page) |
| 3912 | __free_page(req_page); |
| 3913 | return -ENOMEM; |
| 3914 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3915 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3916 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3917 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3918 | reply_page, &inbound_size); |
| 3919 | if (!ret) { |
| 3920 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3921 | ret = inbound_size; |
| 3922 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3923 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3924 | if (req_page) |
| 3925 | __free_page(req_page); |
| 3926 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3927 | return ret; |
| 3928 | } |
| 3929 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3930 | /* |
| 3931 | * lock_rwsem must be held for read |
| 3932 | */ |
| 3933 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3934 | { |
| 3935 | DEFINE_WAIT(wait); |
| 3936 | |
| 3937 | do { |
| 3938 | /* |
| 3939 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3940 | * and cancel_delayed_work() in wake_requests(). |
| 3941 | */ |
| 3942 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3943 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3944 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3945 | TASK_UNINTERRUPTIBLE); |
| 3946 | up_read(&rbd_dev->lock_rwsem); |
| 3947 | schedule(); |
| 3948 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3949 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3950 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3951 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3952 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3953 | } |
| 3954 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3955 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3956 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3957 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3958 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3959 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3960 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3961 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3962 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3963 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3964 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3965 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3966 | int result; |
| 3967 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3968 | switch (req_op(rq)) { |
| 3969 | case REQ_OP_DISCARD: |
| 3970 | op_type = OBJ_OP_DISCARD; |
| 3971 | break; |
| 3972 | case REQ_OP_WRITE: |
| 3973 | op_type = OBJ_OP_WRITE; |
| 3974 | break; |
| 3975 | case REQ_OP_READ: |
| 3976 | op_type = OBJ_OP_READ; |
| 3977 | break; |
| 3978 | default: |
| 3979 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3980 | result = -EIO; |
| 3981 | goto err; |
| 3982 | } |
| 3983 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3984 | /* Ignore/skip any zero-length requests */ |
| 3985 | |
| 3986 | if (!length) { |
| 3987 | dout("%s: zero-length request\n", __func__); |
| 3988 | result = 0; |
| 3989 | goto err_rq; |
| 3990 | } |
| 3991 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3992 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3993 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3994 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3995 | if (rbd_dev->mapping.read_only) { |
| 3996 | result = -EROFS; |
| 3997 | goto err_rq; |
| 3998 | } |
| 3999 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4000 | } |
| 4001 | |
| 4002 | /* |
| 4003 | * Quit early if the mapped snapshot no longer exists. It's |
| 4004 | * still possible the snapshot will have disappeared by the |
| 4005 | * time our request arrives at the osd, but there's no sense in |
| 4006 | * sending it if we already know. |
| 4007 | */ |
| 4008 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4009 | dout("request for non-existent snapshot"); |
| 4010 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4011 | result = -ENXIO; |
| 4012 | goto err_rq; |
| 4013 | } |
| 4014 | |
| 4015 | if (offset && length > U64_MAX - offset + 1) { |
| 4016 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4017 | length); |
| 4018 | result = -EINVAL; |
| 4019 | goto err_rq; /* Shouldn't happen */ |
| 4020 | } |
| 4021 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4022 | blk_mq_start_request(rq); |
| 4023 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4024 | down_read(&rbd_dev->header_rwsem); |
| 4025 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4026 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4027 | snapc = rbd_dev->header.snapc; |
| 4028 | ceph_get_snap_context(snapc); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4029 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4030 | } else { |
| 4031 | must_be_locked = rbd_dev->opts->lock_on_read && |
| 4032 | rbd_is_lock_supported(rbd_dev); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4033 | } |
| 4034 | up_read(&rbd_dev->header_rwsem); |
| 4035 | |
| 4036 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4037 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4038 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4039 | result = -EIO; |
| 4040 | goto err_rq; |
| 4041 | } |
| 4042 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4043 | if (must_be_locked) { |
| 4044 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4045 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 4046 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4047 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4048 | |
| 4049 | WARN_ON((rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) ^ |
| 4050 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 4051 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4052 | result = -EBLACKLISTED; |
| 4053 | goto err_unlock; |
| 4054 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4057 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4058 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4059 | if (!img_request) { |
| 4060 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4061 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4062 | } |
| 4063 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4064 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4065 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4066 | if (op_type == OBJ_OP_DISCARD) |
| 4067 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4068 | NULL); |
| 4069 | else |
| 4070 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4071 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4072 | if (result) |
| 4073 | goto err_img_request; |
| 4074 | |
| 4075 | result = rbd_img_request_submit(img_request); |
| 4076 | if (result) |
| 4077 | goto err_img_request; |
| 4078 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4079 | if (must_be_locked) |
| 4080 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4081 | return; |
| 4082 | |
| 4083 | err_img_request: |
| 4084 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4085 | err_unlock: |
| 4086 | if (must_be_locked) |
| 4087 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4088 | err_rq: |
| 4089 | if (result) |
| 4090 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4091 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4092 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4093 | err: |
| 4094 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4095 | } |
| 4096 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4097 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4098 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4099 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4100 | struct request *rq = bd->rq; |
| 4101 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4102 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4103 | queue_work(rbd_wq, work); |
| 4104 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4105 | } |
| 4106 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4107 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4108 | { |
| 4109 | struct gendisk *disk = rbd_dev->disk; |
| 4110 | |
| 4111 | if (!disk) |
| 4112 | return; |
| 4113 | |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4114 | rbd_dev->disk = NULL; |
| 4115 | if (disk->flags & GENHD_FL_UP) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4116 | del_gendisk(disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4117 | if (disk->queue) |
| 4118 | blk_cleanup_queue(disk->queue); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4119 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4120 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4121 | put_disk(disk); |
| 4122 | } |
| 4123 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4124 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4125 | struct ceph_object_id *oid, |
| 4126 | struct ceph_object_locator *oloc, |
| 4127 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4128 | |
| 4129 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4130 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4131 | struct ceph_osd_request *req; |
| 4132 | struct page **pages; |
| 4133 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4134 | int ret; |
| 4135 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4136 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4137 | if (!req) |
| 4138 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4139 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4140 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4141 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4142 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4143 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4144 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4145 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4146 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4147 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4148 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4149 | if (IS_ERR(pages)) { |
| 4150 | ret = PTR_ERR(pages); |
| 4151 | goto out_req; |
| 4152 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4153 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4154 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4155 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4156 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4157 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4158 | ceph_osdc_start_request(osdc, req, false); |
| 4159 | ret = ceph_osdc_wait_request(osdc, req); |
| 4160 | if (ret >= 0) |
| 4161 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4162 | |
| 4163 | out_req: |
| 4164 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4165 | return ret; |
| 4166 | } |
| 4167 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4168 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4169 | * Read the complete header for the given rbd device. On successful |
| 4170 | * return, the rbd_dev->header field will contain up-to-date |
| 4171 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4172 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4173 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4174 | { |
| 4175 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4176 | u32 snap_count = 0; |
| 4177 | u64 names_size = 0; |
| 4178 | u32 want_count; |
| 4179 | int ret; |
| 4180 | |
| 4181 | /* |
| 4182 | * The complete header will include an array of its 64-bit |
| 4183 | * snapshot ids, followed by the names of those snapshots as |
| 4184 | * a contiguous block of NUL-terminated strings. Note that |
| 4185 | * the number of snapshots could change by the time we read |
| 4186 | * it in, in which case we re-read it. |
| 4187 | */ |
| 4188 | do { |
| 4189 | size_t size; |
| 4190 | |
| 4191 | kfree(ondisk); |
| 4192 | |
| 4193 | size = sizeof (*ondisk); |
| 4194 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4195 | size += names_size; |
| 4196 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4197 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4198 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4199 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4200 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4201 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4202 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4203 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4204 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4205 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4206 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4207 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4208 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4209 | } |
| 4210 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4211 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4212 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4213 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4214 | } |
| 4215 | |
| 4216 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4217 | want_count = snap_count; |
| 4218 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4219 | } while (snap_count != want_count); |
| 4220 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4221 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4222 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4223 | kfree(ondisk); |
| 4224 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4225 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4228 | /* |
| 4229 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4230 | * has disappeared from the (just updated) snapshot context. |
| 4231 | */ |
| 4232 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4233 | { |
| 4234 | u64 snap_id; |
| 4235 | |
| 4236 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4237 | return; |
| 4238 | |
| 4239 | snap_id = rbd_dev->spec->snap_id; |
| 4240 | if (snap_id == CEPH_NOSNAP) |
| 4241 | return; |
| 4242 | |
| 4243 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4244 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4245 | } |
| 4246 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4247 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4248 | { |
| 4249 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4250 | |
| 4251 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4252 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4253 | * try to update its size. If REMOVING is set, updating size |
| 4254 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4255 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4256 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4257 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4258 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4259 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4260 | set_capacity(rbd_dev->disk, size); |
| 4261 | revalidate_disk(rbd_dev->disk); |
| 4262 | } |
| 4263 | } |
| 4264 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4265 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4266 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4267 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4268 | int ret; |
| 4269 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4270 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4271 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4272 | |
| 4273 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4274 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4275 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4276 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4277 | /* |
| 4278 | * If there is a parent, see if it has disappeared due to the |
| 4279 | * mapped image getting flattened. |
| 4280 | */ |
| 4281 | if (rbd_dev->parent) { |
| 4282 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4283 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4284 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4285 | } |
| 4286 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4287 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4288 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4289 | } else { |
| 4290 | /* validate mapped snapshot's EXISTS flag */ |
| 4291 | rbd_exists_validate(rbd_dev); |
| 4292 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4293 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4294 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4295 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4296 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4297 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4298 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4299 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4300 | } |
| 4301 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4302 | static int rbd_init_request(void *data, struct request *rq, |
| 4303 | unsigned int hctx_idx, unsigned int request_idx, |
| 4304 | unsigned int numa_node) |
| 4305 | { |
| 4306 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4307 | |
| 4308 | INIT_WORK(work, rbd_queue_workfn); |
| 4309 | return 0; |
| 4310 | } |
| 4311 | |
| 4312 | static struct blk_mq_ops rbd_mq_ops = { |
| 4313 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4314 | .init_request = rbd_init_request, |
| 4315 | }; |
| 4316 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4317 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4318 | { |
| 4319 | struct gendisk *disk; |
| 4320 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4321 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4322 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4323 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4324 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4325 | disk = alloc_disk(single_major ? |
| 4326 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4327 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4328 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4329 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4330 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4331 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4332 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4333 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4334 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4335 | if (single_major) |
| 4336 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4337 | disk->fops = &rbd_bd_ops; |
| 4338 | disk->private_data = rbd_dev; |
| 4339 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4340 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4341 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4342 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4343 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4344 | 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] | 4345 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4346 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4347 | |
| 4348 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4349 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4350 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4351 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4352 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4353 | if (IS_ERR(q)) { |
| 4354 | err = PTR_ERR(q); |
| 4355 | goto out_tag_set; |
| 4356 | } |
| 4357 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4358 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4359 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4360 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4361 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4362 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4363 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4364 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4365 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4366 | blk_queue_max_segment_size(q, segment_size); |
| 4367 | blk_queue_io_min(q, segment_size); |
| 4368 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4369 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4370 | /* enable the discard support */ |
| 4371 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4372 | q->limits.discard_granularity = segment_size; |
| 4373 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4374 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4375 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4376 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4377 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4378 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4379 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4380 | disk->queue = q; |
| 4381 | |
| 4382 | q->queuedata = rbd_dev; |
| 4383 | |
| 4384 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4385 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4386 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4387 | out_tag_set: |
| 4388 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4389 | out_disk: |
| 4390 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4391 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4392 | } |
| 4393 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4394 | /* |
| 4395 | sysfs |
| 4396 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4397 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4398 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4399 | { |
| 4400 | return container_of(dev, struct rbd_device, dev); |
| 4401 | } |
| 4402 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4403 | static ssize_t rbd_size_show(struct device *dev, |
| 4404 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4405 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4406 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4407 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4408 | return sprintf(buf, "%llu\n", |
| 4409 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4410 | } |
| 4411 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4412 | /* |
| 4413 | * Note this shows the features for whatever's mapped, which is not |
| 4414 | * necessarily the base image. |
| 4415 | */ |
| 4416 | static ssize_t rbd_features_show(struct device *dev, |
| 4417 | struct device_attribute *attr, char *buf) |
| 4418 | { |
| 4419 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4420 | |
| 4421 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4422 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4423 | } |
| 4424 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4425 | static ssize_t rbd_major_show(struct device *dev, |
| 4426 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4427 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4428 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4429 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4430 | if (rbd_dev->major) |
| 4431 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4432 | |
| 4433 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4434 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4435 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4436 | static ssize_t rbd_minor_show(struct device *dev, |
| 4437 | struct device_attribute *attr, char *buf) |
| 4438 | { |
| 4439 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4440 | |
| 4441 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4442 | } |
| 4443 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4444 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4445 | struct device_attribute *attr, char *buf) |
| 4446 | { |
| 4447 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4448 | struct ceph_entity_addr *client_addr = |
| 4449 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4450 | |
| 4451 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4452 | le32_to_cpu(client_addr->nonce)); |
| 4453 | } |
| 4454 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4455 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4456 | struct device_attribute *attr, char *buf) |
| 4457 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4458 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4459 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4460 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4461 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4464 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4465 | struct device_attribute *attr, char *buf) |
| 4466 | { |
| 4467 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4468 | |
| 4469 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4470 | } |
| 4471 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4472 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4473 | struct device_attribute *attr, char *buf) |
| 4474 | { |
| 4475 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4476 | |
| 4477 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | static ssize_t rbd_pool_show(struct device *dev, |
| 4481 | struct device_attribute *attr, char *buf) |
| 4482 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4483 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4484 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4485 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4486 | } |
| 4487 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4488 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4489 | struct device_attribute *attr, char *buf) |
| 4490 | { |
| 4491 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4492 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4493 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4494 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4495 | } |
| 4496 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4497 | static ssize_t rbd_name_show(struct device *dev, |
| 4498 | struct device_attribute *attr, char *buf) |
| 4499 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4500 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4501 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4502 | if (rbd_dev->spec->image_name) |
| 4503 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4504 | |
| 4505 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4506 | } |
| 4507 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4508 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4509 | struct device_attribute *attr, char *buf) |
| 4510 | { |
| 4511 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4512 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4513 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4514 | } |
| 4515 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4516 | /* |
| 4517 | * Shows the name of the currently-mapped snapshot (or |
| 4518 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4519 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4520 | static ssize_t rbd_snap_show(struct device *dev, |
| 4521 | struct device_attribute *attr, |
| 4522 | char *buf) |
| 4523 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4524 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4525 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4526 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4527 | } |
| 4528 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4529 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4530 | struct device_attribute *attr, char *buf) |
| 4531 | { |
| 4532 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4533 | |
| 4534 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4535 | } |
| 4536 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4537 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4538 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4539 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4540 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4541 | */ |
| 4542 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4543 | struct device_attribute *attr, |
| 4544 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4545 | { |
| 4546 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4547 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4548 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4549 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4550 | return sprintf(buf, "(no parent image)\n"); |
| 4551 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4552 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4553 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4554 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4555 | count += sprintf(&buf[count], "%s" |
| 4556 | "pool_id %llu\npool_name %s\n" |
| 4557 | "image_id %s\nimage_name %s\n" |
| 4558 | "snap_id %llu\nsnap_name %s\n" |
| 4559 | "overlap %llu\n", |
| 4560 | !count ? "" : "\n", /* first? */ |
| 4561 | spec->pool_id, spec->pool_name, |
| 4562 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4563 | spec->snap_id, spec->snap_name, |
| 4564 | rbd_dev->parent_overlap); |
| 4565 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4566 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4567 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4568 | } |
| 4569 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4570 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4571 | struct device_attribute *attr, |
| 4572 | const char *buf, |
| 4573 | size_t size) |
| 4574 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4575 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4576 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4577 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4578 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4579 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4580 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4581 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4582 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4583 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4584 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4585 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4586 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4587 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4588 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4589 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4590 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4591 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4592 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4593 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4594 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4595 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4596 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4597 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4598 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4599 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4600 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4601 | |
| 4602 | static struct attribute *rbd_attrs[] = { |
| 4603 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4604 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4605 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4606 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4607 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4608 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4609 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4610 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4611 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4612 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4613 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4614 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4615 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4616 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4617 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4618 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4619 | NULL |
| 4620 | }; |
| 4621 | |
| 4622 | static struct attribute_group rbd_attr_group = { |
| 4623 | .attrs = rbd_attrs, |
| 4624 | }; |
| 4625 | |
| 4626 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4627 | &rbd_attr_group, |
| 4628 | NULL |
| 4629 | }; |
| 4630 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4631 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4632 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4633 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4634 | .name = "rbd", |
| 4635 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4636 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4637 | }; |
| 4638 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4639 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4640 | { |
| 4641 | kref_get(&spec->kref); |
| 4642 | |
| 4643 | return spec; |
| 4644 | } |
| 4645 | |
| 4646 | static void rbd_spec_free(struct kref *kref); |
| 4647 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4648 | { |
| 4649 | if (spec) |
| 4650 | kref_put(&spec->kref, rbd_spec_free); |
| 4651 | } |
| 4652 | |
| 4653 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4654 | { |
| 4655 | struct rbd_spec *spec; |
| 4656 | |
| 4657 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4658 | if (!spec) |
| 4659 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4660 | |
| 4661 | spec->pool_id = CEPH_NOPOOL; |
| 4662 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4663 | kref_init(&spec->kref); |
| 4664 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4665 | return spec; |
| 4666 | } |
| 4667 | |
| 4668 | static void rbd_spec_free(struct kref *kref) |
| 4669 | { |
| 4670 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4671 | |
| 4672 | kfree(spec->pool_name); |
| 4673 | kfree(spec->image_id); |
| 4674 | kfree(spec->image_name); |
| 4675 | kfree(spec->snap_name); |
| 4676 | kfree(spec); |
| 4677 | } |
| 4678 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4679 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4680 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4681 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4682 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4683 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4684 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4685 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4686 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4687 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4688 | rbd_put_client(rbd_dev->rbd_client); |
| 4689 | rbd_spec_put(rbd_dev->spec); |
| 4690 | kfree(rbd_dev->opts); |
| 4691 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | static void rbd_dev_release(struct device *dev) |
| 4695 | { |
| 4696 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4697 | bool need_put = !!rbd_dev->opts; |
| 4698 | |
| 4699 | if (need_put) { |
| 4700 | destroy_workqueue(rbd_dev->task_wq); |
| 4701 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4702 | } |
| 4703 | |
| 4704 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4705 | |
| 4706 | /* |
| 4707 | * This is racy, but way better than putting module outside of |
| 4708 | * the release callback. The race window is pretty small, so |
| 4709 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4710 | */ |
| 4711 | if (need_put) |
| 4712 | module_put(THIS_MODULE); |
| 4713 | } |
| 4714 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4715 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4716 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4717 | { |
| 4718 | struct rbd_device *rbd_dev; |
| 4719 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4720 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4721 | if (!rbd_dev) |
| 4722 | return NULL; |
| 4723 | |
| 4724 | spin_lock_init(&rbd_dev->lock); |
| 4725 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4726 | init_rwsem(&rbd_dev->header_rwsem); |
| 4727 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4728 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4729 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4730 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4731 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4732 | mutex_init(&rbd_dev->watch_mutex); |
| 4733 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4734 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4735 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4736 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4737 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4738 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4739 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4740 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4741 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4742 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4743 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4744 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4745 | rbd_dev->dev.type = &rbd_device_type; |
| 4746 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4747 | device_initialize(&rbd_dev->dev); |
| 4748 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4749 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4750 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4751 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4752 | return rbd_dev; |
| 4753 | } |
| 4754 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4755 | /* |
| 4756 | * Create a mapping rbd_dev. |
| 4757 | */ |
| 4758 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4759 | struct rbd_spec *spec, |
| 4760 | struct rbd_options *opts) |
| 4761 | { |
| 4762 | struct rbd_device *rbd_dev; |
| 4763 | |
| 4764 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4765 | if (!rbd_dev) |
| 4766 | return NULL; |
| 4767 | |
| 4768 | rbd_dev->opts = opts; |
| 4769 | |
| 4770 | /* get an id and fill in device name */ |
| 4771 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4772 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4773 | GFP_KERNEL); |
| 4774 | if (rbd_dev->dev_id < 0) |
| 4775 | goto fail_rbd_dev; |
| 4776 | |
| 4777 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4778 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4779 | rbd_dev->name); |
| 4780 | if (!rbd_dev->task_wq) |
| 4781 | goto fail_dev_id; |
| 4782 | |
| 4783 | /* we have a ref from do_rbd_add() */ |
| 4784 | __module_get(THIS_MODULE); |
| 4785 | |
| 4786 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4787 | return rbd_dev; |
| 4788 | |
| 4789 | fail_dev_id: |
| 4790 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4791 | fail_rbd_dev: |
| 4792 | rbd_dev_free(rbd_dev); |
| 4793 | return NULL; |
| 4794 | } |
| 4795 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4796 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4797 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4798 | if (rbd_dev) |
| 4799 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4800 | } |
| 4801 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4802 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4803 | * Get the size and object order for an image snapshot, or if |
| 4804 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4805 | * image. |
| 4806 | */ |
| 4807 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4808 | u8 *order, u64 *snap_size) |
| 4809 | { |
| 4810 | __le64 snapid = cpu_to_le64(snap_id); |
| 4811 | int ret; |
| 4812 | struct { |
| 4813 | u8 order; |
| 4814 | __le64 size; |
| 4815 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4816 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4817 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4818 | &rbd_dev->header_oloc, "get_size", |
| 4819 | &snapid, sizeof(snapid), |
| 4820 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4821 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4822 | if (ret < 0) |
| 4823 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4824 | if (ret < sizeof (size_buf)) |
| 4825 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4826 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4827 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4828 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4829 | dout(" order %u", (unsigned int)*order); |
| 4830 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4831 | *snap_size = le64_to_cpu(size_buf.size); |
| 4832 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4833 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4834 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4835 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4836 | |
| 4837 | return 0; |
| 4838 | } |
| 4839 | |
| 4840 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4841 | { |
| 4842 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4843 | &rbd_dev->header.obj_order, |
| 4844 | &rbd_dev->header.image_size); |
| 4845 | } |
| 4846 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4847 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4848 | { |
| 4849 | void *reply_buf; |
| 4850 | int ret; |
| 4851 | void *p; |
| 4852 | |
| 4853 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4854 | if (!reply_buf) |
| 4855 | return -ENOMEM; |
| 4856 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4857 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4858 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4859 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4860 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4861 | if (ret < 0) |
| 4862 | goto out; |
| 4863 | |
| 4864 | p = reply_buf; |
| 4865 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4866 | p + ret, NULL, GFP_NOIO); |
| 4867 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4868 | |
| 4869 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4870 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4871 | rbd_dev->header.object_prefix = NULL; |
| 4872 | } else { |
| 4873 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4874 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4875 | out: |
| 4876 | kfree(reply_buf); |
| 4877 | |
| 4878 | return ret; |
| 4879 | } |
| 4880 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4881 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4882 | u64 *snap_features) |
| 4883 | { |
| 4884 | __le64 snapid = cpu_to_le64(snap_id); |
| 4885 | struct { |
| 4886 | __le64 features; |
| 4887 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4888 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4889 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4890 | int ret; |
| 4891 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4892 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4893 | &rbd_dev->header_oloc, "get_features", |
| 4894 | &snapid, sizeof(snapid), |
| 4895 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4896 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4897 | if (ret < 0) |
| 4898 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4899 | if (ret < sizeof (features_buf)) |
| 4900 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4901 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4902 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4903 | if (unsup) { |
| 4904 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4905 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4906 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4907 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4908 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4909 | *snap_features = le64_to_cpu(features_buf.features); |
| 4910 | |
| 4911 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4912 | (unsigned long long)snap_id, |
| 4913 | (unsigned long long)*snap_features, |
| 4914 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4915 | |
| 4916 | return 0; |
| 4917 | } |
| 4918 | |
| 4919 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4920 | { |
| 4921 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4922 | &rbd_dev->header.features); |
| 4923 | } |
| 4924 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4925 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4926 | { |
| 4927 | struct rbd_spec *parent_spec; |
| 4928 | size_t size; |
| 4929 | void *reply_buf = NULL; |
| 4930 | __le64 snapid; |
| 4931 | void *p; |
| 4932 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4933 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4934 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4935 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4936 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4937 | int ret; |
| 4938 | |
| 4939 | parent_spec = rbd_spec_alloc(); |
| 4940 | if (!parent_spec) |
| 4941 | return -ENOMEM; |
| 4942 | |
| 4943 | size = sizeof (__le64) + /* pool_id */ |
| 4944 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4945 | sizeof (__le64) + /* snap_id */ |
| 4946 | sizeof (__le64); /* overlap */ |
| 4947 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4948 | if (!reply_buf) { |
| 4949 | ret = -ENOMEM; |
| 4950 | goto out_err; |
| 4951 | } |
| 4952 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4953 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4954 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4955 | &rbd_dev->header_oloc, "get_parent", |
| 4956 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4957 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4958 | if (ret < 0) |
| 4959 | goto out_err; |
| 4960 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4961 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4962 | end = reply_buf + ret; |
| 4963 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4964 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4965 | if (pool_id == CEPH_NOPOOL) { |
| 4966 | /* |
| 4967 | * Either the parent never existed, or we have |
| 4968 | * record of it but the image got flattened so it no |
| 4969 | * longer has a parent. When the parent of a |
| 4970 | * layered image disappears we immediately set the |
| 4971 | * overlap to 0. The effect of this is that all new |
| 4972 | * requests will be treated as if the image had no |
| 4973 | * parent. |
| 4974 | */ |
| 4975 | if (rbd_dev->parent_overlap) { |
| 4976 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4977 | rbd_dev_parent_put(rbd_dev); |
| 4978 | pr_info("%s: clone image has been flattened\n", |
| 4979 | rbd_dev->disk->disk_name); |
| 4980 | } |
| 4981 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4982 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4983 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4984 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4985 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4986 | |
| 4987 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4988 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4989 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4990 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4991 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4992 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4993 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4994 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4995 | if (IS_ERR(image_id)) { |
| 4996 | ret = PTR_ERR(image_id); |
| 4997 | goto out_err; |
| 4998 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4999 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5000 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5001 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5002 | /* |
| 5003 | * The parent won't change (except when the clone is |
| 5004 | * flattened, already handled that). So we only need to |
| 5005 | * record the parent spec we have not already done so. |
| 5006 | */ |
| 5007 | if (!rbd_dev->parent_spec) { |
| 5008 | parent_spec->pool_id = pool_id; |
| 5009 | parent_spec->image_id = image_id; |
| 5010 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5011 | rbd_dev->parent_spec = parent_spec; |
| 5012 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5013 | } else { |
| 5014 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5015 | } |
| 5016 | |
| 5017 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5018 | * We always update the parent overlap. If it's zero we issue |
| 5019 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5020 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5021 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5022 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5023 | /* refresh, careful to warn just once */ |
| 5024 | if (rbd_dev->parent_overlap) |
| 5025 | rbd_warn(rbd_dev, |
| 5026 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5027 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5028 | /* initial probe */ |
| 5029 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5030 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5031 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5032 | rbd_dev->parent_overlap = overlap; |
| 5033 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5034 | out: |
| 5035 | ret = 0; |
| 5036 | out_err: |
| 5037 | kfree(reply_buf); |
| 5038 | rbd_spec_put(parent_spec); |
| 5039 | |
| 5040 | return ret; |
| 5041 | } |
| 5042 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5043 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5044 | { |
| 5045 | struct { |
| 5046 | __le64 stripe_unit; |
| 5047 | __le64 stripe_count; |
| 5048 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5049 | size_t size = sizeof (striping_info_buf); |
| 5050 | void *p; |
| 5051 | u64 obj_size; |
| 5052 | u64 stripe_unit; |
| 5053 | u64 stripe_count; |
| 5054 | int ret; |
| 5055 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5056 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5057 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5058 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5059 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5060 | if (ret < 0) |
| 5061 | return ret; |
| 5062 | if (ret < size) |
| 5063 | return -ERANGE; |
| 5064 | |
| 5065 | /* |
| 5066 | * We don't actually support the "fancy striping" feature |
| 5067 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5068 | * defaults the behavior is the same as before. So find |
| 5069 | * out, and only fail if the image has non-default values. |
| 5070 | */ |
| 5071 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5072 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5073 | p = &striping_info_buf; |
| 5074 | stripe_unit = ceph_decode_64(&p); |
| 5075 | if (stripe_unit != obj_size) { |
| 5076 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5077 | "(got %llu want %llu)", |
| 5078 | stripe_unit, obj_size); |
| 5079 | return -EINVAL; |
| 5080 | } |
| 5081 | stripe_count = ceph_decode_64(&p); |
| 5082 | if (stripe_count != 1) { |
| 5083 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5084 | "(got %llu want 1)", stripe_count); |
| 5085 | return -EINVAL; |
| 5086 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5087 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5088 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5089 | |
| 5090 | return 0; |
| 5091 | } |
| 5092 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5093 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5094 | { |
| 5095 | __le64 data_pool_id; |
| 5096 | int ret; |
| 5097 | |
| 5098 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5099 | &rbd_dev->header_oloc, "get_data_pool", |
| 5100 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5101 | if (ret < 0) |
| 5102 | return ret; |
| 5103 | if (ret < sizeof(data_pool_id)) |
| 5104 | return -EBADMSG; |
| 5105 | |
| 5106 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5107 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5111 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5112 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5113 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5114 | size_t image_id_size; |
| 5115 | char *image_id; |
| 5116 | void *p; |
| 5117 | void *end; |
| 5118 | size_t size; |
| 5119 | void *reply_buf = NULL; |
| 5120 | size_t len = 0; |
| 5121 | char *image_name = NULL; |
| 5122 | int ret; |
| 5123 | |
| 5124 | rbd_assert(!rbd_dev->spec->image_name); |
| 5125 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5126 | len = strlen(rbd_dev->spec->image_id); |
| 5127 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5128 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5129 | if (!image_id) |
| 5130 | return NULL; |
| 5131 | |
| 5132 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5133 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5134 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5135 | |
| 5136 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5137 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5138 | if (!reply_buf) |
| 5139 | goto out; |
| 5140 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5141 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5142 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5143 | "dir_get_name", image_id, image_id_size, |
| 5144 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5145 | if (ret < 0) |
| 5146 | goto out; |
| 5147 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5148 | end = reply_buf + ret; |
| 5149 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5150 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5151 | if (IS_ERR(image_name)) |
| 5152 | image_name = NULL; |
| 5153 | else |
| 5154 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5155 | out: |
| 5156 | kfree(reply_buf); |
| 5157 | kfree(image_id); |
| 5158 | |
| 5159 | return image_name; |
| 5160 | } |
| 5161 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5162 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5163 | { |
| 5164 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5165 | const char *snap_name; |
| 5166 | u32 which = 0; |
| 5167 | |
| 5168 | /* Skip over names until we find the one we are looking for */ |
| 5169 | |
| 5170 | snap_name = rbd_dev->header.snap_names; |
| 5171 | while (which < snapc->num_snaps) { |
| 5172 | if (!strcmp(name, snap_name)) |
| 5173 | return snapc->snaps[which]; |
| 5174 | snap_name += strlen(snap_name) + 1; |
| 5175 | which++; |
| 5176 | } |
| 5177 | return CEPH_NOSNAP; |
| 5178 | } |
| 5179 | |
| 5180 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5181 | { |
| 5182 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5183 | u32 which; |
| 5184 | bool found = false; |
| 5185 | u64 snap_id; |
| 5186 | |
| 5187 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5188 | const char *snap_name; |
| 5189 | |
| 5190 | snap_id = snapc->snaps[which]; |
| 5191 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5192 | if (IS_ERR(snap_name)) { |
| 5193 | /* ignore no-longer existing snapshots */ |
| 5194 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5195 | continue; |
| 5196 | else |
| 5197 | break; |
| 5198 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5199 | found = !strcmp(name, snap_name); |
| 5200 | kfree(snap_name); |
| 5201 | } |
| 5202 | return found ? snap_id : CEPH_NOSNAP; |
| 5203 | } |
| 5204 | |
| 5205 | /* |
| 5206 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5207 | * no snapshot by that name is found, or if an error occurs. |
| 5208 | */ |
| 5209 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5210 | { |
| 5211 | if (rbd_dev->image_format == 1) |
| 5212 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5213 | |
| 5214 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5215 | } |
| 5216 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5217 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5218 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5219 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5220 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5221 | { |
| 5222 | struct rbd_spec *spec = rbd_dev->spec; |
| 5223 | |
| 5224 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5225 | rbd_assert(spec->image_id && spec->image_name); |
| 5226 | rbd_assert(spec->snap_name); |
| 5227 | |
| 5228 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5229 | u64 snap_id; |
| 5230 | |
| 5231 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5232 | if (snap_id == CEPH_NOSNAP) |
| 5233 | return -ENOENT; |
| 5234 | |
| 5235 | spec->snap_id = snap_id; |
| 5236 | } else { |
| 5237 | spec->snap_id = CEPH_NOSNAP; |
| 5238 | } |
| 5239 | |
| 5240 | return 0; |
| 5241 | } |
| 5242 | |
| 5243 | /* |
| 5244 | * A parent image will have all ids but none of the names. |
| 5245 | * |
| 5246 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5247 | * can't figure out the name for an image id. |
| 5248 | */ |
| 5249 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5250 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5251 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5252 | struct rbd_spec *spec = rbd_dev->spec; |
| 5253 | const char *pool_name; |
| 5254 | const char *image_name; |
| 5255 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5256 | int ret; |
| 5257 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5258 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5259 | rbd_assert(spec->image_id); |
| 5260 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5261 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5262 | /* 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] | 5263 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5264 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5265 | if (!pool_name) { |
| 5266 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5267 | return -EIO; |
| 5268 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5269 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5270 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5271 | return -ENOMEM; |
| 5272 | |
| 5273 | /* Fetch the image name; tolerate failure here */ |
| 5274 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5275 | image_name = rbd_dev_image_name(rbd_dev); |
| 5276 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5277 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5278 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5279 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5280 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5281 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5282 | if (IS_ERR(snap_name)) { |
| 5283 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5284 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5285 | } |
| 5286 | |
| 5287 | spec->pool_name = pool_name; |
| 5288 | spec->image_name = image_name; |
| 5289 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5290 | |
| 5291 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5292 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5293 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5294 | kfree(image_name); |
| 5295 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5296 | return ret; |
| 5297 | } |
| 5298 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5299 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5300 | { |
| 5301 | size_t size; |
| 5302 | int ret; |
| 5303 | void *reply_buf; |
| 5304 | void *p; |
| 5305 | void *end; |
| 5306 | u64 seq; |
| 5307 | u32 snap_count; |
| 5308 | struct ceph_snap_context *snapc; |
| 5309 | u32 i; |
| 5310 | |
| 5311 | /* |
| 5312 | * We'll need room for the seq value (maximum snapshot id), |
| 5313 | * snapshot count, and array of that many snapshot ids. |
| 5314 | * For now we have a fixed upper limit on the number we're |
| 5315 | * prepared to receive. |
| 5316 | */ |
| 5317 | size = sizeof (__le64) + sizeof (__le32) + |
| 5318 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5319 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5320 | if (!reply_buf) |
| 5321 | return -ENOMEM; |
| 5322 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5323 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5324 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5325 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5326 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5327 | if (ret < 0) |
| 5328 | goto out; |
| 5329 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5330 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5331 | end = reply_buf + ret; |
| 5332 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5333 | ceph_decode_64_safe(&p, end, seq, out); |
| 5334 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5335 | |
| 5336 | /* |
| 5337 | * Make sure the reported number of snapshot ids wouldn't go |
| 5338 | * beyond the end of our buffer. But before checking that, |
| 5339 | * make sure the computed size of the snapshot context we |
| 5340 | * allocate is representable in a size_t. |
| 5341 | */ |
| 5342 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5343 | / sizeof (u64)) { |
| 5344 | ret = -EINVAL; |
| 5345 | goto out; |
| 5346 | } |
| 5347 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5348 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5349 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5350 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5351 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5352 | if (!snapc) { |
| 5353 | ret = -ENOMEM; |
| 5354 | goto out; |
| 5355 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5356 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5357 | for (i = 0; i < snap_count; i++) |
| 5358 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5359 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5360 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5361 | rbd_dev->header.snapc = snapc; |
| 5362 | |
| 5363 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5364 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5365 | out: |
| 5366 | kfree(reply_buf); |
| 5367 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5368 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5369 | } |
| 5370 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5371 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5372 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5373 | { |
| 5374 | size_t size; |
| 5375 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5376 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5377 | int ret; |
| 5378 | void *p; |
| 5379 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5380 | char *snap_name; |
| 5381 | |
| 5382 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5383 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5384 | if (!reply_buf) |
| 5385 | return ERR_PTR(-ENOMEM); |
| 5386 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5387 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5388 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5389 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5390 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5391 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5392 | if (ret < 0) { |
| 5393 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5394 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5395 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5396 | |
| 5397 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5398 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5399 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5400 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5401 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5402 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5403 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5404 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5405 | out: |
| 5406 | kfree(reply_buf); |
| 5407 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5408 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5409 | } |
| 5410 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5411 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5412 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5413 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5414 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5415 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5416 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5417 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5418 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5419 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5420 | if (first_time) { |
| 5421 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5422 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5423 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5424 | } |
| 5425 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5426 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5427 | if (ret && first_time) { |
| 5428 | kfree(rbd_dev->header.object_prefix); |
| 5429 | rbd_dev->header.object_prefix = NULL; |
| 5430 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5431 | |
| 5432 | return ret; |
| 5433 | } |
| 5434 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5435 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5436 | { |
| 5437 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5438 | |
| 5439 | if (rbd_dev->image_format == 1) |
| 5440 | return rbd_dev_v1_header_info(rbd_dev); |
| 5441 | |
| 5442 | return rbd_dev_v2_header_info(rbd_dev); |
| 5443 | } |
| 5444 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5445 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5446 | * Skips over white space at *buf, and updates *buf to point to the |
| 5447 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5448 | * the token (string of non-white space characters) found. Note |
| 5449 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5450 | */ |
| 5451 | static inline size_t next_token(const char **buf) |
| 5452 | { |
| 5453 | /* |
| 5454 | * These are the characters that produce nonzero for |
| 5455 | * isspace() in the "C" and "POSIX" locales. |
| 5456 | */ |
| 5457 | const char *spaces = " \f\n\r\t\v"; |
| 5458 | |
| 5459 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5460 | |
| 5461 | return strcspn(*buf, spaces); /* Return token length */ |
| 5462 | } |
| 5463 | |
| 5464 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5465 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5466 | * enough to hold a copy of it, and copies the token into the new |
| 5467 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5468 | * that a duplicate buffer is created even for a zero-length token. |
| 5469 | * |
| 5470 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5471 | * pointer if memory for the duplicate was not available. If |
| 5472 | * the lenp argument is a non-null pointer, the length of the token |
| 5473 | * (not including the '\0') is returned in *lenp. |
| 5474 | * |
| 5475 | * If successful, the *buf pointer will be updated to point beyond |
| 5476 | * the end of the found token. |
| 5477 | * |
| 5478 | * Note: uses GFP_KERNEL for allocation. |
| 5479 | */ |
| 5480 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5481 | { |
| 5482 | char *dup; |
| 5483 | size_t len; |
| 5484 | |
| 5485 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5486 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5487 | if (!dup) |
| 5488 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5489 | *(dup + len) = '\0'; |
| 5490 | *buf += len; |
| 5491 | |
| 5492 | if (lenp) |
| 5493 | *lenp = len; |
| 5494 | |
| 5495 | return dup; |
| 5496 | } |
| 5497 | |
| 5498 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5499 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5500 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5501 | * and the data written is passed here via a NUL-terminated buffer. |
| 5502 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5503 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5504 | * The information extracted from these options is recorded in |
| 5505 | * the other parameters which return dynamically-allocated |
| 5506 | * structures: |
| 5507 | * ceph_opts |
| 5508 | * The address of a pointer that will refer to a ceph options |
| 5509 | * structure. Caller must release the returned pointer using |
| 5510 | * ceph_destroy_options() when it is no longer needed. |
| 5511 | * rbd_opts |
| 5512 | * Address of an rbd options pointer. Fully initialized by |
| 5513 | * this function; caller must release with kfree(). |
| 5514 | * spec |
| 5515 | * Address of an rbd image specification pointer. Fully |
| 5516 | * initialized by this function based on parsed options. |
| 5517 | * Caller must release with rbd_spec_put(). |
| 5518 | * |
| 5519 | * The options passed take this form: |
| 5520 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5521 | * where: |
| 5522 | * <mon_addrs> |
| 5523 | * A comma-separated list of one or more monitor addresses. |
| 5524 | * A monitor address is an ip address, optionally followed |
| 5525 | * by a port number (separated by a colon). |
| 5526 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5527 | * <options> |
| 5528 | * A comma-separated list of ceph and/or rbd options. |
| 5529 | * <pool_name> |
| 5530 | * The name of the rados pool containing the rbd image. |
| 5531 | * <image_name> |
| 5532 | * The name of the image in that pool to map. |
| 5533 | * <snap_id> |
| 5534 | * An optional snapshot id. If provided, the mapping will |
| 5535 | * present data from the image at the time that snapshot was |
| 5536 | * created. The image head is used if no snapshot id is |
| 5537 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5538 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5539 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5540 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5541 | struct rbd_options **opts, |
| 5542 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5543 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5544 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5545 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5546 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5547 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5548 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5549 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5550 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5551 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5552 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5553 | |
| 5554 | /* The first four tokens are required */ |
| 5555 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5556 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5557 | if (!len) { |
| 5558 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5559 | return -EINVAL; |
| 5560 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5561 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5562 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5563 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5564 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5565 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5566 | options = dup_token(&buf, NULL); |
| 5567 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5568 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5569 | if (!*options) { |
| 5570 | rbd_warn(NULL, "no options provided"); |
| 5571 | goto out_err; |
| 5572 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5573 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5574 | spec = rbd_spec_alloc(); |
| 5575 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5576 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5577 | |
| 5578 | spec->pool_name = dup_token(&buf, NULL); |
| 5579 | if (!spec->pool_name) |
| 5580 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5581 | if (!*spec->pool_name) { |
| 5582 | rbd_warn(NULL, "no pool name provided"); |
| 5583 | goto out_err; |
| 5584 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5585 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5586 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5587 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5588 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5589 | if (!*spec->image_name) { |
| 5590 | rbd_warn(NULL, "no image name provided"); |
| 5591 | goto out_err; |
| 5592 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5593 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5594 | /* |
| 5595 | * Snapshot name is optional; default is to use "-" |
| 5596 | * (indicating the head/no snapshot). |
| 5597 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5598 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5599 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5600 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5601 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5602 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5603 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5604 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5605 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5606 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5607 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5608 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5609 | *(snap_name + len) = '\0'; |
| 5610 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5611 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5612 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5613 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5614 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5615 | if (!rbd_opts) |
| 5616 | goto out_mem; |
| 5617 | |
| 5618 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5619 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5620 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5621 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5622 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5623 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5624 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5625 | if (IS_ERR(copts)) { |
| 5626 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5627 | goto out_err; |
| 5628 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5629 | kfree(options); |
| 5630 | |
| 5631 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5632 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5633 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5634 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5635 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5636 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5637 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5638 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5639 | kfree(rbd_opts); |
| 5640 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5641 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5642 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5643 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5644 | } |
| 5645 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5646 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5647 | * Return pool id (>= 0) or a negative error code. |
| 5648 | */ |
| 5649 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5650 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5651 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5652 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5653 | int tries = 0; |
| 5654 | int ret; |
| 5655 | |
| 5656 | again: |
| 5657 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5658 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5659 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5660 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5661 | if (ret < 0) |
| 5662 | return ret; |
| 5663 | |
| 5664 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5665 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5666 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5667 | newest_epoch, |
| 5668 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5669 | goto again; |
| 5670 | } else { |
| 5671 | /* the osdmap we have is new enough */ |
| 5672 | return -ENOENT; |
| 5673 | } |
| 5674 | } |
| 5675 | |
| 5676 | return ret; |
| 5677 | } |
| 5678 | |
| 5679 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5680 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5681 | * name given to it by the user. Internally, that identifier is |
| 5682 | * what's used to specify the names of objects related to the image. |
| 5683 | * |
| 5684 | * A special "rbd id" object is used to map an rbd image name to its |
| 5685 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5686 | * with the supplied name. |
| 5687 | * |
| 5688 | * This function will record the given rbd_dev's image_id field if |
| 5689 | * it can be determined, and in that case will return 0. If any |
| 5690 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5691 | * image_id field will be unchanged (and should be NULL). |
| 5692 | */ |
| 5693 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5694 | { |
| 5695 | int ret; |
| 5696 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5697 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5698 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5699 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5700 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5701 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5702 | * When probing a parent image, the image id is already |
| 5703 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5704 | * need to fetch the image id again in this case. We |
| 5705 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5706 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5707 | if (rbd_dev->spec->image_id) { |
| 5708 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5709 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5710 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5711 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5712 | |
| 5713 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5714 | * First, see if the format 2 image id file exists, and if |
| 5715 | * so, get the image's persistent id from it. |
| 5716 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5717 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5718 | rbd_dev->spec->image_name); |
| 5719 | if (ret) |
| 5720 | return ret; |
| 5721 | |
| 5722 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5723 | |
| 5724 | /* Response will be an encoded string, which includes a length */ |
| 5725 | |
| 5726 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5727 | response = kzalloc(size, GFP_NOIO); |
| 5728 | if (!response) { |
| 5729 | ret = -ENOMEM; |
| 5730 | goto out; |
| 5731 | } |
| 5732 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5733 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5734 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5735 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5736 | "get_id", NULL, 0, |
| 5737 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5738 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5739 | if (ret == -ENOENT) { |
| 5740 | image_id = kstrdup("", GFP_KERNEL); |
| 5741 | ret = image_id ? 0 : -ENOMEM; |
| 5742 | if (!ret) |
| 5743 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5744 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5745 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5746 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5747 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5748 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5749 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5750 | if (!ret) |
| 5751 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | if (!ret) { |
| 5755 | rbd_dev->spec->image_id = image_id; |
| 5756 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5757 | } |
| 5758 | out: |
| 5759 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5760 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5761 | return ret; |
| 5762 | } |
| 5763 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5764 | /* |
| 5765 | * Undo whatever state changes are made by v1 or v2 header info |
| 5766 | * call. |
| 5767 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5768 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5769 | { |
| 5770 | struct rbd_image_header *header; |
| 5771 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5772 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5773 | |
| 5774 | /* Free dynamic fields from the header, then zero it out */ |
| 5775 | |
| 5776 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5777 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5778 | kfree(header->snap_sizes); |
| 5779 | kfree(header->snap_names); |
| 5780 | kfree(header->object_prefix); |
| 5781 | memset(header, 0, sizeof (*header)); |
| 5782 | } |
| 5783 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5784 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5785 | { |
| 5786 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5787 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5788 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5789 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5790 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5791 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5792 | /* |
| 5793 | * Get the and check features for the image. Currently the |
| 5794 | * features are assumed to never change. |
| 5795 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5796 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5797 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5798 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5799 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5800 | /* If the image supports fancy striping, get its parameters */ |
| 5801 | |
| 5802 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5803 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5804 | if (ret < 0) |
| 5805 | goto out_err; |
| 5806 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5807 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5808 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5809 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5810 | if (ret) |
| 5811 | goto out_err; |
| 5812 | } |
| 5813 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5814 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5815 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5816 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5817 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5818 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5819 | kfree(rbd_dev->header.object_prefix); |
| 5820 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5821 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5822 | } |
| 5823 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5824 | /* |
| 5825 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5826 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5827 | * length of the already discovered part of the parent chain. |
| 5828 | */ |
| 5829 | 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] | 5830 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5831 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5832 | int ret; |
| 5833 | |
| 5834 | if (!rbd_dev->parent_spec) |
| 5835 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5836 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5837 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5838 | pr_info("parent chain is too long (%d)\n", depth); |
| 5839 | ret = -EINVAL; |
| 5840 | goto out_err; |
| 5841 | } |
| 5842 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5843 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5844 | if (!parent) { |
| 5845 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5846 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | /* |
| 5850 | * Images related by parent/child relationships always share |
| 5851 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5852 | */ |
| 5853 | __rbd_get_client(rbd_dev->rbd_client); |
| 5854 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5855 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5856 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5857 | if (ret < 0) |
| 5858 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5859 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5860 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5861 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5862 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5863 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5864 | out_err: |
| 5865 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5866 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5867 | return ret; |
| 5868 | } |
| 5869 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5870 | /* |
| 5871 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5872 | * upon return. |
| 5873 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5874 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5875 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5876 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5877 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5878 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5879 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5880 | if (!single_major) { |
| 5881 | ret = register_blkdev(0, rbd_dev->name); |
| 5882 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5883 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5884 | |
| 5885 | rbd_dev->major = ret; |
| 5886 | rbd_dev->minor = 0; |
| 5887 | } else { |
| 5888 | rbd_dev->major = rbd_major; |
| 5889 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5890 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5891 | |
| 5892 | /* Set up the blkdev mapping. */ |
| 5893 | |
| 5894 | ret = rbd_init_disk(rbd_dev); |
| 5895 | if (ret) |
| 5896 | goto err_out_blkdev; |
| 5897 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5898 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5899 | if (ret) |
| 5900 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5901 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5902 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 5903 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5904 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5905 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
| 5906 | ret = device_add(&rbd_dev->dev); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5907 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5908 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5909 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5910 | /* Everything's ready. Announce the disk to the world. */ |
| 5911 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5912 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5913 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5914 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5915 | spin_lock(&rbd_dev_list_lock); |
| 5916 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5917 | spin_unlock(&rbd_dev_list_lock); |
| 5918 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5919 | add_disk(rbd_dev->disk); |
Ilya Dryomov | ca7909e | 2016-08-18 18:38:41 +0200 | [diff] [blame] | 5920 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5921 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5922 | rbd_dev->header.features); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5923 | |
| 5924 | return ret; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5925 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5926 | err_out_mapping: |
| 5927 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5928 | err_out_disk: |
| 5929 | rbd_free_disk(rbd_dev); |
| 5930 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5931 | if (!single_major) |
| 5932 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5933 | err_out_unlock: |
| 5934 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5935 | return ret; |
| 5936 | } |
| 5937 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5938 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5939 | { |
| 5940 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5941 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5942 | |
| 5943 | /* Record the header object name for this rbd image. */ |
| 5944 | |
| 5945 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5946 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5947 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5948 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5949 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5950 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5951 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5952 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5953 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5954 | } |
| 5955 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5956 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5957 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5958 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5959 | rbd_dev->image_format = 0; |
| 5960 | kfree(rbd_dev->spec->image_id); |
| 5961 | rbd_dev->spec->image_id = NULL; |
| 5962 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5963 | rbd_dev_destroy(rbd_dev); |
| 5964 | } |
| 5965 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5966 | /* |
| 5967 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5968 | * device. If this image is the one being mapped (i.e., not a |
| 5969 | * parent), initiate a watch on its header object before using that |
| 5970 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5971 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5972 | 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] | 5973 | { |
| 5974 | int ret; |
| 5975 | |
| 5976 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5977 | * Get the id from the image id object. Unless there's an |
| 5978 | * error, rbd_dev->spec->image_id will be filled in with |
| 5979 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5980 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5981 | */ |
| 5982 | ret = rbd_dev_image_id(rbd_dev); |
| 5983 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5984 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5985 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5986 | ret = rbd_dev_header_name(rbd_dev); |
| 5987 | if (ret) |
| 5988 | goto err_out_format; |
| 5989 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5990 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5991 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5992 | if (ret) { |
| 5993 | if (ret == -ENOENT) |
| 5994 | pr_info("image %s/%s does not exist\n", |
| 5995 | rbd_dev->spec->pool_name, |
| 5996 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5997 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5998 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5999 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6000 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6001 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6002 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6003 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6004 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6005 | /* |
| 6006 | * If this image is the one being mapped, we have pool name and |
| 6007 | * id, image name and id, and snap name - need to fill snap id. |
| 6008 | * Otherwise this is a parent image, identified by pool, image |
| 6009 | * and snap ids - need to fill in names for those ids. |
| 6010 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6011 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6012 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6013 | else |
| 6014 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6015 | if (ret) { |
| 6016 | if (ret == -ENOENT) |
| 6017 | pr_info("snap %s/%s@%s does not exist\n", |
| 6018 | rbd_dev->spec->pool_name, |
| 6019 | rbd_dev->spec->image_name, |
| 6020 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6021 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6022 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6023 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6024 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6025 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6026 | if (ret) |
| 6027 | goto err_out_probe; |
| 6028 | |
| 6029 | /* |
| 6030 | * Need to warn users if this image is the one being |
| 6031 | * mapped and has a parent. |
| 6032 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6033 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6034 | rbd_warn(rbd_dev, |
| 6035 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6036 | } |
| 6037 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6038 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6039 | if (ret) |
| 6040 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6041 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6042 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6043 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6044 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6045 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6046 | err_out_probe: |
| 6047 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6048 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6049 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6050 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6051 | err_out_format: |
| 6052 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6053 | kfree(rbd_dev->spec->image_id); |
| 6054 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6055 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6056 | } |
| 6057 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6058 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6059 | const char *buf, |
| 6060 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6061 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6062 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6063 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6064 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6065 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6066 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6067 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6068 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6069 | |
| 6070 | if (!try_module_get(THIS_MODULE)) |
| 6071 | return -ENODEV; |
| 6072 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6073 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6074 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6075 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6076 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6077 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6078 | rbdc = rbd_get_client(ceph_opts); |
| 6079 | if (IS_ERR(rbdc)) { |
| 6080 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6081 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6082 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6083 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6084 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6085 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6086 | if (rc < 0) { |
| 6087 | if (rc == -ENOENT) |
| 6088 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6089 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6090 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6091 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6092 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6093 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6094 | if (!rbd_dev) { |
| 6095 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6096 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6097 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6098 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6099 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6100 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6101 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6102 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6103 | if (!rbd_dev->config_info) { |
| 6104 | rc = -ENOMEM; |
| 6105 | goto err_out_rbd_dev; |
| 6106 | } |
| 6107 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6108 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6109 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6110 | if (rc < 0) { |
| 6111 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6112 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6113 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6114 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6115 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6116 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6117 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6118 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6119 | read_only = true; |
| 6120 | rbd_dev->mapping.read_only = read_only; |
| 6121 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6122 | rc = rbd_dev_device_setup(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6123 | if (rc) { |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6124 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6125 | * rbd_unregister_watch() can't be moved into |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6126 | * rbd_dev_image_release() without refactoring, see |
| 6127 | * commit 1f3ef78861ac. |
| 6128 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6129 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6130 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6131 | goto out; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6132 | } |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6133 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6134 | rc = count; |
| 6135 | out: |
| 6136 | module_put(THIS_MODULE); |
| 6137 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6138 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6139 | err_out_rbd_dev: |
| 6140 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6141 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6142 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6143 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6144 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6145 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6146 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6147 | } |
| 6148 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6149 | static ssize_t rbd_add(struct bus_type *bus, |
| 6150 | const char *buf, |
| 6151 | size_t count) |
| 6152 | { |
| 6153 | if (single_major) |
| 6154 | return -EINVAL; |
| 6155 | |
| 6156 | return do_rbd_add(bus, buf, count); |
| 6157 | } |
| 6158 | |
| 6159 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6160 | const char *buf, |
| 6161 | size_t count) |
| 6162 | { |
| 6163 | return do_rbd_add(bus, buf, count); |
| 6164 | } |
| 6165 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6166 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6167 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6168 | rbd_free_disk(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6169 | |
| 6170 | spin_lock(&rbd_dev_list_lock); |
| 6171 | list_del_init(&rbd_dev->node); |
| 6172 | spin_unlock(&rbd_dev_list_lock); |
| 6173 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6174 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6175 | device_del(&rbd_dev->dev); |
Alex Elder | 6d80b13 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6176 | rbd_dev_mapping_clear(rbd_dev); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6177 | if (!single_major) |
| 6178 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6179 | } |
| 6180 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6181 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6182 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6183 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6184 | struct rbd_device *first = rbd_dev; |
| 6185 | struct rbd_device *second = first->parent; |
| 6186 | struct rbd_device *third; |
| 6187 | |
| 6188 | /* |
| 6189 | * Follow to the parent with no grandparent and |
| 6190 | * remove it. |
| 6191 | */ |
| 6192 | while (second && (third = second->parent)) { |
| 6193 | first = second; |
| 6194 | second = third; |
| 6195 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6196 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6197 | rbd_dev_image_release(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6198 | first->parent = NULL; |
| 6199 | first->parent_overlap = 0; |
| 6200 | |
| 6201 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6202 | rbd_spec_put(first->parent_spec); |
| 6203 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6207 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6208 | const char *buf, |
| 6209 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6210 | { |
| 6211 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6212 | struct list_head *tmp; |
| 6213 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6214 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6215 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6216 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6217 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6218 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6219 | dev_id = -1; |
| 6220 | opt_buf[0] = '\0'; |
| 6221 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6222 | if (dev_id < 0) { |
| 6223 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6224 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6225 | } |
| 6226 | if (opt_buf[0] != '\0') { |
| 6227 | if (!strcmp(opt_buf, "force")) { |
| 6228 | force = true; |
| 6229 | } else { |
| 6230 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6231 | return -EINVAL; |
| 6232 | } |
| 6233 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6234 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6235 | ret = -ENOENT; |
| 6236 | spin_lock(&rbd_dev_list_lock); |
| 6237 | list_for_each(tmp, &rbd_dev_list) { |
| 6238 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6239 | if (rbd_dev->dev_id == dev_id) { |
| 6240 | ret = 0; |
| 6241 | break; |
| 6242 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6243 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6244 | if (!ret) { |
| 6245 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6246 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6247 | ret = -EBUSY; |
| 6248 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6249 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6250 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6251 | spin_unlock_irq(&rbd_dev->lock); |
| 6252 | } |
| 6253 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6254 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6255 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6256 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6257 | if (force) { |
| 6258 | /* |
| 6259 | * Prevent new IO from being queued and wait for existing |
| 6260 | * IO to complete/fail. |
| 6261 | */ |
| 6262 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6263 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6264 | } |
| 6265 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6266 | down_write(&rbd_dev->lock_rwsem); |
| 6267 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6268 | rbd_unlock(rbd_dev); |
| 6269 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6270 | rbd_unregister_watch(rbd_dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6271 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 6272 | /* |
| 6273 | * Don't free anything from rbd_dev->disk until after all |
| 6274 | * notifies are completely processed. Otherwise |
| 6275 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting |
| 6276 | * in a potential use after free of rbd_dev->disk or rbd_dev. |
| 6277 | */ |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6278 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6279 | rbd_dev_image_release(rbd_dev); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 6280 | |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6281 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6282 | } |
| 6283 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6284 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6285 | const char *buf, |
| 6286 | size_t count) |
| 6287 | { |
| 6288 | if (single_major) |
| 6289 | return -EINVAL; |
| 6290 | |
| 6291 | return do_rbd_remove(bus, buf, count); |
| 6292 | } |
| 6293 | |
| 6294 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6295 | const char *buf, |
| 6296 | size_t count) |
| 6297 | { |
| 6298 | return do_rbd_remove(bus, buf, count); |
| 6299 | } |
| 6300 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6301 | /* |
| 6302 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6303 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6304 | */ |
| 6305 | static int rbd_sysfs_init(void) |
| 6306 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6307 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6308 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6309 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6310 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6311 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6312 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6313 | ret = bus_register(&rbd_bus_type); |
| 6314 | if (ret < 0) |
| 6315 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6316 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6317 | return ret; |
| 6318 | } |
| 6319 | |
| 6320 | static void rbd_sysfs_cleanup(void) |
| 6321 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6322 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6323 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6326 | static int rbd_slab_init(void) |
| 6327 | { |
| 6328 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6329 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6330 | if (!rbd_img_request_cache) |
| 6331 | return -ENOMEM; |
| 6332 | |
| 6333 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6334 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6335 | if (!rbd_obj_request_cache) |
| 6336 | goto out_err; |
| 6337 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6338 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6339 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6340 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6341 | kmem_cache_destroy(rbd_img_request_cache); |
| 6342 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6343 | return -ENOMEM; |
| 6344 | } |
| 6345 | |
| 6346 | static void rbd_slab_exit(void) |
| 6347 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6348 | rbd_assert(rbd_obj_request_cache); |
| 6349 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6350 | rbd_obj_request_cache = NULL; |
| 6351 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6352 | rbd_assert(rbd_img_request_cache); |
| 6353 | kmem_cache_destroy(rbd_img_request_cache); |
| 6354 | rbd_img_request_cache = NULL; |
| 6355 | } |
| 6356 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6357 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6358 | { |
| 6359 | int rc; |
| 6360 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6361 | if (!libceph_compatible(NULL)) { |
| 6362 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6363 | return -EINVAL; |
| 6364 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6365 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6366 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6367 | if (rc) |
| 6368 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6369 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6370 | /* |
| 6371 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6372 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6373 | */ |
| 6374 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6375 | if (!rbd_wq) { |
| 6376 | rc = -ENOMEM; |
| 6377 | goto err_out_slab; |
| 6378 | } |
| 6379 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6380 | if (single_major) { |
| 6381 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6382 | if (rbd_major < 0) { |
| 6383 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6384 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6385 | } |
| 6386 | } |
| 6387 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6388 | rc = rbd_sysfs_init(); |
| 6389 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6390 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6391 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6392 | if (single_major) |
| 6393 | pr_info("loaded (major %d)\n", rbd_major); |
| 6394 | else |
| 6395 | pr_info("loaded\n"); |
| 6396 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6397 | return 0; |
| 6398 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6399 | err_out_blkdev: |
| 6400 | if (single_major) |
| 6401 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6402 | err_out_wq: |
| 6403 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6404 | err_out_slab: |
| 6405 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6406 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6407 | } |
| 6408 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6409 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6410 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6411 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6412 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6413 | if (single_major) |
| 6414 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6415 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6416 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6417 | } |
| 6418 | |
| 6419 | module_init(rbd_init); |
| 6420 | module_exit(rbd_exit); |
| 6421 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6422 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6423 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6424 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6425 | /* following authorship retained from original osdblk.c */ |
| 6426 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6427 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6428 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6429 | MODULE_LICENSE("GPL"); |