Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | rbd.c -- Export ceph rados objects as a Linux block device |
| 4 | |
| 5 | |
| 6 | based on drivers/block/osdblk.c: |
| 7 | |
| 8 | Copyright 2009 Red Hat, Inc. |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; see the file COPYING. If not, write to |
| 21 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | |
| 23 | |
| 24 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 25 | For usage instructions, please refer to: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 26 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 27 | Documentation/ABI/testing/sysfs-bus-rbd |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 28 | |
| 29 | */ |
| 30 | |
| 31 | #include <linux/ceph/libceph.h> |
| 32 | #include <linux/ceph/osd_client.h> |
| 33 | #include <linux/ceph/mon_client.h> |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 34 | #include <linux/ceph/cls_lock_client.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 35 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 36 | #include <linux/parser.h> |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 37 | #include <linux/bsearch.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/device.h> |
| 41 | #include <linux/module.h> |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 42 | #include <linux/blk-mq.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 43 | #include <linux/fs.h> |
| 44 | #include <linux/blkdev.h> |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 45 | #include <linux/slab.h> |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 46 | #include <linux/idr.h> |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 47 | #include <linux/workqueue.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 48 | |
| 49 | #include "rbd_types.h" |
| 50 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 51 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
| 52 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 53 | /* |
| 54 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 55 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 56 | * universally 512 bytes. These symbols are just slightly more |
| 57 | * meaningful than the bare numbers they represent. |
| 58 | */ |
| 59 | #define SECTOR_SHIFT 9 |
| 60 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 61 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Increment the given counter and return its updated value. |
| 64 | * If the counter is already 0 it will not be incremented. |
| 65 | * If the counter is already at its maximum value returns |
| 66 | * -EINVAL without updating it. |
| 67 | */ |
| 68 | static int atomic_inc_return_safe(atomic_t *v) |
| 69 | { |
| 70 | unsigned int counter; |
| 71 | |
| 72 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); |
| 73 | if (counter <= (unsigned int)INT_MAX) |
| 74 | return (int)counter; |
| 75 | |
| 76 | atomic_dec(v); |
| 77 | |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 82 | static int atomic_dec_return_safe(atomic_t *v) |
| 83 | { |
| 84 | int counter; |
| 85 | |
| 86 | counter = atomic_dec_return(v); |
| 87 | if (counter >= 0) |
| 88 | return counter; |
| 89 | |
| 90 | atomic_inc(v); |
| 91 | |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 95 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 96 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 97 | #define RBD_MINORS_PER_MAJOR 256 |
| 98 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 99 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 100 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 101 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 102 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 103 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 104 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 105 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 106 | #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 107 | |
| 108 | #define RBD_SNAP_HEAD_NAME "-" |
| 109 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 110 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 111 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 112 | /* This allows a single page to hold an image name sent by OSD */ |
| 113 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 114 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 115 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 116 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 117 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 118 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 119 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 120 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 121 | /* Feature bits */ |
| 122 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 123 | #define RBD_FEATURE_LAYERING (1ULL<<0) |
| 124 | #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) |
| 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) |
| 126 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 127 | #define RBD_FEATURE_OPERATIONS (1ULL<<8) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 128 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 129 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 130 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 131 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 132 | RBD_FEATURE_DATA_POOL | \ |
| 133 | RBD_FEATURE_OPERATIONS) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 134 | |
| 135 | /* Features supported by this (client software) implementation. */ |
| 136 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 137 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 138 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 139 | /* |
| 140 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 141 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 142 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 143 | #define DEV_NAME_LEN 32 |
| 144 | |
| 145 | /* |
| 146 | * block device image metadata (in-memory version) |
| 147 | */ |
| 148 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 149 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 150 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 151 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 152 | u64 stripe_unit; |
| 153 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 154 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 155 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 156 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 157 | /* The remaining fields need to be updated occasionally */ |
| 158 | u64 image_size; |
| 159 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 160 | char *snap_names; /* format 1 only */ |
| 161 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 162 | }; |
| 163 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 164 | /* |
| 165 | * An rbd image specification. |
| 166 | * |
| 167 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 168 | * identify an image. Each rbd_dev structure includes a pointer to |
| 169 | * an rbd_spec structure that encapsulates this identity. |
| 170 | * |
| 171 | * Each of the id's in an rbd_spec has an associated name. For a |
| 172 | * user-mapped image, the names are supplied and the id's associated |
| 173 | * with them are looked up. For a layered image, a parent image is |
| 174 | * defined by the tuple, and the names are looked up. |
| 175 | * |
| 176 | * An rbd_dev structure contains a parent_spec pointer which is |
| 177 | * non-null if the image it represents is a child in a layered |
| 178 | * image. This pointer will refer to the rbd_spec structure used |
| 179 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 180 | * is shared between the parent and child). |
| 181 | * |
| 182 | * Since these structures are populated once, during the discovery |
| 183 | * phase of image construction, they are effectively immutable so |
| 184 | * we make no effort to synchronize access to them. |
| 185 | * |
| 186 | * Note that code herein does not assume the image name is known (it |
| 187 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 188 | */ |
| 189 | struct rbd_spec { |
| 190 | u64 pool_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 191 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 192 | |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 193 | const char *image_id; |
| 194 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 195 | |
| 196 | u64 snap_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 197 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 198 | |
| 199 | struct kref kref; |
| 200 | }; |
| 201 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 202 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 203 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 204 | */ |
| 205 | struct rbd_client { |
| 206 | struct ceph_client *client; |
| 207 | struct kref kref; |
| 208 | struct list_head node; |
| 209 | }; |
| 210 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 211 | struct rbd_img_request; |
| 212 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); |
| 213 | |
| 214 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ |
| 215 | |
| 216 | struct rbd_obj_request; |
| 217 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); |
| 218 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 219 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 220 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 221 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 222 | OBJ_REQUEST_PAGES, |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 223 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 224 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 225 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 226 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 227 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 228 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 229 | }; |
| 230 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 231 | enum obj_req_flags { |
| 232 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 233 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 234 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
| 235 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 236 | }; |
| 237 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 238 | struct rbd_obj_request { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 239 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 240 | u64 offset; /* object start byte */ |
| 241 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 242 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 243 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 244 | /* |
| 245 | * An object request associated with an image will have its |
| 246 | * img_data flag set; a standalone object request will not. |
| 247 | * |
| 248 | * A standalone object request will have which == BAD_WHICH |
| 249 | * and a null obj_request pointer. |
| 250 | * |
| 251 | * An object request initiated in support of a layered image |
| 252 | * object (to check for its existence before a write) will |
| 253 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 254 | * |
| 255 | * Finally, an object request for rbd image data will have |
| 256 | * which != BAD_WHICH, and will have a non-null img_request |
| 257 | * pointer. The value of which will be in the range |
| 258 | * 0..(img_request->obj_request_count-1). |
| 259 | */ |
| 260 | union { |
| 261 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 262 | struct { |
| 263 | struct rbd_img_request *img_request; |
| 264 | u64 img_offset; |
| 265 | /* links for img_request->obj_requests list */ |
| 266 | struct list_head links; |
| 267 | }; |
| 268 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 269 | u32 which; /* posn image request list */ |
| 270 | |
| 271 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 272 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 273 | struct ceph_bio_iter bio_pos; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 274 | struct { |
| 275 | struct page **pages; |
| 276 | u32 page_count; |
| 277 | }; |
| 278 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 279 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 280 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | |
| 282 | struct ceph_osd_request *osd_req; |
| 283 | |
| 284 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 285 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 286 | |
| 287 | rbd_obj_callback_t callback; |
| 288 | |
| 289 | struct kref kref; |
| 290 | }; |
| 291 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 292 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 293 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 294 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 295 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 296 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 297 | }; |
| 298 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 299 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 300 | struct rbd_device *rbd_dev; |
| 301 | u64 offset; /* starting image byte offset */ |
| 302 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 303 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 304 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 305 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 306 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 307 | }; |
| 308 | union { |
| 309 | struct request *rq; /* block request */ |
| 310 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 311 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 312 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 313 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 314 | spinlock_t completion_lock;/* protects next_completion */ |
| 315 | u32 next_completion; |
| 316 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 317 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 318 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 319 | |
| 320 | u32 obj_request_count; |
| 321 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 322 | |
| 323 | struct kref kref; |
| 324 | }; |
| 325 | |
| 326 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 327 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 328 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 329 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 330 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 331 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 332 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 333 | enum rbd_watch_state { |
| 334 | RBD_WATCH_STATE_UNREGISTERED, |
| 335 | RBD_WATCH_STATE_REGISTERED, |
| 336 | RBD_WATCH_STATE_ERROR, |
| 337 | }; |
| 338 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 339 | enum rbd_lock_state { |
| 340 | RBD_LOCK_STATE_UNLOCKED, |
| 341 | RBD_LOCK_STATE_LOCKED, |
| 342 | RBD_LOCK_STATE_RELEASING, |
| 343 | }; |
| 344 | |
| 345 | /* WatchNotify::ClientId */ |
| 346 | struct rbd_client_id { |
| 347 | u64 gid; |
| 348 | u64 handle; |
| 349 | }; |
| 350 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 351 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 352 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 353 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 354 | }; |
| 355 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 356 | /* |
| 357 | * a single device |
| 358 | */ |
| 359 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 360 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | |
| 362 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 363 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 364 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 365 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 366 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 367 | struct rbd_client *rbd_client; |
| 368 | |
| 369 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 370 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 371 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 372 | |
| 373 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 374 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 375 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 376 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 377 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 378 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 379 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 380 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 381 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 382 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 383 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 384 | struct mutex watch_mutex; |
| 385 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 386 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 387 | u64 watch_cookie; |
| 388 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 389 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 390 | struct rw_semaphore lock_rwsem; |
| 391 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 392 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 393 | struct rbd_client_id owner_cid; |
| 394 | struct work_struct acquired_lock_work; |
| 395 | struct work_struct released_lock_work; |
| 396 | struct delayed_work lock_dwork; |
| 397 | struct work_struct unlock_work; |
| 398 | wait_queue_head_t lock_waitq; |
| 399 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 400 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 401 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 402 | struct rbd_spec *parent_spec; |
| 403 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 404 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 405 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 406 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 407 | /* Block layer tags. */ |
| 408 | struct blk_mq_tag_set tag_set; |
| 409 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 410 | /* protects updating the header */ |
| 411 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 412 | |
| 413 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 414 | |
| 415 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 416 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 417 | /* sysfs related */ |
| 418 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 419 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 420 | }; |
| 421 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 422 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 423 | * Flag bits for rbd_dev->flags: |
| 424 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 425 | * by rbd_dev->lock |
| 426 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 427 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 428 | enum rbd_dev_flags { |
| 429 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 430 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 431 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 432 | }; |
| 433 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 434 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 435 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 436 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 437 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 438 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 439 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 440 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 441 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 442 | /* Slab caches for frequently-allocated structures */ |
| 443 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 444 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 445 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 446 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 447 | static struct bio_set *rbd_bio_clone; |
| 448 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 449 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 450 | static DEFINE_IDA(rbd_dev_id_ida); |
| 451 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 452 | static struct workqueue_struct *rbd_wq; |
| 453 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 454 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 455 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 456 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 457 | static bool single_major = true; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 458 | module_param(single_major, bool, S_IRUGO); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 459 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: true)"); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 460 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 461 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 462 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 463 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 464 | size_t count); |
| 465 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 466 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 467 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 468 | size_t count); |
| 469 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 470 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 471 | 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] | 472 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 473 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 474 | static int rbd_dev_id_to_minor(int dev_id) |
| 475 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 476 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static int minor_to_rbd_dev_id(int minor) |
| 480 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 481 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 484 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 485 | { |
| 486 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 487 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 488 | } |
| 489 | |
| 490 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 491 | { |
| 492 | bool is_lock_owner; |
| 493 | |
| 494 | down_read(&rbd_dev->lock_rwsem); |
| 495 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 496 | up_read(&rbd_dev->lock_rwsem); |
| 497 | return is_lock_owner; |
| 498 | } |
| 499 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 500 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 501 | { |
| 502 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 503 | } |
| 504 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 505 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 506 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 507 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 508 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 509 | static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 510 | |
| 511 | static struct attribute *rbd_bus_attrs[] = { |
| 512 | &bus_attr_add.attr, |
| 513 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 514 | &bus_attr_add_single_major.attr, |
| 515 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 516 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 517 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 518 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 519 | |
| 520 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 521 | struct attribute *attr, int index) |
| 522 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 523 | if (!single_major && |
| 524 | (attr == &bus_attr_add_single_major.attr || |
| 525 | attr == &bus_attr_remove_single_major.attr)) |
| 526 | return 0; |
| 527 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 528 | return attr->mode; |
| 529 | } |
| 530 | |
| 531 | static const struct attribute_group rbd_bus_group = { |
| 532 | .attrs = rbd_bus_attrs, |
| 533 | .is_visible = rbd_bus_is_visible, |
| 534 | }; |
| 535 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 536 | |
| 537 | static struct bus_type rbd_bus_type = { |
| 538 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 539 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | static void rbd_root_dev_release(struct device *dev) |
| 543 | { |
| 544 | } |
| 545 | |
| 546 | static struct device rbd_root_dev = { |
| 547 | .init_name = "rbd", |
| 548 | .release = rbd_root_dev_release, |
| 549 | }; |
| 550 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 551 | static __printf(2, 3) |
| 552 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 553 | { |
| 554 | struct va_format vaf; |
| 555 | va_list args; |
| 556 | |
| 557 | va_start(args, fmt); |
| 558 | vaf.fmt = fmt; |
| 559 | vaf.va = &args; |
| 560 | |
| 561 | if (!rbd_dev) |
| 562 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 563 | else if (rbd_dev->disk) |
| 564 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 565 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 566 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 567 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 568 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 569 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 570 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 571 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 572 | else /* punt */ |
| 573 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 574 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 575 | va_end(args); |
| 576 | } |
| 577 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 578 | #ifdef RBD_DEBUG |
| 579 | #define rbd_assert(expr) \ |
| 580 | if (unlikely(!(expr))) { \ |
| 581 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 582 | "at line %d:\n\n" \ |
| 583 | "\trbd_assert(%s);\n\n", \ |
| 584 | __func__, __LINE__, #expr); \ |
| 585 | BUG(); \ |
| 586 | } |
| 587 | #else /* !RBD_DEBUG */ |
| 588 | # define rbd_assert(expr) ((void) 0) |
| 589 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 590 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 591 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 592 | 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] | 593 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 594 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 595 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 596 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 597 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 598 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 599 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 600 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 601 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 602 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 603 | u8 *order, u64 *snap_size); |
| 604 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 605 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 606 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 607 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 608 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 609 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 610 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 611 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 612 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 613 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 614 | removing = true; |
| 615 | else |
| 616 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 617 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 618 | if (removing) |
| 619 | return -ENOENT; |
| 620 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 621 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 622 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 626 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 627 | { |
| 628 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 629 | unsigned long open_count_before; |
| 630 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 633 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 634 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 636 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 639 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 640 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 641 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 643 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 644 | return -EFAULT; |
| 645 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 646 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 647 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 648 | return -EROFS; |
| 649 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 650 | /* Let blkdev_roset() handle it */ |
| 651 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 655 | unsigned int cmd, unsigned long arg) |
| 656 | { |
| 657 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 658 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 659 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 660 | switch (cmd) { |
| 661 | case BLKROSET: |
| 662 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 663 | break; |
| 664 | default: |
| 665 | ret = -ENOTTY; |
| 666 | } |
| 667 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | #ifdef CONFIG_COMPAT |
| 672 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 673 | unsigned int cmd, unsigned long arg) |
| 674 | { |
| 675 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 676 | } |
| 677 | #endif /* CONFIG_COMPAT */ |
| 678 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 679 | static const struct block_device_operations rbd_bd_ops = { |
| 680 | .owner = THIS_MODULE, |
| 681 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 682 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 683 | .ioctl = rbd_ioctl, |
| 684 | #ifdef CONFIG_COMPAT |
| 685 | .compat_ioctl = rbd_compat_ioctl, |
| 686 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 687 | }; |
| 688 | |
| 689 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 690 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 691 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 692 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 693 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 694 | { |
| 695 | struct rbd_client *rbdc; |
| 696 | int ret = -ENOMEM; |
| 697 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 698 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 699 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 700 | if (!rbdc) |
| 701 | goto out_opt; |
| 702 | |
| 703 | kref_init(&rbdc->kref); |
| 704 | INIT_LIST_HEAD(&rbdc->node); |
| 705 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 706 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 707 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 708 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 709 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 710 | |
| 711 | ret = ceph_open_session(rbdc->client); |
| 712 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 713 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 715 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 716 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 717 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 718 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 719 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 720 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 722 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 723 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 724 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 725 | kfree(rbdc); |
| 726 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 727 | if (ceph_opts) |
| 728 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 729 | dout("%s: error %d\n", __func__, ret); |
| 730 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 731 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 734 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 735 | { |
| 736 | kref_get(&rbdc->kref); |
| 737 | |
| 738 | return rbdc; |
| 739 | } |
| 740 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 741 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 742 | * Find a ceph client with specific addr and configuration. If |
| 743 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 744 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 745 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 746 | { |
| 747 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 748 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 749 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 750 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 751 | return NULL; |
| 752 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 753 | spin_lock(&rbd_client_list_lock); |
| 754 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 755 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 756 | __rbd_get_client(client_node); |
| 757 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 758 | found = true; |
| 759 | break; |
| 760 | } |
| 761 | } |
| 762 | spin_unlock(&rbd_client_list_lock); |
| 763 | |
| 764 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 768 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 769 | */ |
| 770 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 771 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 772 | Opt_last_int, |
| 773 | /* int args above */ |
| 774 | Opt_last_string, |
| 775 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 776 | Opt_read_only, |
| 777 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 778 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 779 | Opt_exclusive, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 780 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 781 | }; |
| 782 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 783 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 784 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 785 | /* int args above */ |
| 786 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 787 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 788 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 789 | {Opt_read_write, "read_write"}, |
| 790 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 791 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 792 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 793 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | }; |
| 795 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 796 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 797 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 798 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 799 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 800 | bool exclusive; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 801 | }; |
| 802 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 803 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 804 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 805 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 806 | #define RBD_EXCLUSIVE_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 807 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 808 | static int parse_rbd_opts_token(char *c, void *private) |
| 809 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 810 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 811 | substring_t argstr[MAX_OPT_ARGS]; |
| 812 | int token, intval, ret; |
| 813 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 814 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 815 | if (token < Opt_last_int) { |
| 816 | ret = match_int(&argstr[0], &intval); |
| 817 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 818 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 819 | return ret; |
| 820 | } |
| 821 | dout("got int token %d val %d\n", token, intval); |
| 822 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 823 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 824 | } else { |
| 825 | dout("got token %d\n", token); |
| 826 | } |
| 827 | |
| 828 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 829 | case Opt_queue_depth: |
| 830 | if (intval < 1) { |
| 831 | pr_err("queue_depth out of range\n"); |
| 832 | return -EINVAL; |
| 833 | } |
| 834 | rbd_opts->queue_depth = intval; |
| 835 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 836 | case Opt_read_only: |
| 837 | rbd_opts->read_only = true; |
| 838 | break; |
| 839 | case Opt_read_write: |
| 840 | rbd_opts->read_only = false; |
| 841 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 842 | case Opt_lock_on_read: |
| 843 | rbd_opts->lock_on_read = true; |
| 844 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 845 | case Opt_exclusive: |
| 846 | rbd_opts->exclusive = true; |
| 847 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 848 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 849 | /* libceph prints "bad option" msg */ |
| 850 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 851 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 852 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 856 | static char* obj_op_name(enum obj_operation_type op_type) |
| 857 | { |
| 858 | switch (op_type) { |
| 859 | case OBJ_OP_READ: |
| 860 | return "read"; |
| 861 | case OBJ_OP_WRITE: |
| 862 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 863 | case OBJ_OP_DISCARD: |
| 864 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 865 | default: |
| 866 | return "???"; |
| 867 | } |
| 868 | } |
| 869 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 870 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 871 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 872 | * not exist create it. Either way, ceph_opts is consumed by this |
| 873 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 874 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 875 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 876 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 877 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 878 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 879 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 880 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 881 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 882 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 883 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 884 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 885 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 886 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 887 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 892 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 893 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 894 | */ |
| 895 | static void rbd_client_release(struct kref *kref) |
| 896 | { |
| 897 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 898 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 899 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 900 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 902 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 903 | |
| 904 | ceph_destroy_client(rbdc->client); |
| 905 | kfree(rbdc); |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 910 | * it. |
| 911 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 912 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 913 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 914 | if (rbdc) |
| 915 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 918 | static bool rbd_image_format_valid(u32 image_format) |
| 919 | { |
| 920 | return image_format == 1 || image_format == 2; |
| 921 | } |
| 922 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 923 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 924 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 925 | size_t size; |
| 926 | u32 snap_count; |
| 927 | |
| 928 | /* The header has to start with the magic rbd header text */ |
| 929 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 930 | return false; |
| 931 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 932 | /* The bio layer requires at least sector-sized I/O */ |
| 933 | |
| 934 | if (ondisk->options.order < SECTOR_SHIFT) |
| 935 | return false; |
| 936 | |
| 937 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 938 | |
| 939 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 940 | return false; |
| 941 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 942 | /* |
| 943 | * The size of a snapshot header has to fit in a size_t, and |
| 944 | * that limits the number of snapshots. |
| 945 | */ |
| 946 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 947 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 948 | if (snap_count > size / sizeof (__le64)) |
| 949 | return false; |
| 950 | |
| 951 | /* |
| 952 | * Not only that, but the size of the entire the snapshot |
| 953 | * header must also be representable in a size_t. |
| 954 | */ |
| 955 | size -= snap_count * sizeof (__le64); |
| 956 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 957 | return false; |
| 958 | |
| 959 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 960 | } |
| 961 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 962 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 963 | * returns the size of an object in the image |
| 964 | */ |
| 965 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 966 | { |
| 967 | return 1U << header->obj_order; |
| 968 | } |
| 969 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 970 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 971 | { |
| 972 | if (rbd_dev->header.stripe_unit == 0 || |
| 973 | rbd_dev->header.stripe_count == 0) { |
| 974 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 975 | rbd_dev->header.stripe_count = 1; |
| 976 | } |
| 977 | |
| 978 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 979 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 980 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 981 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 982 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 983 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 984 | } |
| 985 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 986 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 987 | * Fill an rbd image header with information from the given format 1 |
| 988 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 989 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 990 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 991 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 992 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 993 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 994 | bool first_time = header->object_prefix == NULL; |
| 995 | struct ceph_snap_context *snapc; |
| 996 | char *object_prefix = NULL; |
| 997 | char *snap_names = NULL; |
| 998 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 999 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1000 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1001 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1002 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1003 | /* Allocate this now to avoid having to handle failure below */ |
| 1004 | |
| 1005 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1006 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1007 | sizeof(ondisk->object_prefix), |
| 1008 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | if (!object_prefix) |
| 1010 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1014 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1015 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1016 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1017 | if (!snapc) |
| 1018 | goto out_err; |
| 1019 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1020 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1021 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1022 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1023 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1025 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1026 | if (snap_names_len > (u64)SIZE_MAX) |
| 1027 | goto out_2big; |
| 1028 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1029 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1030 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1031 | |
| 1032 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1033 | snap_sizes = kmalloc_array(snap_count, |
| 1034 | sizeof(*header->snap_sizes), |
| 1035 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1036 | if (!snap_sizes) |
| 1037 | goto out_err; |
| 1038 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1039 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1040 | * Copy the names, and fill in each snapshot's id |
| 1041 | * and size. |
| 1042 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1043 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1044 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1045 | * snap_names_len bytes beyond the end of the |
| 1046 | * snapshot id array, this memcpy() is safe. |
| 1047 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1048 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1049 | snaps = ondisk->snaps; |
| 1050 | for (i = 0; i < snap_count; i++) { |
| 1051 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1052 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1053 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1054 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1055 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1056 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1057 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1058 | if (first_time) { |
| 1059 | header->object_prefix = object_prefix; |
| 1060 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1061 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1062 | } else { |
| 1063 | ceph_put_snap_context(header->snapc); |
| 1064 | kfree(header->snap_names); |
| 1065 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1069 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1070 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1071 | header->snapc = snapc; |
| 1072 | header->snap_names = snap_names; |
| 1073 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1074 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1075 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1076 | out_2big: |
| 1077 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1078 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1079 | kfree(snap_sizes); |
| 1080 | kfree(snap_names); |
| 1081 | ceph_put_snap_context(snapc); |
| 1082 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1083 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1084 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1087 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1088 | { |
| 1089 | const char *snap_name; |
| 1090 | |
| 1091 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1092 | |
| 1093 | /* Skip over names until we find the one we are looking for */ |
| 1094 | |
| 1095 | snap_name = rbd_dev->header.snap_names; |
| 1096 | while (which--) |
| 1097 | snap_name += strlen(snap_name) + 1; |
| 1098 | |
| 1099 | return kstrdup(snap_name, GFP_KERNEL); |
| 1100 | } |
| 1101 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1102 | /* |
| 1103 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1104 | * Note that result is for snapshots in *descending* order. |
| 1105 | */ |
| 1106 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1107 | { |
| 1108 | u64 snap_id1 = *(u64 *)s1; |
| 1109 | u64 snap_id2 = *(u64 *)s2; |
| 1110 | |
| 1111 | if (snap_id1 < snap_id2) |
| 1112 | return 1; |
| 1113 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1114 | } |
| 1115 | |
| 1116 | /* |
| 1117 | * Search a snapshot context to see if the given snapshot id is |
| 1118 | * present. |
| 1119 | * |
| 1120 | * Returns the position of the snapshot id in the array if it's found, |
| 1121 | * or BAD_SNAP_INDEX otherwise. |
| 1122 | * |
| 1123 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1124 | * reverse order, highest snapshot id first. |
| 1125 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1126 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1127 | { |
| 1128 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1129 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1130 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1131 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1132 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1133 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1134 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1135 | } |
| 1136 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1137 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1138 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1139 | { |
| 1140 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1141 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1142 | |
| 1143 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1144 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1145 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1146 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1147 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1148 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1149 | } |
| 1150 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1151 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1152 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1153 | if (snap_id == CEPH_NOSNAP) |
| 1154 | return RBD_SNAP_HEAD_NAME; |
| 1155 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1156 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1157 | if (rbd_dev->image_format == 1) |
| 1158 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1159 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1160 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1163 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1164 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1165 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1166 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1167 | if (snap_id == CEPH_NOSNAP) { |
| 1168 | *snap_size = rbd_dev->header.image_size; |
| 1169 | } else if (rbd_dev->image_format == 1) { |
| 1170 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1171 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1172 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1173 | if (which == BAD_SNAP_INDEX) |
| 1174 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1175 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1176 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1177 | } else { |
| 1178 | u64 size = 0; |
| 1179 | int ret; |
| 1180 | |
| 1181 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1182 | if (ret) |
| 1183 | return ret; |
| 1184 | |
| 1185 | *snap_size = size; |
| 1186 | } |
| 1187 | return 0; |
| 1188 | } |
| 1189 | |
| 1190 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1191 | u64 *snap_features) |
| 1192 | { |
| 1193 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1194 | if (snap_id == CEPH_NOSNAP) { |
| 1195 | *snap_features = rbd_dev->header.features; |
| 1196 | } else if (rbd_dev->image_format == 1) { |
| 1197 | *snap_features = 0; /* No features for format 1 */ |
| 1198 | } else { |
| 1199 | u64 features = 0; |
| 1200 | int ret; |
| 1201 | |
| 1202 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1203 | if (ret) |
| 1204 | return ret; |
| 1205 | |
| 1206 | *snap_features = features; |
| 1207 | } |
| 1208 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1209 | } |
| 1210 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1211 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1212 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1213 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1214 | u64 size = 0; |
| 1215 | u64 features = 0; |
| 1216 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1217 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1218 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1219 | if (ret) |
| 1220 | return ret; |
| 1221 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1222 | if (ret) |
| 1223 | return ret; |
| 1224 | |
| 1225 | rbd_dev->mapping.size = size; |
| 1226 | rbd_dev->mapping.features = features; |
| 1227 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1228 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1231 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1232 | { |
| 1233 | rbd_dev->mapping.size = 0; |
| 1234 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1235 | } |
| 1236 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1237 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1238 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1239 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1240 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1241 | return offset & (segment_size - 1); |
| 1242 | } |
| 1243 | |
| 1244 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1245 | u64 offset, u64 length) |
| 1246 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1247 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1248 | |
| 1249 | offset &= segment_size - 1; |
| 1250 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1251 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1252 | if (offset + length > segment_size) |
| 1253 | length = segment_size - offset; |
| 1254 | |
| 1255 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 1258 | static void zero_bvec(struct bio_vec *bv) |
| 1259 | { |
| 1260 | void *buf; |
| 1261 | unsigned long flags; |
| 1262 | |
| 1263 | buf = bvec_kmap_irq(bv, &flags); |
| 1264 | memset(buf, 0, bv->bv_len); |
| 1265 | flush_dcache_page(bv->bv_page); |
| 1266 | bvec_kunmap_irq(buf, &flags); |
| 1267 | } |
| 1268 | |
| 1269 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
| 1270 | { |
| 1271 | struct ceph_bio_iter it = *bio_pos; |
| 1272 | |
| 1273 | ceph_bio_iter_advance(&it, off); |
| 1274 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1275 | zero_bvec(&bv); |
| 1276 | })); |
| 1277 | } |
| 1278 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1279 | /* |
| 1280 | * bio helpers |
| 1281 | */ |
| 1282 | |
| 1283 | static void bio_chain_put(struct bio *chain) |
| 1284 | { |
| 1285 | struct bio *tmp; |
| 1286 | |
| 1287 | while (chain) { |
| 1288 | tmp = chain; |
| 1289 | chain = chain->bi_next; |
| 1290 | bio_put(tmp); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /* |
| 1295 | * zeros a bio chain, starting at specific offset |
| 1296 | */ |
| 1297 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1298 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1299 | struct bio_vec bv; |
| 1300 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1301 | unsigned long flags; |
| 1302 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1303 | int pos = 0; |
| 1304 | |
| 1305 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1306 | bio_for_each_segment(bv, chain, iter) { |
| 1307 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1308 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1309 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1310 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1311 | bv.bv_len - remainder); |
| 1312 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1313 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1314 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1315 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | chain = chain->bi_next; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1323 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1324 | * starting at the given byte offset from the start of the array and |
| 1325 | * continuing up to the given end offset. The pages array is |
| 1326 | * assumed to be big enough to hold all bytes up to the end. |
| 1327 | */ |
| 1328 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1329 | { |
| 1330 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1331 | |
| 1332 | rbd_assert(end > offset); |
| 1333 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1334 | while (offset < end) { |
| 1335 | size_t page_offset; |
| 1336 | size_t length; |
| 1337 | unsigned long flags; |
| 1338 | void *kaddr; |
| 1339 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1340 | page_offset = offset & ~PAGE_MASK; |
| 1341 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1342 | local_irq_save(flags); |
| 1343 | kaddr = kmap_atomic(*page); |
| 1344 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1345 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1346 | kunmap_atomic(kaddr); |
| 1347 | local_irq_restore(flags); |
| 1348 | |
| 1349 | offset += length; |
| 1350 | page++; |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1355 | * Clone a portion of a bio, starting at the given byte offset |
| 1356 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1357 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1358 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1359 | unsigned int offset, |
| 1360 | unsigned int len, |
| 1361 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1362 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1363 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1364 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1365 | bio = bio_clone_fast(bio_src, gfpmask, rbd_bio_clone); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1366 | if (!bio) |
| 1367 | return NULL; /* ENOMEM */ |
| 1368 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1369 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1370 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1371 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1372 | return bio; |
| 1373 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1374 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1375 | /* |
| 1376 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1377 | * into the first bio in the source chain and continuing for the |
| 1378 | * number of bytes indicated. The result is another bio chain of |
| 1379 | * exactly the given length, or a null pointer on error. |
| 1380 | * |
| 1381 | * The bio_src and offset parameters are both in-out. On entry they |
| 1382 | * refer to the first source bio and the offset into that bio where |
| 1383 | * the start of data to be cloned is located. |
| 1384 | * |
| 1385 | * On return, bio_src is updated to refer to the bio in the source |
| 1386 | * chain that contains first un-cloned byte, and *offset will |
| 1387 | * contain the offset of that byte within that bio. |
| 1388 | */ |
| 1389 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1390 | unsigned int *offset, |
| 1391 | unsigned int len, |
| 1392 | gfp_t gfpmask) |
| 1393 | { |
| 1394 | struct bio *bi = *bio_src; |
| 1395 | unsigned int off = *offset; |
| 1396 | struct bio *chain = NULL; |
| 1397 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1398 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1399 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1400 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1401 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1402 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1403 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1404 | end = &chain; |
| 1405 | while (len) { |
| 1406 | unsigned int bi_size; |
| 1407 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1408 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1409 | if (!bi) { |
| 1410 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1411 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1412 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1413 | 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] | 1414 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1415 | if (!bio) |
| 1416 | goto out_err; /* ENOMEM */ |
| 1417 | |
| 1418 | *end = bio; |
| 1419 | end = &bio->bi_next; |
| 1420 | |
| 1421 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1422 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1423 | bi = bi->bi_next; |
| 1424 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1425 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1426 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1427 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1428 | *bio_src = bi; |
| 1429 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1430 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1431 | return chain; |
| 1432 | out_err: |
| 1433 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1434 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1435 | return NULL; |
| 1436 | } |
| 1437 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1438 | /* |
| 1439 | * The default/initial value for all object request flags is 0. For |
| 1440 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1441 | * again. |
| 1442 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1443 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1444 | { |
| 1445 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1446 | struct rbd_device *rbd_dev; |
| 1447 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1448 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1449 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1450 | obj_request); |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1455 | { |
| 1456 | smp_mb(); |
| 1457 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1458 | } |
| 1459 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1460 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1461 | { |
| 1462 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1463 | struct rbd_device *rbd_dev = NULL; |
| 1464 | |
| 1465 | if (obj_request_img_data_test(obj_request)) |
| 1466 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1467 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1468 | obj_request); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1473 | { |
| 1474 | smp_mb(); |
| 1475 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1476 | } |
| 1477 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1478 | /* |
| 1479 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1480 | * flag. The latter is set based on the "exists" value provided. |
| 1481 | * |
| 1482 | * Note that for our purposes once an object exists it never goes |
| 1483 | * away again. It's possible that the response from two existence |
| 1484 | * checks are separated by the creation of the target object, and |
| 1485 | * the first ("doesn't exist") response arrives *after* the second |
| 1486 | * ("does exist"). In that case we ignore the second one. |
| 1487 | */ |
| 1488 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1489 | bool exists) |
| 1490 | { |
| 1491 | if (exists) |
| 1492 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1493 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1494 | smp_mb(); |
| 1495 | } |
| 1496 | |
| 1497 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1498 | { |
| 1499 | smp_mb(); |
| 1500 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1501 | } |
| 1502 | |
| 1503 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1504 | { |
| 1505 | smp_mb(); |
| 1506 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1507 | } |
| 1508 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1509 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1510 | { |
| 1511 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1512 | |
| 1513 | return obj_request->img_offset < |
| 1514 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1515 | } |
| 1516 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1517 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1518 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1519 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1520 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1521 | kref_get(&obj_request->kref); |
| 1522 | } |
| 1523 | |
| 1524 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1525 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1526 | { |
| 1527 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1528 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1529 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1530 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1531 | } |
| 1532 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1533 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1534 | { |
| 1535 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1536 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1537 | kref_get(&img_request->kref); |
| 1538 | } |
| 1539 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1540 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1541 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1542 | static void rbd_img_request_destroy(struct kref *kref); |
| 1543 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1544 | { |
| 1545 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1546 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1547 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1548 | if (img_request_child_test(img_request)) |
| 1549 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1550 | else |
| 1551 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1555 | struct rbd_obj_request *obj_request) |
| 1556 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1557 | rbd_assert(obj_request->img_request == NULL); |
| 1558 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1559 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1560 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1561 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1562 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1563 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1564 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1565 | img_request->obj_request_count++; |
| 1566 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1567 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1568 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1572 | struct rbd_obj_request *obj_request) |
| 1573 | { |
| 1574 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1575 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1576 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1577 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1578 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1579 | rbd_assert(img_request->obj_request_count > 0); |
| 1580 | img_request->obj_request_count--; |
| 1581 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1582 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1583 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1584 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1585 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1586 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1587 | rbd_obj_request_put(obj_request); |
| 1588 | } |
| 1589 | |
| 1590 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1591 | { |
| 1592 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1593 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1594 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1595 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1596 | return true; |
| 1597 | default: |
| 1598 | return false; |
| 1599 | } |
| 1600 | } |
| 1601 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1602 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1603 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1604 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1605 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1606 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1607 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1608 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1609 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1610 | obj_request->length, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1611 | if (obj_request_img_data_test(obj_request)) { |
| 1612 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1613 | rbd_img_request_get(obj_request->img_request); |
| 1614 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1615 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1619 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1620 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1621 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1622 | |
| 1623 | /* |
| 1624 | * If no error occurred, compute the aggregate transfer |
| 1625 | * count for the image request. We could instead use |
| 1626 | * atomic64_cmpxchg() to update it as each object request |
| 1627 | * completes; not clear which way is better off hand. |
| 1628 | */ |
| 1629 | if (!img_request->result) { |
| 1630 | struct rbd_obj_request *obj_request; |
| 1631 | u64 xferred = 0; |
| 1632 | |
| 1633 | for_each_obj_request(img_request, obj_request) |
| 1634 | xferred += obj_request->xferred; |
| 1635 | img_request->xferred = xferred; |
| 1636 | } |
| 1637 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1638 | if (img_request->callback) |
| 1639 | img_request->callback(img_request); |
| 1640 | else |
| 1641 | rbd_img_request_put(img_request); |
| 1642 | } |
| 1643 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1644 | /* |
| 1645 | * The default/initial value for all image request flags is 0. Each |
| 1646 | * is conditionally set to 1 at image request initialization time |
| 1647 | * and currently never change thereafter. |
| 1648 | */ |
| 1649 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1650 | { |
| 1651 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1652 | smp_mb(); |
| 1653 | } |
| 1654 | |
| 1655 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1656 | { |
| 1657 | smp_mb(); |
| 1658 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1659 | } |
| 1660 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1661 | /* |
| 1662 | * Set the discard flag when the img_request is an discard request |
| 1663 | */ |
| 1664 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1665 | { |
| 1666 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1667 | smp_mb(); |
| 1668 | } |
| 1669 | |
| 1670 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1671 | { |
| 1672 | smp_mb(); |
| 1673 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1674 | } |
| 1675 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1676 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1677 | { |
| 1678 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1679 | smp_mb(); |
| 1680 | } |
| 1681 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1682 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1683 | { |
| 1684 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1685 | smp_mb(); |
| 1686 | } |
| 1687 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1688 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1689 | { |
| 1690 | smp_mb(); |
| 1691 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1692 | } |
| 1693 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1694 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1695 | { |
| 1696 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1697 | smp_mb(); |
| 1698 | } |
| 1699 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1700 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1701 | { |
| 1702 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1703 | smp_mb(); |
| 1704 | } |
| 1705 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1706 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1707 | { |
| 1708 | smp_mb(); |
| 1709 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1710 | } |
| 1711 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1712 | static enum obj_operation_type |
| 1713 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1714 | { |
| 1715 | if (img_request_write_test(img_request)) |
| 1716 | return OBJ_OP_WRITE; |
| 1717 | else if (img_request_discard_test(img_request)) |
| 1718 | return OBJ_OP_DISCARD; |
| 1719 | else |
| 1720 | return OBJ_OP_READ; |
| 1721 | } |
| 1722 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1723 | static void |
| 1724 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1725 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1726 | u64 xferred = obj_request->xferred; |
| 1727 | u64 length = obj_request->length; |
| 1728 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1729 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1730 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1731 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1732 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1733 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1734 | * length of the request. A short read also implies zero-fill |
| 1735 | * to the end of the request. An error requires the whole |
| 1736 | * length of the request to be reported finished with an error |
| 1737 | * to the block layer. In each case we update the xferred |
| 1738 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1739 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1740 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1741 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1742 | if (obj_request->type == OBJ_REQUEST_BIO) |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 1743 | zero_bios(&obj_request->bio_pos, 0, length); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1744 | else |
| 1745 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1746 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1747 | } else if (xferred < length && !obj_request->result) { |
| 1748 | if (obj_request->type == OBJ_REQUEST_BIO) |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 1749 | zero_bios(&obj_request->bio_pos, xferred, |
| 1750 | length - xferred); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1751 | else |
| 1752 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1753 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1754 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1755 | obj_request_done_set(obj_request); |
| 1756 | } |
| 1757 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1758 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1759 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1760 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1761 | obj_request->callback); |
Ilya Dryomov | 2e584bc | 2018-01-15 17:24:51 +0100 | [diff] [blame] | 1762 | obj_request->callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1763 | } |
| 1764 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1765 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1766 | { |
| 1767 | obj_request->result = err; |
| 1768 | obj_request->xferred = 0; |
| 1769 | /* |
| 1770 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1771 | * rbd_img_obj_callback() |
| 1772 | */ |
| 1773 | if (obj_request_img_data_test(obj_request)) { |
| 1774 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1775 | rbd_img_request_get(obj_request->img_request); |
| 1776 | } |
| 1777 | obj_request_done_set(obj_request); |
| 1778 | rbd_obj_request_complete(obj_request); |
| 1779 | } |
| 1780 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1781 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1782 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1783 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1784 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1785 | bool layered = false; |
| 1786 | |
| 1787 | if (obj_request_img_data_test(obj_request)) { |
| 1788 | img_request = obj_request->img_request; |
| 1789 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1790 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1791 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1792 | |
| 1793 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1794 | obj_request, img_request, obj_request->result, |
| 1795 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1796 | if (layered && obj_request->result == -ENOENT && |
| 1797 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1798 | rbd_img_parent_read(obj_request); |
| 1799 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1800 | rbd_img_obj_request_read_callback(obj_request); |
| 1801 | else |
| 1802 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1803 | } |
| 1804 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1805 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1806 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1807 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1808 | obj_request->result, obj_request->length); |
| 1809 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1810 | * There is no such thing as a successful short write. Set |
| 1811 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1812 | */ |
| 1813 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1814 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1815 | } |
| 1816 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1817 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1818 | { |
| 1819 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1820 | obj_request->result, obj_request->length); |
| 1821 | /* |
| 1822 | * There is no such thing as a successful short discard. Set |
| 1823 | * it to our originally-requested length. |
| 1824 | */ |
| 1825 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1826 | /* discarding a non-existent object is not a problem */ |
| 1827 | if (obj_request->result == -ENOENT) |
| 1828 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1829 | obj_request_done_set(obj_request); |
| 1830 | } |
| 1831 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1832 | /* |
| 1833 | * For a simple stat call there's nothing to do. We'll do more if |
| 1834 | * this is part of a write sequence for a layered image. |
| 1835 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1836 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1837 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1838 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1839 | obj_request_done_set(obj_request); |
| 1840 | } |
| 1841 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1842 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1843 | { |
| 1844 | dout("%s: obj %p\n", __func__, obj_request); |
| 1845 | |
| 1846 | if (obj_request_img_data_test(obj_request)) |
| 1847 | rbd_osd_copyup_callback(obj_request); |
| 1848 | else |
| 1849 | obj_request_done_set(obj_request); |
| 1850 | } |
| 1851 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1852 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1853 | { |
| 1854 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1855 | u16 opcode; |
| 1856 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1857 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1858 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1859 | if (obj_request_img_data_test(obj_request)) { |
| 1860 | rbd_assert(obj_request->img_request); |
| 1861 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1862 | } else { |
| 1863 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1864 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1865 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1866 | if (osd_req->r_result < 0) |
| 1867 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1868 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1869 | /* |
| 1870 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1871 | * passed to the block layer, which just supports a 32-bit |
| 1872 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1873 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1874 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1875 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1876 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1877 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1878 | switch (opcode) { |
| 1879 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1880 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1881 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1882 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1883 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1884 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1885 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1886 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1887 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1888 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1889 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1890 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1891 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1892 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1893 | case CEPH_OSD_OP_DELETE: |
| 1894 | case CEPH_OSD_OP_TRUNCATE: |
| 1895 | case CEPH_OSD_OP_ZERO: |
| 1896 | rbd_osd_discard_callback(obj_request); |
| 1897 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1898 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1899 | rbd_osd_call_callback(obj_request); |
| 1900 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1901 | default: |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1902 | rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d", |
| 1903 | obj_request->object_no, opcode); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1904 | break; |
| 1905 | } |
| 1906 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1907 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1908 | rbd_obj_request_complete(obj_request); |
| 1909 | } |
| 1910 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1911 | 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] | 1912 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1913 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1914 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1915 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1916 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1920 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1921 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1922 | |
Deepa Dinamani | 1134e09 | 2017-05-08 15:59:19 -0700 | [diff] [blame] | 1923 | ktime_get_real_ts(&osd_req->r_mtime); |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1924 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1925 | } |
| 1926 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1927 | static struct ceph_osd_request * |
| 1928 | __rbd_osd_req_create(struct rbd_device *rbd_dev, |
| 1929 | struct ceph_snap_context *snapc, |
| 1930 | int num_ops, unsigned int flags, |
| 1931 | struct rbd_obj_request *obj_request) |
| 1932 | { |
| 1933 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1934 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1935 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1936 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1937 | |
| 1938 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
| 1939 | if (!req) |
| 1940 | return NULL; |
| 1941 | |
| 1942 | req->r_flags = flags; |
| 1943 | req->r_callback = rbd_osd_req_callback; |
| 1944 | req->r_priv = obj_request; |
| 1945 | |
| 1946 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1947 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1948 | rbd_dev->header.object_prefix, obj_request->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1949 | goto err_req; |
| 1950 | |
| 1951 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1952 | goto err_req; |
| 1953 | |
| 1954 | return req; |
| 1955 | |
| 1956 | err_req: |
| 1957 | ceph_osdc_put_request(req); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1961 | /* |
| 1962 | * Create an osd request. A read request has one osd op (read). |
| 1963 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1964 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1965 | * technically osd watch is a write request, hence this distinction.) |
| 1966 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1967 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1968 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1969 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1970 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1971 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1972 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1973 | struct ceph_snap_context *snapc = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1974 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1975 | if (obj_request_img_data_test(obj_request) && |
| 1976 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1977 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1978 | if (op_type == OBJ_OP_WRITE) { |
| 1979 | rbd_assert(img_request_write_test(img_request)); |
| 1980 | } else { |
| 1981 | rbd_assert(img_request_discard_test(img_request)); |
| 1982 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1983 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1984 | } |
| 1985 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1986 | 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] | 1987 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1988 | return __rbd_osd_req_create(rbd_dev, snapc, num_ops, |
| 1989 | (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ? |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 1990 | CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1991 | } |
| 1992 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1993 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 1994 | * Create a copyup osd request based on the information in the object |
| 1995 | * request supplied. A copyup request has two or three osd ops, a |
| 1996 | * copyup method call, potentially a hint op, and a write or truncate |
| 1997 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1998 | */ |
| 1999 | static struct ceph_osd_request * |
| 2000 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 2001 | { |
| 2002 | struct rbd_img_request *img_request; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2003 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2004 | |
| 2005 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2006 | img_request = obj_request->img_request; |
| 2007 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2008 | rbd_assert(img_request_write_test(img_request) || |
| 2009 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2010 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2011 | if (img_request_discard_test(img_request)) |
| 2012 | num_osd_ops = 2; |
| 2013 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2014 | return __rbd_osd_req_create(img_request->rbd_dev, |
| 2015 | img_request->snapc, num_osd_ops, |
Ilya Dryomov | 54ea004 | 2017-02-11 18:48:41 +0100 | [diff] [blame] | 2016 | CEPH_OSD_FLAG_WRITE, obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2017 | } |
| 2018 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2019 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2020 | { |
| 2021 | ceph_osdc_put_request(osd_req); |
| 2022 | } |
| 2023 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2024 | static struct rbd_obj_request * |
| 2025 | rbd_obj_request_create(enum obj_request_type type) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2026 | { |
| 2027 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2028 | |
| 2029 | rbd_assert(obj_request_type_valid(type)); |
| 2030 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2031 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2032 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2033 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2034 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2035 | obj_request->which = BAD_WHICH; |
| 2036 | obj_request->type = type; |
| 2037 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2038 | kref_init(&obj_request->kref); |
| 2039 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2040 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2041 | return obj_request; |
| 2042 | } |
| 2043 | |
| 2044 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2045 | { |
| 2046 | struct rbd_obj_request *obj_request; |
| 2047 | |
| 2048 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2049 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2050 | dout("%s: obj %p\n", __func__, obj_request); |
| 2051 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2052 | rbd_assert(obj_request->img_request == NULL); |
| 2053 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2054 | |
| 2055 | if (obj_request->osd_req) |
| 2056 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2057 | |
| 2058 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2059 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2060 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2061 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2062 | break; /* Nothing to do */ |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2063 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2064 | /* img_data requests don't own their page array */ |
| 2065 | if (obj_request->pages && |
| 2066 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2067 | ceph_release_page_vector(obj_request->pages, |
| 2068 | obj_request->page_count); |
| 2069 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2070 | } |
| 2071 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2072 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2073 | } |
| 2074 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2075 | /* It's OK to call this for a device with no parent */ |
| 2076 | |
| 2077 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2078 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2079 | { |
| 2080 | rbd_dev_remove_parent(rbd_dev); |
| 2081 | rbd_spec_put(rbd_dev->parent_spec); |
| 2082 | rbd_dev->parent_spec = NULL; |
| 2083 | rbd_dev->parent_overlap = 0; |
| 2084 | } |
| 2085 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2086 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2087 | * Parent image reference counting is used to determine when an |
| 2088 | * image's parent fields can be safely torn down--after there are no |
| 2089 | * more in-flight requests to the parent image. When the last |
| 2090 | * reference is dropped, cleaning them up is safe. |
| 2091 | */ |
| 2092 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2093 | { |
| 2094 | int counter; |
| 2095 | |
| 2096 | if (!rbd_dev->parent_spec) |
| 2097 | return; |
| 2098 | |
| 2099 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2100 | if (counter > 0) |
| 2101 | return; |
| 2102 | |
| 2103 | /* Last reference; clean up parent data structures */ |
| 2104 | |
| 2105 | if (!counter) |
| 2106 | rbd_dev_unparent(rbd_dev); |
| 2107 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2108 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2109 | } |
| 2110 | |
| 2111 | /* |
| 2112 | * If an image has a non-zero parent overlap, get a reference to its |
| 2113 | * parent. |
| 2114 | * |
| 2115 | * Returns true if the rbd device has a parent with a non-zero |
| 2116 | * overlap and a reference for it was successfully taken, or |
| 2117 | * false otherwise. |
| 2118 | */ |
| 2119 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2120 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2121 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2122 | |
| 2123 | if (!rbd_dev->parent_spec) |
| 2124 | return false; |
| 2125 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2126 | down_read(&rbd_dev->header_rwsem); |
| 2127 | if (rbd_dev->parent_overlap) |
| 2128 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2129 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2130 | |
| 2131 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2132 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2133 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2134 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2135 | } |
| 2136 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2137 | /* |
| 2138 | * Caller is responsible for filling in the list of object requests |
| 2139 | * that comprises the image request, and the Linux request pointer |
| 2140 | * (if there is one). |
| 2141 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2142 | static struct rbd_img_request *rbd_img_request_create( |
| 2143 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2144 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2145 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2146 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2147 | { |
| 2148 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2149 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 2150 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2151 | if (!img_request) |
| 2152 | return NULL; |
| 2153 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2154 | img_request->rbd_dev = rbd_dev; |
| 2155 | img_request->offset = offset; |
| 2156 | img_request->length = length; |
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); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 2168 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2169 | spin_lock_init(&img_request->completion_lock); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2170 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2171 | kref_init(&img_request->kref); |
| 2172 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2173 | 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] | 2174 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2175 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2176 | return img_request; |
| 2177 | } |
| 2178 | |
| 2179 | static void rbd_img_request_destroy(struct kref *kref) |
| 2180 | { |
| 2181 | struct rbd_img_request *img_request; |
| 2182 | struct rbd_obj_request *obj_request; |
| 2183 | struct rbd_obj_request *next_obj_request; |
| 2184 | |
| 2185 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2186 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2187 | dout("%s: img %p\n", __func__, img_request); |
| 2188 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2189 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2190 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2191 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2192 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2193 | if (img_request_layered_test(img_request)) { |
| 2194 | img_request_layered_clear(img_request); |
| 2195 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2196 | } |
| 2197 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2198 | if (img_request_write_test(img_request) || |
| 2199 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2200 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2201 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2202 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2203 | } |
| 2204 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2205 | static struct rbd_img_request *rbd_parent_request_create( |
| 2206 | struct rbd_obj_request *obj_request, |
| 2207 | u64 img_offset, u64 length) |
| 2208 | { |
| 2209 | struct rbd_img_request *parent_request; |
| 2210 | struct rbd_device *rbd_dev; |
| 2211 | |
| 2212 | rbd_assert(obj_request->img_request); |
| 2213 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2214 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2215 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2216 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2217 | if (!parent_request) |
| 2218 | return NULL; |
| 2219 | |
| 2220 | img_request_child_set(parent_request); |
| 2221 | rbd_obj_request_get(obj_request); |
| 2222 | parent_request->obj_request = obj_request; |
| 2223 | |
| 2224 | return parent_request; |
| 2225 | } |
| 2226 | |
| 2227 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2228 | { |
| 2229 | struct rbd_img_request *parent_request; |
| 2230 | struct rbd_obj_request *orig_request; |
| 2231 | |
| 2232 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2233 | orig_request = parent_request->obj_request; |
| 2234 | |
| 2235 | parent_request->obj_request = NULL; |
| 2236 | rbd_obj_request_put(orig_request); |
| 2237 | img_request_child_clear(parent_request); |
| 2238 | |
| 2239 | rbd_img_request_destroy(kref); |
| 2240 | } |
| 2241 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2242 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2243 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2244 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2245 | unsigned int xferred; |
| 2246 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2247 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2248 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2249 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2250 | img_request = obj_request->img_request; |
| 2251 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2252 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2253 | xferred = (unsigned int)obj_request->xferred; |
| 2254 | result = obj_request->result; |
| 2255 | if (result) { |
| 2256 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2257 | enum obj_operation_type op_type; |
| 2258 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2259 | if (img_request_discard_test(img_request)) |
| 2260 | op_type = OBJ_OP_DISCARD; |
| 2261 | else if (img_request_write_test(img_request)) |
| 2262 | op_type = OBJ_OP_WRITE; |
| 2263 | else |
| 2264 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2265 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2266 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2267 | obj_op_name(op_type), obj_request->length, |
| 2268 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2269 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2270 | result, xferred); |
| 2271 | if (!img_request->result) |
| 2272 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2273 | /* |
| 2274 | * Need to end I/O on the entire obj_request worth of |
| 2275 | * bytes in case of error. |
| 2276 | */ |
| 2277 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2278 | } |
| 2279 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2280 | if (img_request_child_test(img_request)) { |
| 2281 | rbd_assert(img_request->obj_request != NULL); |
| 2282 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2283 | } else { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2284 | blk_status_t status = errno_to_blk_status(result); |
| 2285 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2286 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2287 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2288 | more = blk_update_request(img_request->rq, status, xferred); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2289 | if (!more) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2290 | __blk_mq_end_request(img_request->rq, status); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2294 | } |
| 2295 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2296 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2297 | { |
| 2298 | struct rbd_img_request *img_request; |
| 2299 | u32 which = obj_request->which; |
| 2300 | bool more = true; |
| 2301 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2302 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2303 | img_request = obj_request->img_request; |
| 2304 | |
| 2305 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2306 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2307 | rbd_assert(img_request->obj_request_count > 0); |
| 2308 | rbd_assert(which != BAD_WHICH); |
| 2309 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2310 | |
| 2311 | spin_lock_irq(&img_request->completion_lock); |
| 2312 | if (which != img_request->next_completion) |
| 2313 | goto out; |
| 2314 | |
| 2315 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2316 | rbd_assert(more); |
| 2317 | rbd_assert(which < img_request->obj_request_count); |
| 2318 | |
| 2319 | if (!obj_request_done_test(obj_request)) |
| 2320 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2321 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2322 | which++; |
| 2323 | } |
| 2324 | |
| 2325 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2326 | img_request->next_completion = which; |
| 2327 | out: |
| 2328 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2329 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2330 | |
| 2331 | if (!more) |
| 2332 | rbd_img_request_complete(img_request); |
| 2333 | } |
| 2334 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2335 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2336 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2337 | * them for submission. num_ops is the current number of |
| 2338 | * osd operations already to the object request. |
| 2339 | */ |
| 2340 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2341 | struct ceph_osd_request *osd_request, |
| 2342 | enum obj_operation_type op_type, |
| 2343 | unsigned int num_ops) |
| 2344 | { |
| 2345 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2346 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2347 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2348 | u64 offset = obj_request->offset; |
| 2349 | u64 length = obj_request->length; |
| 2350 | u64 img_end; |
| 2351 | u16 opcode; |
| 2352 | |
| 2353 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2354 | if (!offset && length == object_size && |
| 2355 | (!img_request_layered_test(img_request) || |
| 2356 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2357 | opcode = CEPH_OSD_OP_DELETE; |
| 2358 | } else if ((offset + length == object_size)) { |
| 2359 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2360 | } else { |
| 2361 | down_read(&rbd_dev->header_rwsem); |
| 2362 | img_end = rbd_dev->header.image_size; |
| 2363 | up_read(&rbd_dev->header_rwsem); |
| 2364 | |
| 2365 | if (obj_request->img_offset + length == img_end) |
| 2366 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2367 | else |
| 2368 | opcode = CEPH_OSD_OP_ZERO; |
| 2369 | } |
| 2370 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2371 | if (!offset && length == object_size) |
| 2372 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2373 | else |
| 2374 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2375 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2376 | object_size, object_size); |
| 2377 | num_ops++; |
| 2378 | } else { |
| 2379 | opcode = CEPH_OSD_OP_READ; |
| 2380 | } |
| 2381 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2382 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2383 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2384 | else |
| 2385 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2386 | offset, length, 0, 0); |
| 2387 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2388 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2389 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2390 | &obj_request->bio_pos, length); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2391 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2392 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2393 | obj_request->pages, length, |
| 2394 | offset & ~PAGE_MASK, false, false); |
| 2395 | |
| 2396 | /* Discards are also writes */ |
| 2397 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2398 | rbd_osd_req_format_write(obj_request); |
| 2399 | else |
| 2400 | rbd_osd_req_format_read(obj_request); |
| 2401 | } |
| 2402 | |
| 2403 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2404 | * Split up an image request into one or more object requests, each |
| 2405 | * to a different object. The "type" parameter indicates whether |
| 2406 | * "data_desc" is the pointer to the head of a list of bio |
| 2407 | * structures, or the base of a page array. In either case this |
| 2408 | * function assumes data_desc describes memory sufficient to hold |
| 2409 | * all data described by the image request. |
| 2410 | */ |
| 2411 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2412 | enum obj_request_type type, |
| 2413 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2414 | { |
| 2415 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2416 | struct rbd_obj_request *obj_request = NULL; |
| 2417 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2418 | struct ceph_bio_iter bio_it; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2419 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2420 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2421 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2422 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2423 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2424 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2425 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2426 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2427 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2428 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2429 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2430 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2431 | |
| 2432 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2433 | bio_it = *(struct ceph_bio_iter *)data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2434 | rbd_assert(img_offset == |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2435 | bio_it.iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2436 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2437 | pages = data_desc; |
| 2438 | } |
| 2439 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2440 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2441 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2442 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2443 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2444 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2445 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2446 | obj_request = rbd_obj_request_create(type); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2447 | if (!obj_request) |
| 2448 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2449 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2450 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2451 | obj_request->offset = offset; |
| 2452 | obj_request->length = length; |
| 2453 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2454 | /* |
| 2455 | * set obj_request->img_request before creating the |
| 2456 | * osd_request so that it gets the right snapc |
| 2457 | */ |
| 2458 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2459 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2460 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2461 | obj_request->bio_pos = bio_it; |
| 2462 | ceph_bio_iter_advance(&bio_it, length); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2463 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2464 | unsigned int page_count; |
| 2465 | |
| 2466 | obj_request->pages = pages; |
| 2467 | page_count = (u32)calc_pages_for(offset, length); |
| 2468 | obj_request->page_count = page_count; |
| 2469 | if ((offset + length) & ~PAGE_MASK) |
| 2470 | page_count--; /* more on last page */ |
| 2471 | pages += page_count; |
| 2472 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2473 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2474 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2475 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2476 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2477 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2478 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2479 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2480 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2481 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2482 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2483 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2484 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2485 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2486 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2487 | resid -= length; |
| 2488 | } |
| 2489 | |
| 2490 | return 0; |
| 2491 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2492 | out_unwind: |
| 2493 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2494 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2495 | |
| 2496 | return -ENOMEM; |
| 2497 | } |
| 2498 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2499 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2500 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2501 | { |
| 2502 | struct rbd_img_request *img_request; |
| 2503 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2504 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2505 | u32 page_count; |
| 2506 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2507 | dout("%s: obj %p\n", __func__, obj_request); |
| 2508 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2509 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2510 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2511 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2512 | img_request = obj_request->img_request; |
| 2513 | rbd_assert(img_request); |
| 2514 | |
| 2515 | rbd_dev = img_request->rbd_dev; |
| 2516 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2517 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2518 | pages = obj_request->copyup_pages; |
| 2519 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2520 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2521 | page_count = obj_request->copyup_page_count; |
| 2522 | rbd_assert(page_count); |
| 2523 | obj_request->copyup_page_count = 0; |
| 2524 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2525 | |
| 2526 | /* |
| 2527 | * We want the transfer count to reflect the size of the |
| 2528 | * original write request. There is no such thing as a |
| 2529 | * successful short write, so if the request was successful |
| 2530 | * we can just set it to the originally-requested length. |
| 2531 | */ |
| 2532 | if (!obj_request->result) |
| 2533 | obj_request->xferred = obj_request->length; |
| 2534 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2535 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2539 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2540 | { |
| 2541 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2542 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2543 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2544 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2545 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2546 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2547 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2548 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2549 | |
| 2550 | rbd_assert(img_request_child_test(img_request)); |
| 2551 | |
| 2552 | /* First get what we need from the image request */ |
| 2553 | |
| 2554 | pages = img_request->copyup_pages; |
| 2555 | rbd_assert(pages != NULL); |
| 2556 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2557 | page_count = img_request->copyup_page_count; |
| 2558 | rbd_assert(page_count); |
| 2559 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2560 | |
| 2561 | orig_request = img_request->obj_request; |
| 2562 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2563 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2564 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2565 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2566 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2567 | rbd_img_request_put(img_request); |
| 2568 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2569 | rbd_assert(orig_request->img_request); |
| 2570 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2571 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2572 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2573 | /* |
| 2574 | * If the overlap has become 0 (most likely because the |
| 2575 | * image has been flattened) we need to free the pages |
| 2576 | * and re-submit the original write request. |
| 2577 | */ |
| 2578 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2579 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2580 | rbd_obj_request_submit(orig_request); |
| 2581 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2585 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2586 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2587 | /* |
| 2588 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2589 | * 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] | 2590 | * request. Allocate the new copyup osd request for the |
| 2591 | * original request, and release the old one. |
| 2592 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2593 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2594 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2595 | if (!osd_req) |
| 2596 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2597 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2598 | orig_request->osd_req = osd_req; |
| 2599 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2600 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2601 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2602 | /* Initialize the copyup op */ |
| 2603 | |
| 2604 | 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] | 2605 | 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] | 2606 | false, false); |
| 2607 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2608 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2609 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2610 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2611 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2612 | |
| 2613 | /* All set, send it off. */ |
| 2614 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2615 | rbd_obj_request_submit(orig_request); |
| 2616 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2617 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2618 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2619 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2620 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /* |
| 2624 | * Read from the parent image the range of data that covers the |
| 2625 | * entire target of the given object request. This is used for |
| 2626 | * satisfying a layered image write request when the target of an |
| 2627 | * object request from the image request does not exist. |
| 2628 | * |
| 2629 | * A page array big enough to hold the returned data is allocated |
| 2630 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2631 | * When the read completes, this page array will be transferred to |
| 2632 | * the original object request for the copyup operation. |
| 2633 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2634 | * If an error occurs, it is recorded as the result of the original |
| 2635 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2636 | */ |
| 2637 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2638 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2639 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2640 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2641 | u64 img_offset; |
| 2642 | u64 length; |
| 2643 | struct page **pages = NULL; |
| 2644 | u32 page_count; |
| 2645 | int result; |
| 2646 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2647 | rbd_assert(rbd_dev->parent != NULL); |
| 2648 | |
| 2649 | /* |
| 2650 | * Determine the byte range covered by the object in the |
| 2651 | * child image to which the original request was to be sent. |
| 2652 | */ |
| 2653 | img_offset = obj_request->img_offset - obj_request->offset; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2654 | length = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2655 | |
| 2656 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2657 | * There is no defined parent data beyond the parent |
| 2658 | * overlap, so limit what we read at that boundary if |
| 2659 | * necessary. |
| 2660 | */ |
| 2661 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2662 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2663 | length = rbd_dev->parent_overlap - img_offset; |
| 2664 | } |
| 2665 | |
| 2666 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2667 | * Allocate a page array big enough to receive the data read |
| 2668 | * from the parent. |
| 2669 | */ |
| 2670 | page_count = (u32)calc_pages_for(0, length); |
Ilya Dryomov | 1e37f2f | 2017-11-06 11:33:36 +0100 | [diff] [blame] | 2671 | pages = ceph_alloc_page_vector(page_count, GFP_NOIO); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2672 | if (IS_ERR(pages)) { |
| 2673 | result = PTR_ERR(pages); |
| 2674 | pages = NULL; |
| 2675 | goto out_err; |
| 2676 | } |
| 2677 | |
| 2678 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2679 | parent_request = rbd_parent_request_create(obj_request, |
| 2680 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2681 | if (!parent_request) |
| 2682 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2683 | |
| 2684 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2685 | if (result) |
| 2686 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2687 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2688 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2689 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2690 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2691 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2692 | result = rbd_img_request_submit(parent_request); |
| 2693 | if (!result) |
| 2694 | return 0; |
| 2695 | |
| 2696 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2697 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2698 | out_err: |
| 2699 | if (pages) |
| 2700 | ceph_release_page_vector(pages, page_count); |
| 2701 | if (parent_request) |
| 2702 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2703 | return result; |
| 2704 | } |
| 2705 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2706 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2707 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2708 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2709 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2710 | int result; |
| 2711 | |
| 2712 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2713 | |
| 2714 | /* |
| 2715 | * All we need from the object request is the original |
| 2716 | * request and the result of the STAT op. Grab those, then |
| 2717 | * we're done with the request. |
| 2718 | */ |
| 2719 | orig_request = obj_request->obj_request; |
| 2720 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2721 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2722 | rbd_assert(orig_request); |
| 2723 | rbd_assert(orig_request->img_request); |
| 2724 | |
| 2725 | result = obj_request->result; |
| 2726 | obj_request->result = 0; |
| 2727 | |
| 2728 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2729 | obj_request, orig_request, result, |
| 2730 | obj_request->xferred, obj_request->length); |
| 2731 | rbd_obj_request_put(obj_request); |
| 2732 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2733 | /* |
| 2734 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2735 | * image has been flattened) we need to re-submit the |
| 2736 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2737 | */ |
| 2738 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2739 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2740 | rbd_obj_request_submit(orig_request); |
| 2741 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2742 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2743 | |
| 2744 | /* |
| 2745 | * Our only purpose here is to determine whether the object |
| 2746 | * exists, and we don't want to treat the non-existence as |
| 2747 | * an error. If something else comes back, transfer the |
| 2748 | * error to the original request and complete it now. |
| 2749 | */ |
| 2750 | if (!result) { |
| 2751 | obj_request_existence_set(orig_request, true); |
| 2752 | } else if (result == -ENOENT) { |
| 2753 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2754 | } else { |
| 2755 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * Resubmit the original request now that we have recorded |
| 2760 | * whether the target object exists. |
| 2761 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2762 | result = rbd_img_obj_request_submit(orig_request); |
| 2763 | if (result) |
| 2764 | goto fail_orig_request; |
| 2765 | |
| 2766 | return; |
| 2767 | |
| 2768 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2769 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2773 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2774 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2775 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2776 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2777 | u32 page_count; |
| 2778 | size_t size; |
| 2779 | int ret; |
| 2780 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2781 | stat_request = rbd_obj_request_create(OBJ_REQUEST_PAGES); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2782 | if (!stat_request) |
| 2783 | return -ENOMEM; |
| 2784 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2785 | stat_request->object_no = obj_request->object_no; |
| 2786 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2787 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2788 | stat_request); |
| 2789 | if (!stat_request->osd_req) { |
| 2790 | ret = -ENOMEM; |
| 2791 | goto fail_stat_request; |
| 2792 | } |
| 2793 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2794 | /* |
| 2795 | * The response data for a STAT call consists of: |
| 2796 | * le64 length; |
| 2797 | * struct { |
| 2798 | * le32 tv_sec; |
| 2799 | * le32 tv_nsec; |
| 2800 | * } mtime; |
| 2801 | */ |
| 2802 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2803 | page_count = (u32)calc_pages_for(0, size); |
Ilya Dryomov | 1e37f2f | 2017-11-06 11:33:36 +0100 | [diff] [blame] | 2804 | pages = ceph_alloc_page_vector(page_count, GFP_NOIO); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2805 | if (IS_ERR(pages)) { |
| 2806 | ret = PTR_ERR(pages); |
| 2807 | goto fail_stat_request; |
| 2808 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2809 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2810 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2811 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2812 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2813 | |
| 2814 | rbd_obj_request_get(obj_request); |
| 2815 | stat_request->obj_request = obj_request; |
| 2816 | stat_request->pages = pages; |
| 2817 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2818 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2819 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2820 | rbd_obj_request_submit(stat_request); |
| 2821 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2822 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2823 | fail_stat_request: |
| 2824 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2825 | return ret; |
| 2826 | } |
| 2827 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2828 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2829 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2830 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2831 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2832 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2833 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2834 | if (!img_request_write_test(img_request) && |
| 2835 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2836 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2837 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2838 | /* Non-layered writes */ |
| 2839 | if (!img_request_layered_test(img_request)) |
| 2840 | return true; |
| 2841 | |
| 2842 | /* |
| 2843 | * Layered writes outside of the parent overlap range don't |
| 2844 | * share any data with the parent. |
| 2845 | */ |
| 2846 | if (!obj_request_overlaps_parent(obj_request)) |
| 2847 | return true; |
| 2848 | |
| 2849 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2850 | * Entire-object layered writes - we will overwrite whatever |
| 2851 | * parent data there is anyway. |
| 2852 | */ |
| 2853 | if (!obj_request->offset && |
| 2854 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2855 | return true; |
| 2856 | |
| 2857 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2858 | * If the object is known to already exist, its parent data has |
| 2859 | * already been copied. |
| 2860 | */ |
| 2861 | if (obj_request_known_test(obj_request) && |
| 2862 | obj_request_exists_test(obj_request)) |
| 2863 | return true; |
| 2864 | |
| 2865 | return false; |
| 2866 | } |
| 2867 | |
| 2868 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2869 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2870 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2871 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2872 | rbd_assert(obj_request->img_request); |
| 2873 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2874 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2875 | rbd_obj_request_submit(obj_request); |
| 2876 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2880 | * It's a layered write. The target object might exist but |
| 2881 | * we may not know that yet. If we know it doesn't exist, |
| 2882 | * start by reading the data for the full target object from |
| 2883 | * the parent so we can use it for a copyup to the target. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2884 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2885 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2886 | return rbd_img_obj_parent_read_full(obj_request); |
| 2887 | |
| 2888 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2889 | |
| 2890 | return rbd_img_obj_exists_submit(obj_request); |
| 2891 | } |
| 2892 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2893 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 2894 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2895 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 2896 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2897 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2898 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2899 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2900 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2901 | rbd_img_request_get(img_request); |
| 2902 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2903 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2904 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2905 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2906 | } |
| 2907 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2908 | out_put_ireq: |
| 2909 | rbd_img_request_put(img_request); |
| 2910 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2911 | } |
| 2912 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2913 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 2914 | { |
| 2915 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2916 | struct rbd_device *rbd_dev; |
| 2917 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2918 | u64 img_xferred; |
| 2919 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2920 | |
| 2921 | rbd_assert(img_request_child_test(img_request)); |
| 2922 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2923 | /* First get what we need from the image request and release it */ |
| 2924 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2925 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2926 | img_xferred = img_request->xferred; |
| 2927 | img_result = img_request->result; |
| 2928 | rbd_img_request_put(img_request); |
| 2929 | |
| 2930 | /* |
| 2931 | * If the overlap has become 0 (most likely because the |
| 2932 | * image has been flattened) we need to re-submit the |
| 2933 | * original request. |
| 2934 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2935 | rbd_assert(obj_request); |
| 2936 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2937 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2938 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2939 | rbd_obj_request_submit(obj_request); |
| 2940 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2944 | if (obj_request->result) |
| 2945 | goto out; |
| 2946 | |
| 2947 | /* |
| 2948 | * We need to zero anything beyond the parent overlap |
| 2949 | * boundary. Since rbd_img_obj_request_read_callback() |
| 2950 | * will zero anything beyond the end of a short read, an |
| 2951 | * easy way to do this is to pretend the data from the |
| 2952 | * parent came up short--ending at the overlap boundary. |
| 2953 | */ |
| 2954 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 2955 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2956 | if (obj_end > rbd_dev->parent_overlap) { |
| 2957 | u64 xferred = 0; |
| 2958 | |
| 2959 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 2960 | xferred = rbd_dev->parent_overlap - |
| 2961 | obj_request->img_offset; |
| 2962 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2963 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2964 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2965 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2966 | } |
| 2967 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2968 | rbd_img_obj_request_read_callback(obj_request); |
| 2969 | rbd_obj_request_complete(obj_request); |
| 2970 | } |
| 2971 | |
| 2972 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 2973 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2974 | struct rbd_img_request *img_request; |
| 2975 | int result; |
| 2976 | |
| 2977 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2978 | rbd_assert(obj_request->img_request != NULL); |
| 2979 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2980 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2981 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2982 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2983 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2984 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2985 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2986 | result = -ENOMEM; |
| 2987 | if (!img_request) |
| 2988 | goto out_err; |
| 2989 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2990 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2991 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 2992 | &obj_request->bio_pos); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2993 | else |
| 2994 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 2995 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2996 | if (result) |
| 2997 | goto out_err; |
| 2998 | |
| 2999 | img_request->callback = rbd_img_parent_read_callback; |
| 3000 | result = rbd_img_request_submit(img_request); |
| 3001 | if (result) |
| 3002 | goto out_err; |
| 3003 | |
| 3004 | return; |
| 3005 | out_err: |
| 3006 | if (img_request) |
| 3007 | rbd_img_request_put(img_request); |
| 3008 | obj_request->result = result; |
| 3009 | obj_request->xferred = 0; |
| 3010 | obj_request_done_set(obj_request); |
| 3011 | } |
| 3012 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3013 | static const struct rbd_client_id rbd_empty_cid; |
| 3014 | |
| 3015 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3016 | const struct rbd_client_id *rhs) |
| 3017 | { |
| 3018 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3019 | } |
| 3020 | |
| 3021 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3022 | { |
| 3023 | struct rbd_client_id cid; |
| 3024 | |
| 3025 | mutex_lock(&rbd_dev->watch_mutex); |
| 3026 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3027 | cid.handle = rbd_dev->watch_cookie; |
| 3028 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3029 | return cid; |
| 3030 | } |
| 3031 | |
| 3032 | /* |
| 3033 | * lock_rwsem must be held for write |
| 3034 | */ |
| 3035 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3036 | const struct rbd_client_id *cid) |
| 3037 | { |
| 3038 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3039 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3040 | cid->gid, cid->handle); |
| 3041 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3042 | } |
| 3043 | |
| 3044 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3045 | { |
| 3046 | mutex_lock(&rbd_dev->watch_mutex); |
| 3047 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3048 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3049 | } |
| 3050 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3051 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 3052 | { |
| 3053 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3054 | |
| 3055 | strcpy(rbd_dev->lock_cookie, cookie); |
| 3056 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3057 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3058 | } |
| 3059 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3060 | /* |
| 3061 | * lock_rwsem must be held for write |
| 3062 | */ |
| 3063 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3064 | { |
| 3065 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3066 | char cookie[32]; |
| 3067 | int ret; |
| 3068 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3069 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 3070 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3071 | |
| 3072 | format_lock_cookie(rbd_dev, cookie); |
| 3073 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3074 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3075 | RBD_LOCK_TAG, "", 0); |
| 3076 | if (ret) |
| 3077 | return ret; |
| 3078 | |
| 3079 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3080 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3081 | return 0; |
| 3082 | } |
| 3083 | |
| 3084 | /* |
| 3085 | * lock_rwsem must be held for write |
| 3086 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3087 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3088 | { |
| 3089 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3090 | int ret; |
| 3091 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3092 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 3093 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3094 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3095 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3096 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3097 | if (ret && ret != -ENOENT) |
| 3098 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3099 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3100 | /* treat errors as the image is unlocked */ |
| 3101 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3102 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3103 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3104 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3108 | enum rbd_notify_op notify_op, |
| 3109 | struct page ***preply_pages, |
| 3110 | size_t *preply_len) |
| 3111 | { |
| 3112 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3113 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3114 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3115 | char buf[buf_size]; |
| 3116 | void *p = buf; |
| 3117 | |
| 3118 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3119 | |
| 3120 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3121 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3122 | ceph_encode_32(&p, notify_op); |
| 3123 | ceph_encode_64(&p, cid.gid); |
| 3124 | ceph_encode_64(&p, cid.handle); |
| 3125 | |
| 3126 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3127 | &rbd_dev->header_oloc, buf, buf_size, |
| 3128 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3129 | } |
| 3130 | |
| 3131 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3132 | enum rbd_notify_op notify_op) |
| 3133 | { |
| 3134 | struct page **reply_pages; |
| 3135 | size_t reply_len; |
| 3136 | |
| 3137 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3138 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3139 | } |
| 3140 | |
| 3141 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3142 | { |
| 3143 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3144 | acquired_lock_work); |
| 3145 | |
| 3146 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3147 | } |
| 3148 | |
| 3149 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3150 | { |
| 3151 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3152 | released_lock_work); |
| 3153 | |
| 3154 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3155 | } |
| 3156 | |
| 3157 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3158 | { |
| 3159 | struct page **reply_pages; |
| 3160 | size_t reply_len; |
| 3161 | bool lock_owner_responded = false; |
| 3162 | int ret; |
| 3163 | |
| 3164 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3165 | |
| 3166 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3167 | &reply_pages, &reply_len); |
| 3168 | if (ret && ret != -ETIMEDOUT) { |
| 3169 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3170 | goto out; |
| 3171 | } |
| 3172 | |
| 3173 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3174 | void *p = page_address(reply_pages[0]); |
| 3175 | void *const end = p + reply_len; |
| 3176 | u32 n; |
| 3177 | |
| 3178 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3179 | while (n--) { |
| 3180 | u8 struct_v; |
| 3181 | u32 len; |
| 3182 | |
| 3183 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3184 | p += 8 + 8; /* skip gid and cookie */ |
| 3185 | |
| 3186 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3187 | if (!len) |
| 3188 | continue; |
| 3189 | |
| 3190 | if (lock_owner_responded) { |
| 3191 | rbd_warn(rbd_dev, |
| 3192 | "duplicate lock owners detected"); |
| 3193 | ret = -EIO; |
| 3194 | goto out; |
| 3195 | } |
| 3196 | |
| 3197 | lock_owner_responded = true; |
| 3198 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3199 | &struct_v, &len); |
| 3200 | if (ret) { |
| 3201 | rbd_warn(rbd_dev, |
| 3202 | "failed to decode ResponseMessage: %d", |
| 3203 | ret); |
| 3204 | goto e_inval; |
| 3205 | } |
| 3206 | |
| 3207 | ret = ceph_decode_32(&p); |
| 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | if (!lock_owner_responded) { |
| 3212 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3213 | ret = -ETIMEDOUT; |
| 3214 | } |
| 3215 | |
| 3216 | out: |
| 3217 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3218 | return ret; |
| 3219 | |
| 3220 | e_inval: |
| 3221 | ret = -EINVAL; |
| 3222 | goto out; |
| 3223 | } |
| 3224 | |
| 3225 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3226 | { |
| 3227 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3228 | |
| 3229 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3230 | if (wake_all) |
| 3231 | wake_up_all(&rbd_dev->lock_waitq); |
| 3232 | else |
| 3233 | wake_up(&rbd_dev->lock_waitq); |
| 3234 | } |
| 3235 | |
| 3236 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3237 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3238 | { |
| 3239 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3240 | u8 lock_type; |
| 3241 | char *lock_tag; |
| 3242 | int ret; |
| 3243 | |
| 3244 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3245 | |
| 3246 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3247 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3248 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3249 | if (ret) |
| 3250 | return ret; |
| 3251 | |
| 3252 | if (*num_lockers == 0) { |
| 3253 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3254 | goto out; |
| 3255 | } |
| 3256 | |
| 3257 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3258 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3259 | lock_tag); |
| 3260 | ret = -EBUSY; |
| 3261 | goto out; |
| 3262 | } |
| 3263 | |
| 3264 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3265 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3266 | ret = -EBUSY; |
| 3267 | goto out; |
| 3268 | } |
| 3269 | |
| 3270 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3271 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3272 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3273 | (*lockers)[0].id.cookie); |
| 3274 | ret = -EBUSY; |
| 3275 | goto out; |
| 3276 | } |
| 3277 | |
| 3278 | out: |
| 3279 | kfree(lock_tag); |
| 3280 | return ret; |
| 3281 | } |
| 3282 | |
| 3283 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3284 | const struct ceph_locker *locker) |
| 3285 | { |
| 3286 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3287 | struct ceph_watch_item *watchers; |
| 3288 | u32 num_watchers; |
| 3289 | u64 cookie; |
| 3290 | int i; |
| 3291 | int ret; |
| 3292 | |
| 3293 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3294 | &rbd_dev->header_oloc, &watchers, |
| 3295 | &num_watchers); |
| 3296 | if (ret) |
| 3297 | return ret; |
| 3298 | |
| 3299 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3300 | for (i = 0; i < num_watchers; i++) { |
| 3301 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3302 | sizeof(locker->info.addr)) && |
| 3303 | watchers[i].cookie == cookie) { |
| 3304 | struct rbd_client_id cid = { |
| 3305 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3306 | .handle = cookie, |
| 3307 | }; |
| 3308 | |
| 3309 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3310 | rbd_dev, cid.gid, cid.handle); |
| 3311 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3312 | ret = 1; |
| 3313 | goto out; |
| 3314 | } |
| 3315 | } |
| 3316 | |
| 3317 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3318 | ret = 0; |
| 3319 | out: |
| 3320 | kfree(watchers); |
| 3321 | return ret; |
| 3322 | } |
| 3323 | |
| 3324 | /* |
| 3325 | * lock_rwsem must be held for write |
| 3326 | */ |
| 3327 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3328 | { |
| 3329 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3330 | struct ceph_locker *lockers; |
| 3331 | u32 num_lockers; |
| 3332 | int ret; |
| 3333 | |
| 3334 | for (;;) { |
| 3335 | ret = rbd_lock(rbd_dev); |
| 3336 | if (ret != -EBUSY) |
| 3337 | return ret; |
| 3338 | |
| 3339 | /* determine if the current lock holder is still alive */ |
| 3340 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3341 | if (ret) |
| 3342 | return ret; |
| 3343 | |
| 3344 | if (num_lockers == 0) |
| 3345 | goto again; |
| 3346 | |
| 3347 | ret = find_watcher(rbd_dev, lockers); |
| 3348 | if (ret) { |
| 3349 | if (ret > 0) |
| 3350 | ret = 0; /* have to request lock */ |
| 3351 | goto out; |
| 3352 | } |
| 3353 | |
| 3354 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3355 | ENTITY_NAME(lockers[0].id.name)); |
| 3356 | |
| 3357 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3358 | &lockers[0].info.addr); |
| 3359 | if (ret) { |
| 3360 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3361 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3362 | goto out; |
| 3363 | } |
| 3364 | |
| 3365 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3366 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3367 | lockers[0].id.cookie, |
| 3368 | &lockers[0].id.name); |
| 3369 | if (ret && ret != -ENOENT) |
| 3370 | goto out; |
| 3371 | |
| 3372 | again: |
| 3373 | ceph_free_lockers(lockers, num_lockers); |
| 3374 | } |
| 3375 | |
| 3376 | out: |
| 3377 | ceph_free_lockers(lockers, num_lockers); |
| 3378 | return ret; |
| 3379 | } |
| 3380 | |
| 3381 | /* |
| 3382 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3383 | */ |
| 3384 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3385 | int *pret) |
| 3386 | { |
| 3387 | enum rbd_lock_state lock_state; |
| 3388 | |
| 3389 | down_read(&rbd_dev->lock_rwsem); |
| 3390 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3391 | rbd_dev->lock_state); |
| 3392 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3393 | lock_state = rbd_dev->lock_state; |
| 3394 | up_read(&rbd_dev->lock_rwsem); |
| 3395 | return lock_state; |
| 3396 | } |
| 3397 | |
| 3398 | up_read(&rbd_dev->lock_rwsem); |
| 3399 | down_write(&rbd_dev->lock_rwsem); |
| 3400 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3401 | rbd_dev->lock_state); |
| 3402 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3403 | *pret = rbd_try_lock(rbd_dev); |
| 3404 | if (*pret) |
| 3405 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3406 | } |
| 3407 | |
| 3408 | lock_state = rbd_dev->lock_state; |
| 3409 | up_write(&rbd_dev->lock_rwsem); |
| 3410 | return lock_state; |
| 3411 | } |
| 3412 | |
| 3413 | static void rbd_acquire_lock(struct work_struct *work) |
| 3414 | { |
| 3415 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3416 | struct rbd_device, lock_dwork); |
| 3417 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3418 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3419 | |
| 3420 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3421 | again: |
| 3422 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3423 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3424 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3425 | wake_requests(rbd_dev, true); |
| 3426 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3427 | rbd_dev, lock_state, ret); |
| 3428 | return; |
| 3429 | } |
| 3430 | |
| 3431 | ret = rbd_request_lock(rbd_dev); |
| 3432 | if (ret == -ETIMEDOUT) { |
| 3433 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3434 | } else if (ret == -EROFS) { |
| 3435 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3436 | /* |
| 3437 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3438 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3439 | * want to block. |
| 3440 | */ |
| 3441 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3442 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3443 | /* wake "rbd map --exclusive" process */ |
| 3444 | wake_requests(rbd_dev, false); |
| 3445 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3446 | } else if (ret < 0) { |
| 3447 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3448 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3449 | RBD_RETRY_DELAY); |
| 3450 | } else { |
| 3451 | /* |
| 3452 | * lock owner acked, but resend if we don't see them |
| 3453 | * release the lock |
| 3454 | */ |
| 3455 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3456 | rbd_dev); |
| 3457 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3458 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | /* |
| 3463 | * lock_rwsem must be held for write |
| 3464 | */ |
| 3465 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3466 | { |
| 3467 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3468 | rbd_dev->lock_state); |
| 3469 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3470 | return false; |
| 3471 | |
| 3472 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3473 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3474 | /* |
| 3475 | * Ensure that all in-flight IO is flushed. |
| 3476 | * |
| 3477 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3478 | * may be shared with other devices. |
| 3479 | */ |
| 3480 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3481 | up_read(&rbd_dev->lock_rwsem); |
| 3482 | |
| 3483 | down_write(&rbd_dev->lock_rwsem); |
| 3484 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3485 | rbd_dev->lock_state); |
| 3486 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3487 | return false; |
| 3488 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3489 | rbd_unlock(rbd_dev); |
| 3490 | /* |
| 3491 | * Give others a chance to grab the lock - we would re-acquire |
| 3492 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3493 | * otherwise. We need to ack our own notifications, so this |
| 3494 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3495 | * after wake_requests() in rbd_handle_released_lock(). |
| 3496 | */ |
| 3497 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3498 | return true; |
| 3499 | } |
| 3500 | |
| 3501 | static void rbd_release_lock_work(struct work_struct *work) |
| 3502 | { |
| 3503 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3504 | unlock_work); |
| 3505 | |
| 3506 | down_write(&rbd_dev->lock_rwsem); |
| 3507 | rbd_release_lock(rbd_dev); |
| 3508 | up_write(&rbd_dev->lock_rwsem); |
| 3509 | } |
| 3510 | |
| 3511 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3512 | void **p) |
| 3513 | { |
| 3514 | struct rbd_client_id cid = { 0 }; |
| 3515 | |
| 3516 | if (struct_v >= 2) { |
| 3517 | cid.gid = ceph_decode_64(p); |
| 3518 | cid.handle = ceph_decode_64(p); |
| 3519 | } |
| 3520 | |
| 3521 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3522 | cid.handle); |
| 3523 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3524 | down_write(&rbd_dev->lock_rwsem); |
| 3525 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3526 | /* |
| 3527 | * we already know that the remote client is |
| 3528 | * the owner |
| 3529 | */ |
| 3530 | up_write(&rbd_dev->lock_rwsem); |
| 3531 | return; |
| 3532 | } |
| 3533 | |
| 3534 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3535 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3536 | } else { |
| 3537 | down_read(&rbd_dev->lock_rwsem); |
| 3538 | } |
| 3539 | |
| 3540 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3541 | wake_requests(rbd_dev, false); |
| 3542 | up_read(&rbd_dev->lock_rwsem); |
| 3543 | } |
| 3544 | |
| 3545 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3546 | void **p) |
| 3547 | { |
| 3548 | struct rbd_client_id cid = { 0 }; |
| 3549 | |
| 3550 | if (struct_v >= 2) { |
| 3551 | cid.gid = ceph_decode_64(p); |
| 3552 | cid.handle = ceph_decode_64(p); |
| 3553 | } |
| 3554 | |
| 3555 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3556 | cid.handle); |
| 3557 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3558 | down_write(&rbd_dev->lock_rwsem); |
| 3559 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3560 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3561 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3562 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3563 | up_write(&rbd_dev->lock_rwsem); |
| 3564 | return; |
| 3565 | } |
| 3566 | |
| 3567 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3568 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3569 | } else { |
| 3570 | down_read(&rbd_dev->lock_rwsem); |
| 3571 | } |
| 3572 | |
| 3573 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3574 | wake_requests(rbd_dev, false); |
| 3575 | up_read(&rbd_dev->lock_rwsem); |
| 3576 | } |
| 3577 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3578 | /* |
| 3579 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3580 | * ResponseMessage is needed. |
| 3581 | */ |
| 3582 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3583 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3584 | { |
| 3585 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3586 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3587 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3588 | |
| 3589 | if (struct_v >= 2) { |
| 3590 | cid.gid = ceph_decode_64(p); |
| 3591 | cid.handle = ceph_decode_64(p); |
| 3592 | } |
| 3593 | |
| 3594 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3595 | cid.handle); |
| 3596 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3597 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3598 | |
| 3599 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3600 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3601 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3602 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3603 | goto out_unlock; |
| 3604 | |
| 3605 | /* |
| 3606 | * encode ResponseMessage(0) so the peer can detect |
| 3607 | * a missing owner |
| 3608 | */ |
| 3609 | result = 0; |
| 3610 | |
| 3611 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3612 | if (!rbd_dev->opts->exclusive) { |
| 3613 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3614 | __func__, rbd_dev); |
| 3615 | queue_work(rbd_dev->task_wq, |
| 3616 | &rbd_dev->unlock_work); |
| 3617 | } else { |
| 3618 | /* refuse to release the lock */ |
| 3619 | result = -EROFS; |
| 3620 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3621 | } |
| 3622 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3623 | |
| 3624 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3625 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3626 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3630 | u64 notify_id, u64 cookie, s32 *result) |
| 3631 | { |
| 3632 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3633 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3634 | char buf[buf_size]; |
| 3635 | int ret; |
| 3636 | |
| 3637 | if (result) { |
| 3638 | void *p = buf; |
| 3639 | |
| 3640 | /* encode ResponseMessage */ |
| 3641 | ceph_start_encoding(&p, 1, 1, |
| 3642 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3643 | ceph_encode_32(&p, *result); |
| 3644 | } else { |
| 3645 | buf_size = 0; |
| 3646 | } |
| 3647 | |
| 3648 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3649 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3650 | buf, buf_size); |
| 3651 | if (ret) |
| 3652 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3653 | } |
| 3654 | |
| 3655 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3656 | u64 cookie) |
| 3657 | { |
| 3658 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3659 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3660 | } |
| 3661 | |
| 3662 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3663 | u64 notify_id, u64 cookie, s32 result) |
| 3664 | { |
| 3665 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3666 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3667 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3668 | |
| 3669 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3670 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3671 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3672 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3673 | void *p = data; |
| 3674 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3675 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3676 | u32 len; |
| 3677 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3678 | int ret; |
| 3679 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3680 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3681 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3682 | if (data_len) { |
| 3683 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3684 | &struct_v, &len); |
| 3685 | if (ret) { |
| 3686 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3687 | ret); |
| 3688 | return; |
| 3689 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3690 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3691 | notify_op = ceph_decode_32(&p); |
| 3692 | } else { |
| 3693 | /* legacy notification for header updates */ |
| 3694 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3695 | len = 0; |
| 3696 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3697 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3698 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3699 | switch (notify_op) { |
| 3700 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3701 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3702 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3703 | break; |
| 3704 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3705 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3706 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3707 | break; |
| 3708 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3709 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3710 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3711 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3712 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3713 | else |
| 3714 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3715 | break; |
| 3716 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3717 | ret = rbd_dev_refresh(rbd_dev); |
| 3718 | if (ret) |
| 3719 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3720 | |
| 3721 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3722 | break; |
| 3723 | default: |
| 3724 | if (rbd_is_lock_owner(rbd_dev)) |
| 3725 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3726 | cookie, -EOPNOTSUPP); |
| 3727 | else |
| 3728 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3729 | break; |
| 3730 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3731 | } |
| 3732 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3733 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3734 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3735 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3736 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3737 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3738 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3739 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3740 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3741 | down_write(&rbd_dev->lock_rwsem); |
| 3742 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3743 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3744 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3745 | mutex_lock(&rbd_dev->watch_mutex); |
| 3746 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3747 | __rbd_unregister_watch(rbd_dev); |
| 3748 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3749 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3750 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3751 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3752 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3756 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3757 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3758 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3759 | { |
| 3760 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3761 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3762 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3763 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3764 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3765 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3766 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3767 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3768 | rbd_watch_errcb, rbd_dev); |
| 3769 | if (IS_ERR(handle)) |
| 3770 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3771 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3772 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3773 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3774 | } |
| 3775 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3776 | /* |
| 3777 | * watch_mutex must be locked |
| 3778 | */ |
| 3779 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3780 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3781 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3782 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3783 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3784 | rbd_assert(rbd_dev->watch_handle); |
| 3785 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3786 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3787 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3788 | if (ret) |
| 3789 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3790 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3791 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3792 | } |
| 3793 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3794 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3795 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3796 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3797 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3798 | mutex_lock(&rbd_dev->watch_mutex); |
| 3799 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3800 | ret = __rbd_register_watch(rbd_dev); |
| 3801 | if (ret) |
| 3802 | goto out; |
| 3803 | |
| 3804 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3805 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3806 | |
| 3807 | out: |
| 3808 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3809 | return ret; |
| 3810 | } |
| 3811 | |
| 3812 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3813 | { |
| 3814 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3815 | |
| 3816 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3817 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3818 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3819 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3820 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3824 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3825 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3826 | cancel_tasks_sync(rbd_dev); |
| 3827 | |
| 3828 | mutex_lock(&rbd_dev->watch_mutex); |
| 3829 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3830 | __rbd_unregister_watch(rbd_dev); |
| 3831 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3832 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3833 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3834 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3835 | } |
| 3836 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3837 | /* |
| 3838 | * lock_rwsem must be held for write |
| 3839 | */ |
| 3840 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3841 | { |
| 3842 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3843 | char cookie[32]; |
| 3844 | int ret; |
| 3845 | |
| 3846 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3847 | |
| 3848 | format_lock_cookie(rbd_dev, cookie); |
| 3849 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3850 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3851 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3852 | RBD_LOCK_TAG, cookie); |
| 3853 | if (ret) { |
| 3854 | if (ret != -EOPNOTSUPP) |
| 3855 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3856 | ret); |
| 3857 | |
| 3858 | /* |
| 3859 | * Lock cookie cannot be updated on older OSDs, so do |
| 3860 | * a manual release and queue an acquire. |
| 3861 | */ |
| 3862 | if (rbd_release_lock(rbd_dev)) |
| 3863 | queue_delayed_work(rbd_dev->task_wq, |
| 3864 | &rbd_dev->lock_dwork, 0); |
| 3865 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3866 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3870 | static void rbd_reregister_watch(struct work_struct *work) |
| 3871 | { |
| 3872 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3873 | struct rbd_device, watch_dwork); |
| 3874 | int ret; |
| 3875 | |
| 3876 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3877 | |
| 3878 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3879 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3880 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3881 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3882 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3883 | |
| 3884 | ret = __rbd_register_watch(rbd_dev); |
| 3885 | if (ret) { |
| 3886 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3887 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3888 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3889 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3890 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3891 | queue_delayed_work(rbd_dev->task_wq, |
| 3892 | &rbd_dev->watch_dwork, |
| 3893 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3894 | } |
| 3895 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3896 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3897 | } |
| 3898 | |
| 3899 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3900 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3901 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3902 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3903 | down_write(&rbd_dev->lock_rwsem); |
| 3904 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3905 | rbd_reacquire_lock(rbd_dev); |
| 3906 | up_write(&rbd_dev->lock_rwsem); |
| 3907 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3908 | ret = rbd_dev_refresh(rbd_dev); |
| 3909 | if (ret) |
| 3910 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3911 | } |
| 3912 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3913 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3914 | * Synchronous osd object method call. Returns the number of bytes |
| 3915 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3916 | */ |
| 3917 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3918 | struct ceph_object_id *oid, |
| 3919 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3920 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3921 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3922 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3923 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3924 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3925 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3926 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3927 | struct page *req_page = NULL; |
| 3928 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3929 | int ret; |
| 3930 | |
| 3931 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3932 | * Method calls are ultimately read operations. The result |
| 3933 | * should placed into the inbound buffer provided. They |
| 3934 | * also supply outbound data--parameters for the object |
| 3935 | * method. Currently if this is present it will be a |
| 3936 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3937 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3938 | if (outbound) { |
| 3939 | if (outbound_size > PAGE_SIZE) |
| 3940 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3941 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3942 | req_page = alloc_page(GFP_KERNEL); |
| 3943 | if (!req_page) |
| 3944 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3945 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3946 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3947 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3948 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3949 | reply_page = alloc_page(GFP_KERNEL); |
| 3950 | if (!reply_page) { |
| 3951 | if (req_page) |
| 3952 | __free_page(req_page); |
| 3953 | return -ENOMEM; |
| 3954 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3955 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3956 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3957 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3958 | reply_page, &inbound_size); |
| 3959 | if (!ret) { |
| 3960 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3961 | ret = inbound_size; |
| 3962 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3963 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3964 | if (req_page) |
| 3965 | __free_page(req_page); |
| 3966 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3967 | return ret; |
| 3968 | } |
| 3969 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3970 | /* |
| 3971 | * lock_rwsem must be held for read |
| 3972 | */ |
| 3973 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3974 | { |
| 3975 | DEFINE_WAIT(wait); |
| 3976 | |
| 3977 | do { |
| 3978 | /* |
| 3979 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3980 | * and cancel_delayed_work() in wake_requests(). |
| 3981 | */ |
| 3982 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3983 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3984 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3985 | TASK_UNINTERRUPTIBLE); |
| 3986 | up_read(&rbd_dev->lock_rwsem); |
| 3987 | schedule(); |
| 3988 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3989 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3990 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3991 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3992 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3993 | } |
| 3994 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3995 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3996 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3997 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3998 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3999 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4000 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4001 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4002 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4003 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4004 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4005 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4006 | int result; |
| 4007 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4008 | switch (req_op(rq)) { |
| 4009 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 4010 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4011 | op_type = OBJ_OP_DISCARD; |
| 4012 | break; |
| 4013 | case REQ_OP_WRITE: |
| 4014 | op_type = OBJ_OP_WRITE; |
| 4015 | break; |
| 4016 | case REQ_OP_READ: |
| 4017 | op_type = OBJ_OP_READ; |
| 4018 | break; |
| 4019 | default: |
| 4020 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4021 | result = -EIO; |
| 4022 | goto err; |
| 4023 | } |
| 4024 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4025 | /* Ignore/skip any zero-length requests */ |
| 4026 | |
| 4027 | if (!length) { |
| 4028 | dout("%s: zero-length request\n", __func__); |
| 4029 | result = 0; |
| 4030 | goto err_rq; |
| 4031 | } |
| 4032 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 4033 | rbd_assert(op_type == OBJ_OP_READ || |
| 4034 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4035 | |
| 4036 | /* |
| 4037 | * Quit early if the mapped snapshot no longer exists. It's |
| 4038 | * still possible the snapshot will have disappeared by the |
| 4039 | * time our request arrives at the osd, but there's no sense in |
| 4040 | * sending it if we already know. |
| 4041 | */ |
| 4042 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4043 | dout("request for non-existent snapshot"); |
| 4044 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4045 | result = -ENXIO; |
| 4046 | goto err_rq; |
| 4047 | } |
| 4048 | |
| 4049 | if (offset && length > U64_MAX - offset + 1) { |
| 4050 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4051 | length); |
| 4052 | result = -EINVAL; |
| 4053 | goto err_rq; /* Shouldn't happen */ |
| 4054 | } |
| 4055 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4056 | blk_mq_start_request(rq); |
| 4057 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4058 | down_read(&rbd_dev->header_rwsem); |
| 4059 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4060 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4061 | snapc = rbd_dev->header.snapc; |
| 4062 | ceph_get_snap_context(snapc); |
| 4063 | } |
| 4064 | up_read(&rbd_dev->header_rwsem); |
| 4065 | |
| 4066 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4067 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4068 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4069 | result = -EIO; |
| 4070 | goto err_rq; |
| 4071 | } |
| 4072 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4073 | must_be_locked = |
| 4074 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 4075 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4076 | if (must_be_locked) { |
| 4077 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4078 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4079 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4080 | if (rbd_dev->opts->exclusive) { |
| 4081 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 4082 | result = -EROFS; |
| 4083 | goto err_unlock; |
| 4084 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4085 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4086 | } |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4087 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4088 | result = -EBLACKLISTED; |
| 4089 | goto err_unlock; |
| 4090 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4091 | } |
| 4092 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4093 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4094 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4095 | if (!img_request) { |
| 4096 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4097 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4098 | } |
| 4099 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4100 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4101 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4102 | if (op_type == OBJ_OP_DISCARD) |
| 4103 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4104 | NULL); |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 4105 | else { |
| 4106 | struct ceph_bio_iter bio_it = { .bio = rq->bio, |
| 4107 | .iter = rq->bio->bi_iter }; |
| 4108 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4109 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame^] | 4110 | &bio_it); |
| 4111 | } |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4112 | if (result) |
| 4113 | goto err_img_request; |
| 4114 | |
| 4115 | result = rbd_img_request_submit(img_request); |
| 4116 | if (result) |
| 4117 | goto err_img_request; |
| 4118 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4119 | if (must_be_locked) |
| 4120 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4121 | return; |
| 4122 | |
| 4123 | err_img_request: |
| 4124 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4125 | err_unlock: |
| 4126 | if (must_be_locked) |
| 4127 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4128 | err_rq: |
| 4129 | if (result) |
| 4130 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4131 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4132 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4133 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 4134 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4135 | } |
| 4136 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4137 | static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4138 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4139 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4140 | struct request *rq = bd->rq; |
| 4141 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4142 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4143 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4144 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4145 | } |
| 4146 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4147 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4148 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4149 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 4150 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 4151 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4152 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4153 | } |
| 4154 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4155 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4156 | struct ceph_object_id *oid, |
| 4157 | struct ceph_object_locator *oloc, |
| 4158 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4159 | |
| 4160 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4161 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4162 | struct ceph_osd_request *req; |
| 4163 | struct page **pages; |
| 4164 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4165 | int ret; |
| 4166 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4167 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4168 | if (!req) |
| 4169 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4170 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4171 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4172 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4173 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4174 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4175 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4176 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4177 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4178 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4179 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4180 | if (IS_ERR(pages)) { |
| 4181 | ret = PTR_ERR(pages); |
| 4182 | goto out_req; |
| 4183 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4184 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4185 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4186 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4187 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4188 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4189 | ceph_osdc_start_request(osdc, req, false); |
| 4190 | ret = ceph_osdc_wait_request(osdc, req); |
| 4191 | if (ret >= 0) |
| 4192 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4193 | |
| 4194 | out_req: |
| 4195 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4196 | return ret; |
| 4197 | } |
| 4198 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4199 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4200 | * Read the complete header for the given rbd device. On successful |
| 4201 | * return, the rbd_dev->header field will contain up-to-date |
| 4202 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4203 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4204 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4205 | { |
| 4206 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4207 | u32 snap_count = 0; |
| 4208 | u64 names_size = 0; |
| 4209 | u32 want_count; |
| 4210 | int ret; |
| 4211 | |
| 4212 | /* |
| 4213 | * The complete header will include an array of its 64-bit |
| 4214 | * snapshot ids, followed by the names of those snapshots as |
| 4215 | * a contiguous block of NUL-terminated strings. Note that |
| 4216 | * the number of snapshots could change by the time we read |
| 4217 | * it in, in which case we re-read it. |
| 4218 | */ |
| 4219 | do { |
| 4220 | size_t size; |
| 4221 | |
| 4222 | kfree(ondisk); |
| 4223 | |
| 4224 | size = sizeof (*ondisk); |
| 4225 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4226 | size += names_size; |
| 4227 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4228 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4229 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4230 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4231 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4232 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4233 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4234 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4235 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4236 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4237 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4238 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4239 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4240 | } |
| 4241 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4242 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4243 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4244 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4248 | want_count = snap_count; |
| 4249 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4250 | } while (snap_count != want_count); |
| 4251 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4252 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4253 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4254 | kfree(ondisk); |
| 4255 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4256 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4259 | /* |
| 4260 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4261 | * has disappeared from the (just updated) snapshot context. |
| 4262 | */ |
| 4263 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4264 | { |
| 4265 | u64 snap_id; |
| 4266 | |
| 4267 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4268 | return; |
| 4269 | |
| 4270 | snap_id = rbd_dev->spec->snap_id; |
| 4271 | if (snap_id == CEPH_NOSNAP) |
| 4272 | return; |
| 4273 | |
| 4274 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4275 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4276 | } |
| 4277 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4278 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4279 | { |
| 4280 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4281 | |
| 4282 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4283 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4284 | * try to update its size. If REMOVING is set, updating size |
| 4285 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4286 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4287 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4288 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4289 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4290 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4291 | set_capacity(rbd_dev->disk, size); |
| 4292 | revalidate_disk(rbd_dev->disk); |
| 4293 | } |
| 4294 | } |
| 4295 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4296 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4297 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4298 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4299 | int ret; |
| 4300 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4301 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4302 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4303 | |
| 4304 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4305 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4306 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4307 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4308 | /* |
| 4309 | * If there is a parent, see if it has disappeared due to the |
| 4310 | * mapped image getting flattened. |
| 4311 | */ |
| 4312 | if (rbd_dev->parent) { |
| 4313 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4314 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4315 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4316 | } |
| 4317 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4318 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4319 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4320 | } else { |
| 4321 | /* validate mapped snapshot's EXISTS flag */ |
| 4322 | rbd_exists_validate(rbd_dev); |
| 4323 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4324 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4325 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4326 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4327 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4328 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4329 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4330 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4331 | } |
| 4332 | |
Christoph Hellwig | d6296d3 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 4333 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 4334 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4335 | { |
| 4336 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4337 | |
| 4338 | INIT_WORK(work, rbd_queue_workfn); |
| 4339 | return 0; |
| 4340 | } |
| 4341 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 4342 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4343 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4344 | .init_request = rbd_init_request, |
| 4345 | }; |
| 4346 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4347 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4348 | { |
| 4349 | struct gendisk *disk; |
| 4350 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4351 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4352 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4353 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4354 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4355 | disk = alloc_disk(single_major ? |
| 4356 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4357 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4358 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4359 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4360 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4361 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4362 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4363 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4364 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4365 | if (single_major) |
| 4366 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4367 | disk->fops = &rbd_bd_ops; |
| 4368 | disk->private_data = rbd_dev; |
| 4369 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4370 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4371 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4372 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4373 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4374 | 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] | 4375 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4376 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4377 | |
| 4378 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4379 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4380 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4381 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4382 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4383 | if (IS_ERR(q)) { |
| 4384 | err = PTR_ERR(q); |
| 4385 | goto out_tag_set; |
| 4386 | } |
| 4387 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4388 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4389 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4390 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4391 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4392 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4393 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4394 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 4395 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 4396 | blk_queue_max_segment_size(q, UINT_MAX); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4397 | blk_queue_io_min(q, segment_size); |
| 4398 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4399 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4400 | /* enable the discard support */ |
| 4401 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4402 | q->limits.discard_granularity = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4403 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 4404 | blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4405 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4406 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4407 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4408 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4409 | /* |
| 4410 | * disk_release() expects a queue ref from add_disk() and will |
| 4411 | * put it. Hold an extra ref until add_disk() is called. |
| 4412 | */ |
| 4413 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4414 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4415 | q->queuedata = rbd_dev; |
| 4416 | |
| 4417 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4418 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4419 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4420 | out_tag_set: |
| 4421 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4422 | out_disk: |
| 4423 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4424 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4427 | /* |
| 4428 | sysfs |
| 4429 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4430 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4431 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4432 | { |
| 4433 | return container_of(dev, struct rbd_device, dev); |
| 4434 | } |
| 4435 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4436 | static ssize_t rbd_size_show(struct device *dev, |
| 4437 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4438 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4439 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4440 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4441 | return sprintf(buf, "%llu\n", |
| 4442 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4443 | } |
| 4444 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4445 | /* |
| 4446 | * Note this shows the features for whatever's mapped, which is not |
| 4447 | * necessarily the base image. |
| 4448 | */ |
| 4449 | static ssize_t rbd_features_show(struct device *dev, |
| 4450 | struct device_attribute *attr, char *buf) |
| 4451 | { |
| 4452 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4453 | |
| 4454 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4455 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4456 | } |
| 4457 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4458 | static ssize_t rbd_major_show(struct device *dev, |
| 4459 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4460 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4461 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4462 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4463 | if (rbd_dev->major) |
| 4464 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4465 | |
| 4466 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4467 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4468 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4469 | static ssize_t rbd_minor_show(struct device *dev, |
| 4470 | struct device_attribute *attr, char *buf) |
| 4471 | { |
| 4472 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4473 | |
| 4474 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4475 | } |
| 4476 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4477 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4478 | struct device_attribute *attr, char *buf) |
| 4479 | { |
| 4480 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4481 | struct ceph_entity_addr *client_addr = |
| 4482 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4483 | |
| 4484 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4485 | le32_to_cpu(client_addr->nonce)); |
| 4486 | } |
| 4487 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4488 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4489 | struct device_attribute *attr, char *buf) |
| 4490 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4491 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4492 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4493 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4494 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4495 | } |
| 4496 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4497 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4498 | struct device_attribute *attr, char *buf) |
| 4499 | { |
| 4500 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4501 | |
| 4502 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4503 | } |
| 4504 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4505 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4506 | struct device_attribute *attr, char *buf) |
| 4507 | { |
| 4508 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4509 | |
| 4510 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | static ssize_t rbd_pool_show(struct device *dev, |
| 4514 | struct device_attribute *attr, char *buf) |
| 4515 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4516 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4517 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4518 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4519 | } |
| 4520 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4521 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4522 | struct device_attribute *attr, char *buf) |
| 4523 | { |
| 4524 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4525 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4526 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4527 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4528 | } |
| 4529 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4530 | static ssize_t rbd_name_show(struct device *dev, |
| 4531 | struct device_attribute *attr, char *buf) |
| 4532 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4533 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4534 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4535 | if (rbd_dev->spec->image_name) |
| 4536 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4537 | |
| 4538 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4539 | } |
| 4540 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4541 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4542 | struct device_attribute *attr, char *buf) |
| 4543 | { |
| 4544 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4545 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4546 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4547 | } |
| 4548 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4549 | /* |
| 4550 | * Shows the name of the currently-mapped snapshot (or |
| 4551 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4552 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4553 | static ssize_t rbd_snap_show(struct device *dev, |
| 4554 | struct device_attribute *attr, |
| 4555 | char *buf) |
| 4556 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4557 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4558 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4559 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4560 | } |
| 4561 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4562 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4563 | struct device_attribute *attr, char *buf) |
| 4564 | { |
| 4565 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4566 | |
| 4567 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4568 | } |
| 4569 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4570 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4571 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4572 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4573 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4574 | */ |
| 4575 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4576 | struct device_attribute *attr, |
| 4577 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4578 | { |
| 4579 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4580 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4581 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4582 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4583 | return sprintf(buf, "(no parent image)\n"); |
| 4584 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4585 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4586 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4587 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4588 | count += sprintf(&buf[count], "%s" |
| 4589 | "pool_id %llu\npool_name %s\n" |
| 4590 | "image_id %s\nimage_name %s\n" |
| 4591 | "snap_id %llu\nsnap_name %s\n" |
| 4592 | "overlap %llu\n", |
| 4593 | !count ? "" : "\n", /* first? */ |
| 4594 | spec->pool_id, spec->pool_name, |
| 4595 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4596 | spec->snap_id, spec->snap_name, |
| 4597 | rbd_dev->parent_overlap); |
| 4598 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4599 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4600 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4601 | } |
| 4602 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4603 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4604 | struct device_attribute *attr, |
| 4605 | const char *buf, |
| 4606 | size_t size) |
| 4607 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4608 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4609 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4610 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4611 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4612 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4613 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4614 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4615 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4616 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4617 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4618 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4619 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4620 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4621 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4622 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4623 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4624 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4625 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4626 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4627 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4628 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4629 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4630 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4631 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4632 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4633 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4634 | |
| 4635 | static struct attribute *rbd_attrs[] = { |
| 4636 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4637 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4638 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4639 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4640 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4641 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4642 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4643 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4644 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4645 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4646 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4647 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4648 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4649 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4650 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4651 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4652 | NULL |
| 4653 | }; |
| 4654 | |
| 4655 | static struct attribute_group rbd_attr_group = { |
| 4656 | .attrs = rbd_attrs, |
| 4657 | }; |
| 4658 | |
| 4659 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4660 | &rbd_attr_group, |
| 4661 | NULL |
| 4662 | }; |
| 4663 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4664 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4665 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4666 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4667 | .name = "rbd", |
| 4668 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4669 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4670 | }; |
| 4671 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4672 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4673 | { |
| 4674 | kref_get(&spec->kref); |
| 4675 | |
| 4676 | return spec; |
| 4677 | } |
| 4678 | |
| 4679 | static void rbd_spec_free(struct kref *kref); |
| 4680 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4681 | { |
| 4682 | if (spec) |
| 4683 | kref_put(&spec->kref, rbd_spec_free); |
| 4684 | } |
| 4685 | |
| 4686 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4687 | { |
| 4688 | struct rbd_spec *spec; |
| 4689 | |
| 4690 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4691 | if (!spec) |
| 4692 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4693 | |
| 4694 | spec->pool_id = CEPH_NOPOOL; |
| 4695 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4696 | kref_init(&spec->kref); |
| 4697 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4698 | return spec; |
| 4699 | } |
| 4700 | |
| 4701 | static void rbd_spec_free(struct kref *kref) |
| 4702 | { |
| 4703 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4704 | |
| 4705 | kfree(spec->pool_name); |
| 4706 | kfree(spec->image_id); |
| 4707 | kfree(spec->image_name); |
| 4708 | kfree(spec->snap_name); |
| 4709 | kfree(spec); |
| 4710 | } |
| 4711 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4712 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4713 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4714 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4715 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4716 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4717 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4718 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4719 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4720 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4721 | rbd_put_client(rbd_dev->rbd_client); |
| 4722 | rbd_spec_put(rbd_dev->spec); |
| 4723 | kfree(rbd_dev->opts); |
| 4724 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | static void rbd_dev_release(struct device *dev) |
| 4728 | { |
| 4729 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4730 | bool need_put = !!rbd_dev->opts; |
| 4731 | |
| 4732 | if (need_put) { |
| 4733 | destroy_workqueue(rbd_dev->task_wq); |
| 4734 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4735 | } |
| 4736 | |
| 4737 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4738 | |
| 4739 | /* |
| 4740 | * This is racy, but way better than putting module outside of |
| 4741 | * the release callback. The race window is pretty small, so |
| 4742 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4743 | */ |
| 4744 | if (need_put) |
| 4745 | module_put(THIS_MODULE); |
| 4746 | } |
| 4747 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4748 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4749 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4750 | { |
| 4751 | struct rbd_device *rbd_dev; |
| 4752 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4753 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4754 | if (!rbd_dev) |
| 4755 | return NULL; |
| 4756 | |
| 4757 | spin_lock_init(&rbd_dev->lock); |
| 4758 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4759 | init_rwsem(&rbd_dev->header_rwsem); |
| 4760 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4761 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4762 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4763 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4764 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4765 | mutex_init(&rbd_dev->watch_mutex); |
| 4766 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4767 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4768 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4769 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4770 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4771 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4772 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4773 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4774 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4775 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4776 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4777 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4778 | rbd_dev->dev.type = &rbd_device_type; |
| 4779 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4780 | device_initialize(&rbd_dev->dev); |
| 4781 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4782 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4783 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4784 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4785 | return rbd_dev; |
| 4786 | } |
| 4787 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4788 | /* |
| 4789 | * Create a mapping rbd_dev. |
| 4790 | */ |
| 4791 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4792 | struct rbd_spec *spec, |
| 4793 | struct rbd_options *opts) |
| 4794 | { |
| 4795 | struct rbd_device *rbd_dev; |
| 4796 | |
| 4797 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4798 | if (!rbd_dev) |
| 4799 | return NULL; |
| 4800 | |
| 4801 | rbd_dev->opts = opts; |
| 4802 | |
| 4803 | /* get an id and fill in device name */ |
| 4804 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4805 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4806 | GFP_KERNEL); |
| 4807 | if (rbd_dev->dev_id < 0) |
| 4808 | goto fail_rbd_dev; |
| 4809 | |
| 4810 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4811 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4812 | rbd_dev->name); |
| 4813 | if (!rbd_dev->task_wq) |
| 4814 | goto fail_dev_id; |
| 4815 | |
| 4816 | /* we have a ref from do_rbd_add() */ |
| 4817 | __module_get(THIS_MODULE); |
| 4818 | |
| 4819 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4820 | return rbd_dev; |
| 4821 | |
| 4822 | fail_dev_id: |
| 4823 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4824 | fail_rbd_dev: |
| 4825 | rbd_dev_free(rbd_dev); |
| 4826 | return NULL; |
| 4827 | } |
| 4828 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4829 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4830 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4831 | if (rbd_dev) |
| 4832 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4833 | } |
| 4834 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4835 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4836 | * Get the size and object order for an image snapshot, or if |
| 4837 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4838 | * image. |
| 4839 | */ |
| 4840 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4841 | u8 *order, u64 *snap_size) |
| 4842 | { |
| 4843 | __le64 snapid = cpu_to_le64(snap_id); |
| 4844 | int ret; |
| 4845 | struct { |
| 4846 | u8 order; |
| 4847 | __le64 size; |
| 4848 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4849 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4850 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4851 | &rbd_dev->header_oloc, "get_size", |
| 4852 | &snapid, sizeof(snapid), |
| 4853 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4854 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4855 | if (ret < 0) |
| 4856 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4857 | if (ret < sizeof (size_buf)) |
| 4858 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4859 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4860 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4861 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4862 | dout(" order %u", (unsigned int)*order); |
| 4863 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4864 | *snap_size = le64_to_cpu(size_buf.size); |
| 4865 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4866 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4867 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4868 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4869 | |
| 4870 | return 0; |
| 4871 | } |
| 4872 | |
| 4873 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4874 | { |
| 4875 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4876 | &rbd_dev->header.obj_order, |
| 4877 | &rbd_dev->header.image_size); |
| 4878 | } |
| 4879 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4880 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4881 | { |
| 4882 | void *reply_buf; |
| 4883 | int ret; |
| 4884 | void *p; |
| 4885 | |
| 4886 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4887 | if (!reply_buf) |
| 4888 | return -ENOMEM; |
| 4889 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4890 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4891 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4892 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4893 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4894 | if (ret < 0) |
| 4895 | goto out; |
| 4896 | |
| 4897 | p = reply_buf; |
| 4898 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4899 | p + ret, NULL, GFP_NOIO); |
| 4900 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4901 | |
| 4902 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4903 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4904 | rbd_dev->header.object_prefix = NULL; |
| 4905 | } else { |
| 4906 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4907 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4908 | out: |
| 4909 | kfree(reply_buf); |
| 4910 | |
| 4911 | return ret; |
| 4912 | } |
| 4913 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4914 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4915 | u64 *snap_features) |
| 4916 | { |
| 4917 | __le64 snapid = cpu_to_le64(snap_id); |
| 4918 | struct { |
| 4919 | __le64 features; |
| 4920 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4921 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4922 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4923 | int ret; |
| 4924 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4925 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4926 | &rbd_dev->header_oloc, "get_features", |
| 4927 | &snapid, sizeof(snapid), |
| 4928 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4929 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4930 | if (ret < 0) |
| 4931 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4932 | if (ret < sizeof (features_buf)) |
| 4933 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4934 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4935 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4936 | if (unsup) { |
| 4937 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4938 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4939 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4940 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4941 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4942 | *snap_features = le64_to_cpu(features_buf.features); |
| 4943 | |
| 4944 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4945 | (unsigned long long)snap_id, |
| 4946 | (unsigned long long)*snap_features, |
| 4947 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4948 | |
| 4949 | return 0; |
| 4950 | } |
| 4951 | |
| 4952 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4953 | { |
| 4954 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4955 | &rbd_dev->header.features); |
| 4956 | } |
| 4957 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4958 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4959 | { |
| 4960 | struct rbd_spec *parent_spec; |
| 4961 | size_t size; |
| 4962 | void *reply_buf = NULL; |
| 4963 | __le64 snapid; |
| 4964 | void *p; |
| 4965 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4966 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4967 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4968 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4969 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4970 | int ret; |
| 4971 | |
| 4972 | parent_spec = rbd_spec_alloc(); |
| 4973 | if (!parent_spec) |
| 4974 | return -ENOMEM; |
| 4975 | |
| 4976 | size = sizeof (__le64) + /* pool_id */ |
| 4977 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4978 | sizeof (__le64) + /* snap_id */ |
| 4979 | sizeof (__le64); /* overlap */ |
| 4980 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4981 | if (!reply_buf) { |
| 4982 | ret = -ENOMEM; |
| 4983 | goto out_err; |
| 4984 | } |
| 4985 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4986 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4987 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4988 | &rbd_dev->header_oloc, "get_parent", |
| 4989 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4990 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4991 | if (ret < 0) |
| 4992 | goto out_err; |
| 4993 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4994 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4995 | end = reply_buf + ret; |
| 4996 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4997 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4998 | if (pool_id == CEPH_NOPOOL) { |
| 4999 | /* |
| 5000 | * Either the parent never existed, or we have |
| 5001 | * record of it but the image got flattened so it no |
| 5002 | * longer has a parent. When the parent of a |
| 5003 | * layered image disappears we immediately set the |
| 5004 | * overlap to 0. The effect of this is that all new |
| 5005 | * requests will be treated as if the image had no |
| 5006 | * parent. |
| 5007 | */ |
| 5008 | if (rbd_dev->parent_overlap) { |
| 5009 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5010 | rbd_dev_parent_put(rbd_dev); |
| 5011 | pr_info("%s: clone image has been flattened\n", |
| 5012 | rbd_dev->disk->disk_name); |
| 5013 | } |
| 5014 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5015 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5016 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5017 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5018 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5019 | |
| 5020 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5021 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5022 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5023 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5024 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5025 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5026 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5027 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5028 | if (IS_ERR(image_id)) { |
| 5029 | ret = PTR_ERR(image_id); |
| 5030 | goto out_err; |
| 5031 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5032 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5033 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5034 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5035 | /* |
| 5036 | * The parent won't change (except when the clone is |
| 5037 | * flattened, already handled that). So we only need to |
| 5038 | * record the parent spec we have not already done so. |
| 5039 | */ |
| 5040 | if (!rbd_dev->parent_spec) { |
| 5041 | parent_spec->pool_id = pool_id; |
| 5042 | parent_spec->image_id = image_id; |
| 5043 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5044 | rbd_dev->parent_spec = parent_spec; |
| 5045 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5046 | } else { |
| 5047 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5051 | * We always update the parent overlap. If it's zero we issue |
| 5052 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5053 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5054 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5055 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5056 | /* refresh, careful to warn just once */ |
| 5057 | if (rbd_dev->parent_overlap) |
| 5058 | rbd_warn(rbd_dev, |
| 5059 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5060 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5061 | /* initial probe */ |
| 5062 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5063 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5064 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5065 | rbd_dev->parent_overlap = overlap; |
| 5066 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5067 | out: |
| 5068 | ret = 0; |
| 5069 | out_err: |
| 5070 | kfree(reply_buf); |
| 5071 | rbd_spec_put(parent_spec); |
| 5072 | |
| 5073 | return ret; |
| 5074 | } |
| 5075 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5076 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5077 | { |
| 5078 | struct { |
| 5079 | __le64 stripe_unit; |
| 5080 | __le64 stripe_count; |
| 5081 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5082 | size_t size = sizeof (striping_info_buf); |
| 5083 | void *p; |
| 5084 | u64 obj_size; |
| 5085 | u64 stripe_unit; |
| 5086 | u64 stripe_count; |
| 5087 | int ret; |
| 5088 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5089 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5090 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5091 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5092 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5093 | if (ret < 0) |
| 5094 | return ret; |
| 5095 | if (ret < size) |
| 5096 | return -ERANGE; |
| 5097 | |
| 5098 | /* |
| 5099 | * We don't actually support the "fancy striping" feature |
| 5100 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5101 | * defaults the behavior is the same as before. So find |
| 5102 | * out, and only fail if the image has non-default values. |
| 5103 | */ |
| 5104 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5105 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5106 | p = &striping_info_buf; |
| 5107 | stripe_unit = ceph_decode_64(&p); |
| 5108 | if (stripe_unit != obj_size) { |
| 5109 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5110 | "(got %llu want %llu)", |
| 5111 | stripe_unit, obj_size); |
| 5112 | return -EINVAL; |
| 5113 | } |
| 5114 | stripe_count = ceph_decode_64(&p); |
| 5115 | if (stripe_count != 1) { |
| 5116 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5117 | "(got %llu want 1)", stripe_count); |
| 5118 | return -EINVAL; |
| 5119 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5120 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5121 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5122 | |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5126 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5127 | { |
| 5128 | __le64 data_pool_id; |
| 5129 | int ret; |
| 5130 | |
| 5131 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5132 | &rbd_dev->header_oloc, "get_data_pool", |
| 5133 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5134 | if (ret < 0) |
| 5135 | return ret; |
| 5136 | if (ret < sizeof(data_pool_id)) |
| 5137 | return -EBADMSG; |
| 5138 | |
| 5139 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5140 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5144 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5145 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5146 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5147 | size_t image_id_size; |
| 5148 | char *image_id; |
| 5149 | void *p; |
| 5150 | void *end; |
| 5151 | size_t size; |
| 5152 | void *reply_buf = NULL; |
| 5153 | size_t len = 0; |
| 5154 | char *image_name = NULL; |
| 5155 | int ret; |
| 5156 | |
| 5157 | rbd_assert(!rbd_dev->spec->image_name); |
| 5158 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5159 | len = strlen(rbd_dev->spec->image_id); |
| 5160 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5161 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5162 | if (!image_id) |
| 5163 | return NULL; |
| 5164 | |
| 5165 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5166 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5167 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5168 | |
| 5169 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5170 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5171 | if (!reply_buf) |
| 5172 | goto out; |
| 5173 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5174 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5175 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5176 | "dir_get_name", image_id, image_id_size, |
| 5177 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5178 | if (ret < 0) |
| 5179 | goto out; |
| 5180 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5181 | end = reply_buf + ret; |
| 5182 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5183 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5184 | if (IS_ERR(image_name)) |
| 5185 | image_name = NULL; |
| 5186 | else |
| 5187 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5188 | out: |
| 5189 | kfree(reply_buf); |
| 5190 | kfree(image_id); |
| 5191 | |
| 5192 | return image_name; |
| 5193 | } |
| 5194 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5195 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5196 | { |
| 5197 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5198 | const char *snap_name; |
| 5199 | u32 which = 0; |
| 5200 | |
| 5201 | /* Skip over names until we find the one we are looking for */ |
| 5202 | |
| 5203 | snap_name = rbd_dev->header.snap_names; |
| 5204 | while (which < snapc->num_snaps) { |
| 5205 | if (!strcmp(name, snap_name)) |
| 5206 | return snapc->snaps[which]; |
| 5207 | snap_name += strlen(snap_name) + 1; |
| 5208 | which++; |
| 5209 | } |
| 5210 | return CEPH_NOSNAP; |
| 5211 | } |
| 5212 | |
| 5213 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5214 | { |
| 5215 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5216 | u32 which; |
| 5217 | bool found = false; |
| 5218 | u64 snap_id; |
| 5219 | |
| 5220 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5221 | const char *snap_name; |
| 5222 | |
| 5223 | snap_id = snapc->snaps[which]; |
| 5224 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5225 | if (IS_ERR(snap_name)) { |
| 5226 | /* ignore no-longer existing snapshots */ |
| 5227 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5228 | continue; |
| 5229 | else |
| 5230 | break; |
| 5231 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5232 | found = !strcmp(name, snap_name); |
| 5233 | kfree(snap_name); |
| 5234 | } |
| 5235 | return found ? snap_id : CEPH_NOSNAP; |
| 5236 | } |
| 5237 | |
| 5238 | /* |
| 5239 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5240 | * no snapshot by that name is found, or if an error occurs. |
| 5241 | */ |
| 5242 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5243 | { |
| 5244 | if (rbd_dev->image_format == 1) |
| 5245 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5246 | |
| 5247 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5248 | } |
| 5249 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5250 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5251 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5252 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5253 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5254 | { |
| 5255 | struct rbd_spec *spec = rbd_dev->spec; |
| 5256 | |
| 5257 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5258 | rbd_assert(spec->image_id && spec->image_name); |
| 5259 | rbd_assert(spec->snap_name); |
| 5260 | |
| 5261 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5262 | u64 snap_id; |
| 5263 | |
| 5264 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5265 | if (snap_id == CEPH_NOSNAP) |
| 5266 | return -ENOENT; |
| 5267 | |
| 5268 | spec->snap_id = snap_id; |
| 5269 | } else { |
| 5270 | spec->snap_id = CEPH_NOSNAP; |
| 5271 | } |
| 5272 | |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | /* |
| 5277 | * A parent image will have all ids but none of the names. |
| 5278 | * |
| 5279 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5280 | * can't figure out the name for an image id. |
| 5281 | */ |
| 5282 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5283 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5284 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5285 | struct rbd_spec *spec = rbd_dev->spec; |
| 5286 | const char *pool_name; |
| 5287 | const char *image_name; |
| 5288 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5289 | int ret; |
| 5290 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5291 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5292 | rbd_assert(spec->image_id); |
| 5293 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5294 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5295 | /* 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] | 5296 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5297 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5298 | if (!pool_name) { |
| 5299 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5300 | return -EIO; |
| 5301 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5302 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5303 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5304 | return -ENOMEM; |
| 5305 | |
| 5306 | /* Fetch the image name; tolerate failure here */ |
| 5307 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5308 | image_name = rbd_dev_image_name(rbd_dev); |
| 5309 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5310 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5311 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5312 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5313 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5314 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5315 | if (IS_ERR(snap_name)) { |
| 5316 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5317 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5318 | } |
| 5319 | |
| 5320 | spec->pool_name = pool_name; |
| 5321 | spec->image_name = image_name; |
| 5322 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5323 | |
| 5324 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5325 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5326 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5327 | kfree(image_name); |
| 5328 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5329 | return ret; |
| 5330 | } |
| 5331 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5332 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5333 | { |
| 5334 | size_t size; |
| 5335 | int ret; |
| 5336 | void *reply_buf; |
| 5337 | void *p; |
| 5338 | void *end; |
| 5339 | u64 seq; |
| 5340 | u32 snap_count; |
| 5341 | struct ceph_snap_context *snapc; |
| 5342 | u32 i; |
| 5343 | |
| 5344 | /* |
| 5345 | * We'll need room for the seq value (maximum snapshot id), |
| 5346 | * snapshot count, and array of that many snapshot ids. |
| 5347 | * For now we have a fixed upper limit on the number we're |
| 5348 | * prepared to receive. |
| 5349 | */ |
| 5350 | size = sizeof (__le64) + sizeof (__le32) + |
| 5351 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5352 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5353 | if (!reply_buf) |
| 5354 | return -ENOMEM; |
| 5355 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5356 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5357 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5358 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5359 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5360 | if (ret < 0) |
| 5361 | goto out; |
| 5362 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5363 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5364 | end = reply_buf + ret; |
| 5365 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5366 | ceph_decode_64_safe(&p, end, seq, out); |
| 5367 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5368 | |
| 5369 | /* |
| 5370 | * Make sure the reported number of snapshot ids wouldn't go |
| 5371 | * beyond the end of our buffer. But before checking that, |
| 5372 | * make sure the computed size of the snapshot context we |
| 5373 | * allocate is representable in a size_t. |
| 5374 | */ |
| 5375 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5376 | / sizeof (u64)) { |
| 5377 | ret = -EINVAL; |
| 5378 | goto out; |
| 5379 | } |
| 5380 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5381 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5382 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5383 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5384 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5385 | if (!snapc) { |
| 5386 | ret = -ENOMEM; |
| 5387 | goto out; |
| 5388 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5389 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5390 | for (i = 0; i < snap_count; i++) |
| 5391 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5392 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5393 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5394 | rbd_dev->header.snapc = snapc; |
| 5395 | |
| 5396 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5397 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5398 | out: |
| 5399 | kfree(reply_buf); |
| 5400 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5401 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5402 | } |
| 5403 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5404 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5405 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5406 | { |
| 5407 | size_t size; |
| 5408 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5409 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5410 | int ret; |
| 5411 | void *p; |
| 5412 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5413 | char *snap_name; |
| 5414 | |
| 5415 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5416 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5417 | if (!reply_buf) |
| 5418 | return ERR_PTR(-ENOMEM); |
| 5419 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5420 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5421 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5422 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5423 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5424 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5425 | if (ret < 0) { |
| 5426 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5427 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5428 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5429 | |
| 5430 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5431 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5432 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5433 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5434 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5435 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5436 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5437 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5438 | out: |
| 5439 | kfree(reply_buf); |
| 5440 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5441 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5442 | } |
| 5443 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5444 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5445 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5446 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5447 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5448 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5449 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5450 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5451 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5452 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5453 | if (first_time) { |
| 5454 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5455 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5456 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5457 | } |
| 5458 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5459 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5460 | if (ret && first_time) { |
| 5461 | kfree(rbd_dev->header.object_prefix); |
| 5462 | rbd_dev->header.object_prefix = NULL; |
| 5463 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5464 | |
| 5465 | return ret; |
| 5466 | } |
| 5467 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5468 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5469 | { |
| 5470 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5471 | |
| 5472 | if (rbd_dev->image_format == 1) |
| 5473 | return rbd_dev_v1_header_info(rbd_dev); |
| 5474 | |
| 5475 | return rbd_dev_v2_header_info(rbd_dev); |
| 5476 | } |
| 5477 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5478 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5479 | * Skips over white space at *buf, and updates *buf to point to the |
| 5480 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5481 | * the token (string of non-white space characters) found. Note |
| 5482 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5483 | */ |
| 5484 | static inline size_t next_token(const char **buf) |
| 5485 | { |
| 5486 | /* |
| 5487 | * These are the characters that produce nonzero for |
| 5488 | * isspace() in the "C" and "POSIX" locales. |
| 5489 | */ |
| 5490 | const char *spaces = " \f\n\r\t\v"; |
| 5491 | |
| 5492 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5493 | |
| 5494 | return strcspn(*buf, spaces); /* Return token length */ |
| 5495 | } |
| 5496 | |
| 5497 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5498 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5499 | * enough to hold a copy of it, and copies the token into the new |
| 5500 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5501 | * that a duplicate buffer is created even for a zero-length token. |
| 5502 | * |
| 5503 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5504 | * pointer if memory for the duplicate was not available. If |
| 5505 | * the lenp argument is a non-null pointer, the length of the token |
| 5506 | * (not including the '\0') is returned in *lenp. |
| 5507 | * |
| 5508 | * If successful, the *buf pointer will be updated to point beyond |
| 5509 | * the end of the found token. |
| 5510 | * |
| 5511 | * Note: uses GFP_KERNEL for allocation. |
| 5512 | */ |
| 5513 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5514 | { |
| 5515 | char *dup; |
| 5516 | size_t len; |
| 5517 | |
| 5518 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5519 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5520 | if (!dup) |
| 5521 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5522 | *(dup + len) = '\0'; |
| 5523 | *buf += len; |
| 5524 | |
| 5525 | if (lenp) |
| 5526 | *lenp = len; |
| 5527 | |
| 5528 | return dup; |
| 5529 | } |
| 5530 | |
| 5531 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5532 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5533 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5534 | * and the data written is passed here via a NUL-terminated buffer. |
| 5535 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5536 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5537 | * The information extracted from these options is recorded in |
| 5538 | * the other parameters which return dynamically-allocated |
| 5539 | * structures: |
| 5540 | * ceph_opts |
| 5541 | * The address of a pointer that will refer to a ceph options |
| 5542 | * structure. Caller must release the returned pointer using |
| 5543 | * ceph_destroy_options() when it is no longer needed. |
| 5544 | * rbd_opts |
| 5545 | * Address of an rbd options pointer. Fully initialized by |
| 5546 | * this function; caller must release with kfree(). |
| 5547 | * spec |
| 5548 | * Address of an rbd image specification pointer. Fully |
| 5549 | * initialized by this function based on parsed options. |
| 5550 | * Caller must release with rbd_spec_put(). |
| 5551 | * |
| 5552 | * The options passed take this form: |
| 5553 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5554 | * where: |
| 5555 | * <mon_addrs> |
| 5556 | * A comma-separated list of one or more monitor addresses. |
| 5557 | * A monitor address is an ip address, optionally followed |
| 5558 | * by a port number (separated by a colon). |
| 5559 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5560 | * <options> |
| 5561 | * A comma-separated list of ceph and/or rbd options. |
| 5562 | * <pool_name> |
| 5563 | * The name of the rados pool containing the rbd image. |
| 5564 | * <image_name> |
| 5565 | * The name of the image in that pool to map. |
| 5566 | * <snap_id> |
| 5567 | * An optional snapshot id. If provided, the mapping will |
| 5568 | * present data from the image at the time that snapshot was |
| 5569 | * created. The image head is used if no snapshot id is |
| 5570 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5571 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5572 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5573 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5574 | struct rbd_options **opts, |
| 5575 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5576 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5577 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5578 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5579 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5580 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5581 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5582 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5583 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5584 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5585 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5586 | |
| 5587 | /* The first four tokens are required */ |
| 5588 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5589 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5590 | if (!len) { |
| 5591 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5592 | return -EINVAL; |
| 5593 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5594 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5595 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5596 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5597 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5598 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5599 | options = dup_token(&buf, NULL); |
| 5600 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5601 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5602 | if (!*options) { |
| 5603 | rbd_warn(NULL, "no options provided"); |
| 5604 | goto out_err; |
| 5605 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5606 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5607 | spec = rbd_spec_alloc(); |
| 5608 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5609 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5610 | |
| 5611 | spec->pool_name = dup_token(&buf, NULL); |
| 5612 | if (!spec->pool_name) |
| 5613 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5614 | if (!*spec->pool_name) { |
| 5615 | rbd_warn(NULL, "no pool name provided"); |
| 5616 | goto out_err; |
| 5617 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5618 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5619 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5620 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5621 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5622 | if (!*spec->image_name) { |
| 5623 | rbd_warn(NULL, "no image name provided"); |
| 5624 | goto out_err; |
| 5625 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5626 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5627 | /* |
| 5628 | * Snapshot name is optional; default is to use "-" |
| 5629 | * (indicating the head/no snapshot). |
| 5630 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5631 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5632 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5633 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5634 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5635 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5636 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5637 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5638 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5639 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5640 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5641 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5642 | *(snap_name + len) = '\0'; |
| 5643 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5644 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5645 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5646 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5647 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5648 | if (!rbd_opts) |
| 5649 | goto out_mem; |
| 5650 | |
| 5651 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5652 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5653 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5654 | rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5655 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5656 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5657 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5658 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5659 | if (IS_ERR(copts)) { |
| 5660 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5661 | goto out_err; |
| 5662 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5663 | kfree(options); |
| 5664 | |
| 5665 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5666 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5667 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5668 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5669 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5670 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5671 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5672 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5673 | kfree(rbd_opts); |
| 5674 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5675 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5676 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5677 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5678 | } |
| 5679 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5680 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5681 | * Return pool id (>= 0) or a negative error code. |
| 5682 | */ |
| 5683 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5684 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5685 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5686 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5687 | int tries = 0; |
| 5688 | int ret; |
| 5689 | |
| 5690 | again: |
| 5691 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5692 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5693 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5694 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5695 | if (ret < 0) |
| 5696 | return ret; |
| 5697 | |
| 5698 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5699 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5700 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5701 | newest_epoch, |
| 5702 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5703 | goto again; |
| 5704 | } else { |
| 5705 | /* the osdmap we have is new enough */ |
| 5706 | return -ENOENT; |
| 5707 | } |
| 5708 | } |
| 5709 | |
| 5710 | return ret; |
| 5711 | } |
| 5712 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5713 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5714 | { |
| 5715 | down_write(&rbd_dev->lock_rwsem); |
| 5716 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5717 | rbd_unlock(rbd_dev); |
| 5718 | up_write(&rbd_dev->lock_rwsem); |
| 5719 | } |
| 5720 | |
| 5721 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5722 | { |
| 5723 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5724 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5725 | return -EINVAL; |
| 5726 | } |
| 5727 | |
| 5728 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5729 | down_read(&rbd_dev->lock_rwsem); |
| 5730 | rbd_wait_state_locked(rbd_dev); |
| 5731 | up_read(&rbd_dev->lock_rwsem); |
| 5732 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 5733 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5734 | return -EROFS; |
| 5735 | } |
| 5736 | |
| 5737 | return 0; |
| 5738 | } |
| 5739 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5740 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5741 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5742 | * name given to it by the user. Internally, that identifier is |
| 5743 | * what's used to specify the names of objects related to the image. |
| 5744 | * |
| 5745 | * A special "rbd id" object is used to map an rbd image name to its |
| 5746 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5747 | * with the supplied name. |
| 5748 | * |
| 5749 | * This function will record the given rbd_dev's image_id field if |
| 5750 | * it can be determined, and in that case will return 0. If any |
| 5751 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5752 | * image_id field will be unchanged (and should be NULL). |
| 5753 | */ |
| 5754 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5755 | { |
| 5756 | int ret; |
| 5757 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5758 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5759 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5760 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5761 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5762 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5763 | * When probing a parent image, the image id is already |
| 5764 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5765 | * need to fetch the image id again in this case. We |
| 5766 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5767 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5768 | if (rbd_dev->spec->image_id) { |
| 5769 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5770 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5771 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5772 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5773 | |
| 5774 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5775 | * First, see if the format 2 image id file exists, and if |
| 5776 | * so, get the image's persistent id from it. |
| 5777 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5778 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5779 | rbd_dev->spec->image_name); |
| 5780 | if (ret) |
| 5781 | return ret; |
| 5782 | |
| 5783 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5784 | |
| 5785 | /* Response will be an encoded string, which includes a length */ |
| 5786 | |
| 5787 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5788 | response = kzalloc(size, GFP_NOIO); |
| 5789 | if (!response) { |
| 5790 | ret = -ENOMEM; |
| 5791 | goto out; |
| 5792 | } |
| 5793 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5794 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5795 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5796 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5797 | "get_id", NULL, 0, |
| 5798 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5799 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5800 | if (ret == -ENOENT) { |
| 5801 | image_id = kstrdup("", GFP_KERNEL); |
| 5802 | ret = image_id ? 0 : -ENOMEM; |
| 5803 | if (!ret) |
| 5804 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5805 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5806 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5807 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5808 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5809 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5810 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5811 | if (!ret) |
| 5812 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | if (!ret) { |
| 5816 | rbd_dev->spec->image_id = image_id; |
| 5817 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5818 | } |
| 5819 | out: |
| 5820 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5821 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5822 | return ret; |
| 5823 | } |
| 5824 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5825 | /* |
| 5826 | * Undo whatever state changes are made by v1 or v2 header info |
| 5827 | * call. |
| 5828 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5829 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5830 | { |
| 5831 | struct rbd_image_header *header; |
| 5832 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5833 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5834 | |
| 5835 | /* Free dynamic fields from the header, then zero it out */ |
| 5836 | |
| 5837 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5838 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5839 | kfree(header->snap_sizes); |
| 5840 | kfree(header->snap_names); |
| 5841 | kfree(header->object_prefix); |
| 5842 | memset(header, 0, sizeof (*header)); |
| 5843 | } |
| 5844 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5845 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5846 | { |
| 5847 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5848 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5849 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5850 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5851 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5852 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5853 | /* |
| 5854 | * Get the and check features for the image. Currently the |
| 5855 | * features are assumed to never change. |
| 5856 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5857 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5858 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5859 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5860 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5861 | /* If the image supports fancy striping, get its parameters */ |
| 5862 | |
| 5863 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5864 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5865 | if (ret < 0) |
| 5866 | goto out_err; |
| 5867 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5868 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5869 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5870 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5871 | if (ret) |
| 5872 | goto out_err; |
| 5873 | } |
| 5874 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5875 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5876 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5877 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5878 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5879 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5880 | kfree(rbd_dev->header.object_prefix); |
| 5881 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5882 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5883 | } |
| 5884 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5885 | /* |
| 5886 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5887 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5888 | * length of the already discovered part of the parent chain. |
| 5889 | */ |
| 5890 | 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] | 5891 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5892 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5893 | int ret; |
| 5894 | |
| 5895 | if (!rbd_dev->parent_spec) |
| 5896 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5897 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5898 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5899 | pr_info("parent chain is too long (%d)\n", depth); |
| 5900 | ret = -EINVAL; |
| 5901 | goto out_err; |
| 5902 | } |
| 5903 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5904 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5905 | if (!parent) { |
| 5906 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5907 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | /* |
| 5911 | * Images related by parent/child relationships always share |
| 5912 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5913 | */ |
| 5914 | __rbd_get_client(rbd_dev->rbd_client); |
| 5915 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5916 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5917 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5918 | if (ret < 0) |
| 5919 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5920 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5921 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5922 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5923 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5924 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5925 | out_err: |
| 5926 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5927 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5928 | return ret; |
| 5929 | } |
| 5930 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5931 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5932 | { |
| 5933 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5934 | rbd_dev_mapping_clear(rbd_dev); |
| 5935 | rbd_free_disk(rbd_dev); |
| 5936 | if (!single_major) |
| 5937 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5938 | } |
| 5939 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5940 | /* |
| 5941 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5942 | * upon return. |
| 5943 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5944 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5945 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5946 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5947 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5948 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5949 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5950 | if (!single_major) { |
| 5951 | ret = register_blkdev(0, rbd_dev->name); |
| 5952 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5953 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5954 | |
| 5955 | rbd_dev->major = ret; |
| 5956 | rbd_dev->minor = 0; |
| 5957 | } else { |
| 5958 | rbd_dev->major = rbd_major; |
| 5959 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5960 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5961 | |
| 5962 | /* Set up the blkdev mapping. */ |
| 5963 | |
| 5964 | ret = rbd_init_disk(rbd_dev); |
| 5965 | if (ret) |
| 5966 | goto err_out_blkdev; |
| 5967 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5968 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5969 | if (ret) |
| 5970 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5971 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5972 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5973 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5974 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5975 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5976 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5977 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5978 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5979 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5980 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5981 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5982 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5983 | err_out_mapping: |
| 5984 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5985 | err_out_disk: |
| 5986 | rbd_free_disk(rbd_dev); |
| 5987 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5988 | if (!single_major) |
| 5989 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5990 | err_out_unlock: |
| 5991 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5992 | return ret; |
| 5993 | } |
| 5994 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5995 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5996 | { |
| 5997 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5998 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5999 | |
| 6000 | /* Record the header object name for this rbd image. */ |
| 6001 | |
| 6002 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6003 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6004 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6005 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6006 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6007 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6008 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6009 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6010 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6011 | } |
| 6012 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6013 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6014 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6015 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6016 | if (rbd_dev->opts) |
| 6017 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6018 | rbd_dev->image_format = 0; |
| 6019 | kfree(rbd_dev->spec->image_id); |
| 6020 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6021 | } |
| 6022 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6023 | /* |
| 6024 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6025 | * device. If this image is the one being mapped (i.e., not a |
| 6026 | * parent), initiate a watch on its header object before using that |
| 6027 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6028 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6029 | 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] | 6030 | { |
| 6031 | int ret; |
| 6032 | |
| 6033 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6034 | * Get the id from the image id object. Unless there's an |
| 6035 | * error, rbd_dev->spec->image_id will be filled in with |
| 6036 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6037 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6038 | */ |
| 6039 | ret = rbd_dev_image_id(rbd_dev); |
| 6040 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6041 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6042 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6043 | ret = rbd_dev_header_name(rbd_dev); |
| 6044 | if (ret) |
| 6045 | goto err_out_format; |
| 6046 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6047 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6048 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6049 | if (ret) { |
| 6050 | if (ret == -ENOENT) |
| 6051 | pr_info("image %s/%s does not exist\n", |
| 6052 | rbd_dev->spec->pool_name, |
| 6053 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6054 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6055 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6056 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6057 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6058 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6059 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6060 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6061 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6062 | /* |
| 6063 | * If this image is the one being mapped, we have pool name and |
| 6064 | * id, image name and id, and snap name - need to fill snap id. |
| 6065 | * Otherwise this is a parent image, identified by pool, image |
| 6066 | * and snap ids - need to fill in names for those ids. |
| 6067 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6068 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6069 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6070 | else |
| 6071 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6072 | if (ret) { |
| 6073 | if (ret == -ENOENT) |
| 6074 | pr_info("snap %s/%s@%s does not exist\n", |
| 6075 | rbd_dev->spec->pool_name, |
| 6076 | rbd_dev->spec->image_name, |
| 6077 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6078 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6079 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6080 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6081 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6082 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6083 | if (ret) |
| 6084 | goto err_out_probe; |
| 6085 | |
| 6086 | /* |
| 6087 | * Need to warn users if this image is the one being |
| 6088 | * mapped and has a parent. |
| 6089 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6090 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6091 | rbd_warn(rbd_dev, |
| 6092 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6093 | } |
| 6094 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6095 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6096 | if (ret) |
| 6097 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6098 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6099 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6100 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6101 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6102 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6103 | err_out_probe: |
| 6104 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6105 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6106 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6107 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6108 | err_out_format: |
| 6109 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6110 | kfree(rbd_dev->spec->image_id); |
| 6111 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6112 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6113 | } |
| 6114 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6115 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6116 | const char *buf, |
| 6117 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6118 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6119 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6120 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6121 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6122 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6123 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6124 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6125 | |
| 6126 | if (!try_module_get(THIS_MODULE)) |
| 6127 | return -ENODEV; |
| 6128 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6129 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6130 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6131 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6132 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6133 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6134 | rbdc = rbd_get_client(ceph_opts); |
| 6135 | if (IS_ERR(rbdc)) { |
| 6136 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6137 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6138 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6139 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6140 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6141 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6142 | if (rc < 0) { |
| 6143 | if (rc == -ENOENT) |
| 6144 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6145 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6146 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6147 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6148 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6149 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6150 | if (!rbd_dev) { |
| 6151 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6152 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6153 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6154 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6155 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6156 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6157 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6158 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6159 | if (!rbd_dev->config_info) { |
| 6160 | rc = -ENOMEM; |
| 6161 | goto err_out_rbd_dev; |
| 6162 | } |
| 6163 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6164 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6165 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6166 | if (rc < 0) { |
| 6167 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6168 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6169 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6170 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6171 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6172 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 6173 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6174 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6175 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6176 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6177 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6178 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6179 | if (rbd_dev->opts->exclusive) { |
| 6180 | rc = rbd_add_acquire_lock(rbd_dev); |
| 6181 | if (rc) |
| 6182 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6183 | } |
| 6184 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6185 | /* Everything's ready. Announce the disk to the world. */ |
| 6186 | |
| 6187 | rc = device_add(&rbd_dev->dev); |
| 6188 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6189 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6190 | |
| 6191 | add_disk(rbd_dev->disk); |
| 6192 | /* see rbd_init_disk() */ |
| 6193 | blk_put_queue(rbd_dev->disk->queue); |
| 6194 | |
| 6195 | spin_lock(&rbd_dev_list_lock); |
| 6196 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6197 | spin_unlock(&rbd_dev_list_lock); |
| 6198 | |
| 6199 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6200 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6201 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6202 | rc = count; |
| 6203 | out: |
| 6204 | module_put(THIS_MODULE); |
| 6205 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6206 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6207 | err_out_image_lock: |
| 6208 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6209 | err_out_device_setup: |
| 6210 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6211 | err_out_image_probe: |
| 6212 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6213 | err_out_rbd_dev: |
| 6214 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6215 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6216 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6217 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6218 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6219 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6220 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6221 | } |
| 6222 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6223 | static ssize_t rbd_add(struct bus_type *bus, |
| 6224 | const char *buf, |
| 6225 | size_t count) |
| 6226 | { |
| 6227 | if (single_major) |
| 6228 | return -EINVAL; |
| 6229 | |
| 6230 | return do_rbd_add(bus, buf, count); |
| 6231 | } |
| 6232 | |
| 6233 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6234 | const char *buf, |
| 6235 | size_t count) |
| 6236 | { |
| 6237 | return do_rbd_add(bus, buf, count); |
| 6238 | } |
| 6239 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6240 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6241 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6242 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6243 | struct rbd_device *first = rbd_dev; |
| 6244 | struct rbd_device *second = first->parent; |
| 6245 | struct rbd_device *third; |
| 6246 | |
| 6247 | /* |
| 6248 | * Follow to the parent with no grandparent and |
| 6249 | * remove it. |
| 6250 | */ |
| 6251 | while (second && (third = second->parent)) { |
| 6252 | first = second; |
| 6253 | second = third; |
| 6254 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6255 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6256 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6257 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6258 | first->parent = NULL; |
| 6259 | first->parent_overlap = 0; |
| 6260 | |
| 6261 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6262 | rbd_spec_put(first->parent_spec); |
| 6263 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6264 | } |
| 6265 | } |
| 6266 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6267 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6268 | const char *buf, |
| 6269 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6270 | { |
| 6271 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6272 | struct list_head *tmp; |
| 6273 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6274 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6275 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6276 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6277 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6278 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6279 | dev_id = -1; |
| 6280 | opt_buf[0] = '\0'; |
| 6281 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6282 | if (dev_id < 0) { |
| 6283 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6284 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6285 | } |
| 6286 | if (opt_buf[0] != '\0') { |
| 6287 | if (!strcmp(opt_buf, "force")) { |
| 6288 | force = true; |
| 6289 | } else { |
| 6290 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6291 | return -EINVAL; |
| 6292 | } |
| 6293 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6294 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6295 | ret = -ENOENT; |
| 6296 | spin_lock(&rbd_dev_list_lock); |
| 6297 | list_for_each(tmp, &rbd_dev_list) { |
| 6298 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6299 | if (rbd_dev->dev_id == dev_id) { |
| 6300 | ret = 0; |
| 6301 | break; |
| 6302 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6303 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6304 | if (!ret) { |
| 6305 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6306 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6307 | ret = -EBUSY; |
| 6308 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6309 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6310 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6311 | spin_unlock_irq(&rbd_dev->lock); |
| 6312 | } |
| 6313 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6314 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6315 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6316 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6317 | if (force) { |
| 6318 | /* |
| 6319 | * Prevent new IO from being queued and wait for existing |
| 6320 | * IO to complete/fail. |
| 6321 | */ |
| 6322 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6323 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6324 | } |
| 6325 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6326 | del_gendisk(rbd_dev->disk); |
| 6327 | spin_lock(&rbd_dev_list_lock); |
| 6328 | list_del_init(&rbd_dev->node); |
| 6329 | spin_unlock(&rbd_dev_list_lock); |
| 6330 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6331 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6332 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6333 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6334 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6335 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6336 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6337 | } |
| 6338 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6339 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6340 | const char *buf, |
| 6341 | size_t count) |
| 6342 | { |
| 6343 | if (single_major) |
| 6344 | return -EINVAL; |
| 6345 | |
| 6346 | return do_rbd_remove(bus, buf, count); |
| 6347 | } |
| 6348 | |
| 6349 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6350 | const char *buf, |
| 6351 | size_t count) |
| 6352 | { |
| 6353 | return do_rbd_remove(bus, buf, count); |
| 6354 | } |
| 6355 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6356 | /* |
| 6357 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6358 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6359 | */ |
| 6360 | static int rbd_sysfs_init(void) |
| 6361 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6362 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6363 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6364 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6365 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6366 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6367 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6368 | ret = bus_register(&rbd_bus_type); |
| 6369 | if (ret < 0) |
| 6370 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6371 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6372 | return ret; |
| 6373 | } |
| 6374 | |
| 6375 | static void rbd_sysfs_cleanup(void) |
| 6376 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6377 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6378 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6379 | } |
| 6380 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6381 | static int rbd_slab_init(void) |
| 6382 | { |
| 6383 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6384 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6385 | if (!rbd_img_request_cache) |
| 6386 | return -ENOMEM; |
| 6387 | |
| 6388 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6389 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6390 | if (!rbd_obj_request_cache) |
| 6391 | goto out_err; |
| 6392 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6393 | rbd_assert(!rbd_bio_clone); |
| 6394 | rbd_bio_clone = bioset_create(BIO_POOL_SIZE, 0, 0); |
| 6395 | if (!rbd_bio_clone) |
| 6396 | goto out_err_clone; |
| 6397 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6398 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6399 | |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6400 | out_err_clone: |
| 6401 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6402 | rbd_obj_request_cache = NULL; |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6403 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6404 | kmem_cache_destroy(rbd_img_request_cache); |
| 6405 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6406 | return -ENOMEM; |
| 6407 | } |
| 6408 | |
| 6409 | static void rbd_slab_exit(void) |
| 6410 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6411 | rbd_assert(rbd_obj_request_cache); |
| 6412 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6413 | rbd_obj_request_cache = NULL; |
| 6414 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6415 | rbd_assert(rbd_img_request_cache); |
| 6416 | kmem_cache_destroy(rbd_img_request_cache); |
| 6417 | rbd_img_request_cache = NULL; |
NeilBrown | f856dc3 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 6418 | |
| 6419 | rbd_assert(rbd_bio_clone); |
| 6420 | bioset_free(rbd_bio_clone); |
| 6421 | rbd_bio_clone = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6422 | } |
| 6423 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6424 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6425 | { |
| 6426 | int rc; |
| 6427 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6428 | if (!libceph_compatible(NULL)) { |
| 6429 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6430 | return -EINVAL; |
| 6431 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6432 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6433 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6434 | if (rc) |
| 6435 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6436 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6437 | /* |
| 6438 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6439 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6440 | */ |
| 6441 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6442 | if (!rbd_wq) { |
| 6443 | rc = -ENOMEM; |
| 6444 | goto err_out_slab; |
| 6445 | } |
| 6446 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6447 | if (single_major) { |
| 6448 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6449 | if (rbd_major < 0) { |
| 6450 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6451 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6452 | } |
| 6453 | } |
| 6454 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6455 | rc = rbd_sysfs_init(); |
| 6456 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6457 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6458 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6459 | if (single_major) |
| 6460 | pr_info("loaded (major %d)\n", rbd_major); |
| 6461 | else |
| 6462 | pr_info("loaded\n"); |
| 6463 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6464 | return 0; |
| 6465 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6466 | err_out_blkdev: |
| 6467 | if (single_major) |
| 6468 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6469 | err_out_wq: |
| 6470 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6471 | err_out_slab: |
| 6472 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6473 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6474 | } |
| 6475 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6476 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6477 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6478 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6479 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6480 | if (single_major) |
| 6481 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6482 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6483 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6484 | } |
| 6485 | |
| 6486 | module_init(rbd_init); |
| 6487 | module_exit(rbd_exit); |
| 6488 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6489 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6490 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6491 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6492 | /* following authorship retained from original osdblk.c */ |
| 6493 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6494 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6495 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6496 | MODULE_LICENSE("GPL"); |