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 | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 222 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
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 | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 234 | }; |
| 235 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Writes go through the following state machine to deal with |
| 238 | * layering: |
| 239 | * |
| 240 | * need copyup |
| 241 | * RBD_OBJ_WRITE_GUARD ---------------> RBD_OBJ_WRITE_COPYUP |
| 242 | * | ^ | |
| 243 | * v \------------------------------/ |
| 244 | * done |
| 245 | * ^ |
| 246 | * | |
| 247 | * RBD_OBJ_WRITE_FLAT |
| 248 | * |
| 249 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
| 250 | * there is a parent or not. |
| 251 | */ |
| 252 | enum rbd_obj_write_state { |
| 253 | RBD_OBJ_WRITE_FLAT = 1, |
| 254 | RBD_OBJ_WRITE_GUARD, |
| 255 | RBD_OBJ_WRITE_COPYUP, |
| 256 | }; |
| 257 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 258 | struct rbd_obj_request { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 259 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 260 | u64 offset; /* object start byte */ |
| 261 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 262 | unsigned long flags; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 263 | union { |
| 264 | bool tried_parent; /* for reads */ |
| 265 | enum rbd_obj_write_state write_state; /* for writes */ |
| 266 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 267 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 268 | /* |
| 269 | * An object request associated with an image will have its |
| 270 | * img_data flag set; a standalone object request will not. |
| 271 | * |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 272 | * Finally, an object request for rbd image data will have |
| 273 | * which != BAD_WHICH, and will have a non-null img_request |
| 274 | * pointer. The value of which will be in the range |
| 275 | * 0..(img_request->obj_request_count-1). |
| 276 | */ |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 277 | struct rbd_img_request *img_request; |
| 278 | u64 img_offset; |
| 279 | /* links for img_request->obj_requests list */ |
| 280 | struct list_head links; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | u32 which; /* posn image request list */ |
| 282 | |
| 283 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 284 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 285 | struct ceph_bio_iter bio_pos; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 286 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 287 | struct ceph_bvec_iter bvec_pos; |
| 288 | u32 bvec_count; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 289 | }; |
| 290 | }; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 291 | struct bio_vec *copyup_bvecs; |
| 292 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 293 | |
| 294 | struct ceph_osd_request *osd_req; |
| 295 | |
| 296 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 297 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 298 | |
| 299 | rbd_obj_callback_t callback; |
| 300 | |
| 301 | struct kref kref; |
| 302 | }; |
| 303 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 304 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 305 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 306 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 307 | }; |
| 308 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 309 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 310 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 311 | enum obj_operation_type op_type; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 312 | u64 offset; /* starting image byte offset */ |
| 313 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 314 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 315 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 316 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 317 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 318 | }; |
| 319 | union { |
| 320 | struct request *rq; /* block request */ |
| 321 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 322 | }; |
| 323 | spinlock_t completion_lock;/* protects next_completion */ |
| 324 | u32 next_completion; |
| 325 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 326 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 327 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 328 | |
| 329 | u32 obj_request_count; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 330 | u32 pending_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 331 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 332 | |
| 333 | struct kref kref; |
| 334 | }; |
| 335 | |
| 336 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 337 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 338 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 339 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 340 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 341 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 342 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 343 | enum rbd_watch_state { |
| 344 | RBD_WATCH_STATE_UNREGISTERED, |
| 345 | RBD_WATCH_STATE_REGISTERED, |
| 346 | RBD_WATCH_STATE_ERROR, |
| 347 | }; |
| 348 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 349 | enum rbd_lock_state { |
| 350 | RBD_LOCK_STATE_UNLOCKED, |
| 351 | RBD_LOCK_STATE_LOCKED, |
| 352 | RBD_LOCK_STATE_RELEASING, |
| 353 | }; |
| 354 | |
| 355 | /* WatchNotify::ClientId */ |
| 356 | struct rbd_client_id { |
| 357 | u64 gid; |
| 358 | u64 handle; |
| 359 | }; |
| 360 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 361 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 362 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 363 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 364 | }; |
| 365 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 366 | /* |
| 367 | * a single device |
| 368 | */ |
| 369 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 370 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 371 | |
| 372 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 373 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 374 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 375 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 376 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 377 | struct rbd_client *rbd_client; |
| 378 | |
| 379 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 380 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 381 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 382 | |
| 383 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 384 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 385 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 386 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 387 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 388 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 389 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 390 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 391 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 392 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 393 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 394 | struct mutex watch_mutex; |
| 395 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 396 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 397 | u64 watch_cookie; |
| 398 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 399 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 400 | struct rw_semaphore lock_rwsem; |
| 401 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 402 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 403 | struct rbd_client_id owner_cid; |
| 404 | struct work_struct acquired_lock_work; |
| 405 | struct work_struct released_lock_work; |
| 406 | struct delayed_work lock_dwork; |
| 407 | struct work_struct unlock_work; |
| 408 | wait_queue_head_t lock_waitq; |
| 409 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 410 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 411 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 412 | struct rbd_spec *parent_spec; |
| 413 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 414 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 415 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 416 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 417 | /* Block layer tags. */ |
| 418 | struct blk_mq_tag_set tag_set; |
| 419 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 420 | /* protects updating the header */ |
| 421 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 422 | |
| 423 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 424 | |
| 425 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 426 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 427 | /* sysfs related */ |
| 428 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 429 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 430 | }; |
| 431 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 432 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 433 | * Flag bits for rbd_dev->flags: |
| 434 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 435 | * by rbd_dev->lock |
| 436 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 437 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 438 | enum rbd_dev_flags { |
| 439 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 440 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 441 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 442 | }; |
| 443 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 444 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 445 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 446 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 447 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 448 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 449 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 450 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 451 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 452 | /* Slab caches for frequently-allocated structures */ |
| 453 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 454 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 455 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 456 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 457 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 458 | static DEFINE_IDA(rbd_dev_id_ida); |
| 459 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 460 | static struct workqueue_struct *rbd_wq; |
| 461 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 462 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 463 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 464 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 465 | static bool single_major = true; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 466 | module_param(single_major, bool, S_IRUGO); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 467 | 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] | 468 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 469 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 470 | size_t count); |
| 471 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 472 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 473 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 474 | size_t count); |
| 475 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 476 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 477 | 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] | 478 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 479 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 480 | static int rbd_dev_id_to_minor(int dev_id) |
| 481 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 482 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int minor_to_rbd_dev_id(int minor) |
| 486 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 487 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 488 | } |
| 489 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 490 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 491 | { |
| 492 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 493 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 494 | } |
| 495 | |
| 496 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 497 | { |
| 498 | bool is_lock_owner; |
| 499 | |
| 500 | down_read(&rbd_dev->lock_rwsem); |
| 501 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 502 | up_read(&rbd_dev->lock_rwsem); |
| 503 | return is_lock_owner; |
| 504 | } |
| 505 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 506 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 507 | { |
| 508 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 509 | } |
| 510 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 511 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 512 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 513 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 514 | 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] | 515 | 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] | 516 | |
| 517 | static struct attribute *rbd_bus_attrs[] = { |
| 518 | &bus_attr_add.attr, |
| 519 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 520 | &bus_attr_add_single_major.attr, |
| 521 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 522 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 523 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 524 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 525 | |
| 526 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 527 | struct attribute *attr, int index) |
| 528 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 529 | if (!single_major && |
| 530 | (attr == &bus_attr_add_single_major.attr || |
| 531 | attr == &bus_attr_remove_single_major.attr)) |
| 532 | return 0; |
| 533 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 534 | return attr->mode; |
| 535 | } |
| 536 | |
| 537 | static const struct attribute_group rbd_bus_group = { |
| 538 | .attrs = rbd_bus_attrs, |
| 539 | .is_visible = rbd_bus_is_visible, |
| 540 | }; |
| 541 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 542 | |
| 543 | static struct bus_type rbd_bus_type = { |
| 544 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 545 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 546 | }; |
| 547 | |
| 548 | static void rbd_root_dev_release(struct device *dev) |
| 549 | { |
| 550 | } |
| 551 | |
| 552 | static struct device rbd_root_dev = { |
| 553 | .init_name = "rbd", |
| 554 | .release = rbd_root_dev_release, |
| 555 | }; |
| 556 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 557 | static __printf(2, 3) |
| 558 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 559 | { |
| 560 | struct va_format vaf; |
| 561 | va_list args; |
| 562 | |
| 563 | va_start(args, fmt); |
| 564 | vaf.fmt = fmt; |
| 565 | vaf.va = &args; |
| 566 | |
| 567 | if (!rbd_dev) |
| 568 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 569 | else if (rbd_dev->disk) |
| 570 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 571 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 572 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 573 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 574 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 575 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 576 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 577 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 578 | else /* punt */ |
| 579 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 580 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 581 | va_end(args); |
| 582 | } |
| 583 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 584 | #ifdef RBD_DEBUG |
| 585 | #define rbd_assert(expr) \ |
| 586 | if (unlikely(!(expr))) { \ |
| 587 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 588 | "at line %d:\n\n" \ |
| 589 | "\trbd_assert(%s);\n\n", \ |
| 590 | __func__, __LINE__, #expr); \ |
| 591 | BUG(); \ |
| 592 | } |
| 593 | #else /* !RBD_DEBUG */ |
| 594 | # define rbd_assert(expr) ((void) 0) |
| 595 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 596 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 597 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 598 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 599 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 600 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 601 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 602 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 603 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 604 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 605 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 606 | u8 *order, u64 *snap_size); |
| 607 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 608 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 609 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 610 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 611 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 612 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 613 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 614 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 615 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 616 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 617 | removing = true; |
| 618 | else |
| 619 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 620 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 621 | if (removing) |
| 622 | return -ENOENT; |
| 623 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 624 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 625 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 626 | return 0; |
| 627 | } |
| 628 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 629 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 630 | { |
| 631 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | unsigned long open_count_before; |
| 633 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 634 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 635 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 636 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 637 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 638 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 639 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 642 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 643 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 644 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 646 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 647 | return -EFAULT; |
| 648 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 649 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 650 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 651 | return -EROFS; |
| 652 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 653 | /* Let blkdev_roset() handle it */ |
| 654 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 658 | unsigned int cmd, unsigned long arg) |
| 659 | { |
| 660 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 661 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 662 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 663 | switch (cmd) { |
| 664 | case BLKROSET: |
| 665 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 666 | break; |
| 667 | default: |
| 668 | ret = -ENOTTY; |
| 669 | } |
| 670 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 671 | return ret; |
| 672 | } |
| 673 | |
| 674 | #ifdef CONFIG_COMPAT |
| 675 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 676 | unsigned int cmd, unsigned long arg) |
| 677 | { |
| 678 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 679 | } |
| 680 | #endif /* CONFIG_COMPAT */ |
| 681 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 682 | static const struct block_device_operations rbd_bd_ops = { |
| 683 | .owner = THIS_MODULE, |
| 684 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 685 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 686 | .ioctl = rbd_ioctl, |
| 687 | #ifdef CONFIG_COMPAT |
| 688 | .compat_ioctl = rbd_compat_ioctl, |
| 689 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 690 | }; |
| 691 | |
| 692 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 693 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 694 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 695 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 696 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 697 | { |
| 698 | struct rbd_client *rbdc; |
| 699 | int ret = -ENOMEM; |
| 700 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 701 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 702 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 703 | if (!rbdc) |
| 704 | goto out_opt; |
| 705 | |
| 706 | kref_init(&rbdc->kref); |
| 707 | INIT_LIST_HEAD(&rbdc->node); |
| 708 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 709 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 710 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 711 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 712 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 713 | |
| 714 | ret = ceph_open_session(rbdc->client); |
| 715 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 716 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 717 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 718 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 720 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 722 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 723 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 724 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 725 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 726 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 727 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 728 | kfree(rbdc); |
| 729 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 730 | if (ceph_opts) |
| 731 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 732 | dout("%s: error %d\n", __func__, ret); |
| 733 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 734 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 737 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 738 | { |
| 739 | kref_get(&rbdc->kref); |
| 740 | |
| 741 | return rbdc; |
| 742 | } |
| 743 | |
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 | * Find a ceph client with specific addr and configuration. If |
| 746 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 747 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 748 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 749 | { |
| 750 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 751 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 752 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 753 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 754 | return NULL; |
| 755 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 756 | spin_lock(&rbd_client_list_lock); |
| 757 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 758 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 759 | __rbd_get_client(client_node); |
| 760 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 761 | found = true; |
| 762 | break; |
| 763 | } |
| 764 | } |
| 765 | spin_unlock(&rbd_client_list_lock); |
| 766 | |
| 767 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 771 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 772 | */ |
| 773 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 774 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 775 | Opt_last_int, |
| 776 | /* int args above */ |
| 777 | Opt_last_string, |
| 778 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 779 | Opt_read_only, |
| 780 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 781 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 782 | Opt_exclusive, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 783 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 784 | }; |
| 785 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 786 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 787 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 788 | /* int args above */ |
| 789 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 790 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 791 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 792 | {Opt_read_write, "read_write"}, |
| 793 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 794 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 795 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 796 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 797 | }; |
| 798 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 799 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 800 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 801 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 802 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 803 | bool exclusive; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 804 | }; |
| 805 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 806 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 807 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 808 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 809 | #define RBD_EXCLUSIVE_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 810 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 811 | static int parse_rbd_opts_token(char *c, void *private) |
| 812 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 813 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 814 | substring_t argstr[MAX_OPT_ARGS]; |
| 815 | int token, intval, ret; |
| 816 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 817 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 818 | if (token < Opt_last_int) { |
| 819 | ret = match_int(&argstr[0], &intval); |
| 820 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 821 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 822 | return ret; |
| 823 | } |
| 824 | dout("got int token %d val %d\n", token, intval); |
| 825 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 826 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 827 | } else { |
| 828 | dout("got token %d\n", token); |
| 829 | } |
| 830 | |
| 831 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 832 | case Opt_queue_depth: |
| 833 | if (intval < 1) { |
| 834 | pr_err("queue_depth out of range\n"); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | rbd_opts->queue_depth = intval; |
| 838 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 839 | case Opt_read_only: |
| 840 | rbd_opts->read_only = true; |
| 841 | break; |
| 842 | case Opt_read_write: |
| 843 | rbd_opts->read_only = false; |
| 844 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 845 | case Opt_lock_on_read: |
| 846 | rbd_opts->lock_on_read = true; |
| 847 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 848 | case Opt_exclusive: |
| 849 | rbd_opts->exclusive = true; |
| 850 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 851 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 852 | /* libceph prints "bad option" msg */ |
| 853 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 854 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 855 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 856 | return 0; |
| 857 | } |
| 858 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 859 | static char* obj_op_name(enum obj_operation_type op_type) |
| 860 | { |
| 861 | switch (op_type) { |
| 862 | case OBJ_OP_READ: |
| 863 | return "read"; |
| 864 | case OBJ_OP_WRITE: |
| 865 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 866 | case OBJ_OP_DISCARD: |
| 867 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 868 | default: |
| 869 | return "???"; |
| 870 | } |
| 871 | } |
| 872 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 873 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 874 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 875 | * not exist create it. Either way, ceph_opts is consumed by this |
| 876 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 877 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 878 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 879 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 880 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 881 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 882 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 883 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 884 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 885 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 886 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 887 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 888 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 890 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 895 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 896 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 897 | */ |
| 898 | static void rbd_client_release(struct kref *kref) |
| 899 | { |
| 900 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 901 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 902 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 903 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 904 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 905 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 906 | |
| 907 | ceph_destroy_client(rbdc->client); |
| 908 | kfree(rbdc); |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 913 | * it. |
| 914 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 915 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 917 | if (rbdc) |
| 918 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 921 | static bool rbd_image_format_valid(u32 image_format) |
| 922 | { |
| 923 | return image_format == 1 || image_format == 2; |
| 924 | } |
| 925 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 926 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 927 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 928 | size_t size; |
| 929 | u32 snap_count; |
| 930 | |
| 931 | /* The header has to start with the magic rbd header text */ |
| 932 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 933 | return false; |
| 934 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 935 | /* The bio layer requires at least sector-sized I/O */ |
| 936 | |
| 937 | if (ondisk->options.order < SECTOR_SHIFT) |
| 938 | return false; |
| 939 | |
| 940 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 941 | |
| 942 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 943 | return false; |
| 944 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 945 | /* |
| 946 | * The size of a snapshot header has to fit in a size_t, and |
| 947 | * that limits the number of snapshots. |
| 948 | */ |
| 949 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 950 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 951 | if (snap_count > size / sizeof (__le64)) |
| 952 | return false; |
| 953 | |
| 954 | /* |
| 955 | * Not only that, but the size of the entire the snapshot |
| 956 | * header must also be representable in a size_t. |
| 957 | */ |
| 958 | size -= snap_count * sizeof (__le64); |
| 959 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 960 | return false; |
| 961 | |
| 962 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 963 | } |
| 964 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 965 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 966 | * returns the size of an object in the image |
| 967 | */ |
| 968 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 969 | { |
| 970 | return 1U << header->obj_order; |
| 971 | } |
| 972 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 973 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 974 | { |
| 975 | if (rbd_dev->header.stripe_unit == 0 || |
| 976 | rbd_dev->header.stripe_count == 0) { |
| 977 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 978 | rbd_dev->header.stripe_count = 1; |
| 979 | } |
| 980 | |
| 981 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 982 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 983 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 984 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 985 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 986 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 987 | } |
| 988 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 989 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 990 | * Fill an rbd image header with information from the given format 1 |
| 991 | * on-disk header. |
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 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 994 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 995 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 996 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 997 | bool first_time = header->object_prefix == NULL; |
| 998 | struct ceph_snap_context *snapc; |
| 999 | char *object_prefix = NULL; |
| 1000 | char *snap_names = NULL; |
| 1001 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1002 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1003 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1004 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1005 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1006 | /* Allocate this now to avoid having to handle failure below */ |
| 1007 | |
| 1008 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1009 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1010 | sizeof(ondisk->object_prefix), |
| 1011 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1012 | if (!object_prefix) |
| 1013 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1017 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1018 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1019 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1020 | if (!snapc) |
| 1021 | goto out_err; |
| 1022 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1023 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1025 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1026 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1027 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1028 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | if (snap_names_len > (u64)SIZE_MAX) |
| 1030 | goto out_2big; |
| 1031 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1032 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1033 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1034 | |
| 1035 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1036 | snap_sizes = kmalloc_array(snap_count, |
| 1037 | sizeof(*header->snap_sizes), |
| 1038 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1039 | if (!snap_sizes) |
| 1040 | goto out_err; |
| 1041 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1042 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1043 | * Copy the names, and fill in each snapshot's id |
| 1044 | * and size. |
| 1045 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1046 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1047 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1048 | * snap_names_len bytes beyond the end of the |
| 1049 | * snapshot id array, this memcpy() is safe. |
| 1050 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1051 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1052 | snaps = ondisk->snaps; |
| 1053 | for (i = 0; i < snap_count; i++) { |
| 1054 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1055 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1056 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1057 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1058 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1060 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1061 | if (first_time) { |
| 1062 | header->object_prefix = object_prefix; |
| 1063 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1064 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1065 | } else { |
| 1066 | ceph_put_snap_context(header->snapc); |
| 1067 | kfree(header->snap_names); |
| 1068 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1072 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1073 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | header->snapc = snapc; |
| 1075 | header->snap_names = snap_names; |
| 1076 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1077 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1078 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1079 | out_2big: |
| 1080 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1081 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1082 | kfree(snap_sizes); |
| 1083 | kfree(snap_names); |
| 1084 | ceph_put_snap_context(snapc); |
| 1085 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1086 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1087 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1090 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1091 | { |
| 1092 | const char *snap_name; |
| 1093 | |
| 1094 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1095 | |
| 1096 | /* Skip over names until we find the one we are looking for */ |
| 1097 | |
| 1098 | snap_name = rbd_dev->header.snap_names; |
| 1099 | while (which--) |
| 1100 | snap_name += strlen(snap_name) + 1; |
| 1101 | |
| 1102 | return kstrdup(snap_name, GFP_KERNEL); |
| 1103 | } |
| 1104 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1105 | /* |
| 1106 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1107 | * Note that result is for snapshots in *descending* order. |
| 1108 | */ |
| 1109 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1110 | { |
| 1111 | u64 snap_id1 = *(u64 *)s1; |
| 1112 | u64 snap_id2 = *(u64 *)s2; |
| 1113 | |
| 1114 | if (snap_id1 < snap_id2) |
| 1115 | return 1; |
| 1116 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * Search a snapshot context to see if the given snapshot id is |
| 1121 | * present. |
| 1122 | * |
| 1123 | * Returns the position of the snapshot id in the array if it's found, |
| 1124 | * or BAD_SNAP_INDEX otherwise. |
| 1125 | * |
| 1126 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1127 | * reverse order, highest snapshot id first. |
| 1128 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1129 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1130 | { |
| 1131 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1132 | u64 *found; |
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 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1135 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1136 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1137 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1138 | } |
| 1139 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1140 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1141 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1142 | { |
| 1143 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1144 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1145 | |
| 1146 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1147 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1148 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1149 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1150 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1151 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1152 | } |
| 1153 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1154 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1155 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1156 | if (snap_id == CEPH_NOSNAP) |
| 1157 | return RBD_SNAP_HEAD_NAME; |
| 1158 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1159 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1160 | if (rbd_dev->image_format == 1) |
| 1161 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1162 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1163 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1166 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1167 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1168 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1169 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1170 | if (snap_id == CEPH_NOSNAP) { |
| 1171 | *snap_size = rbd_dev->header.image_size; |
| 1172 | } else if (rbd_dev->image_format == 1) { |
| 1173 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1174 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1175 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1176 | if (which == BAD_SNAP_INDEX) |
| 1177 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1178 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1179 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1180 | } else { |
| 1181 | u64 size = 0; |
| 1182 | int ret; |
| 1183 | |
| 1184 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1185 | if (ret) |
| 1186 | return ret; |
| 1187 | |
| 1188 | *snap_size = size; |
| 1189 | } |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
| 1193 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1194 | u64 *snap_features) |
| 1195 | { |
| 1196 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1197 | if (snap_id == CEPH_NOSNAP) { |
| 1198 | *snap_features = rbd_dev->header.features; |
| 1199 | } else if (rbd_dev->image_format == 1) { |
| 1200 | *snap_features = 0; /* No features for format 1 */ |
| 1201 | } else { |
| 1202 | u64 features = 0; |
| 1203 | int ret; |
| 1204 | |
| 1205 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1206 | if (ret) |
| 1207 | return ret; |
| 1208 | |
| 1209 | *snap_features = features; |
| 1210 | } |
| 1211 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1214 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1215 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1216 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1217 | u64 size = 0; |
| 1218 | u64 features = 0; |
| 1219 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1220 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1221 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1222 | if (ret) |
| 1223 | return ret; |
| 1224 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
| 1228 | rbd_dev->mapping.size = size; |
| 1229 | rbd_dev->mapping.features = features; |
| 1230 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1231 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1234 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1235 | { |
| 1236 | rbd_dev->mapping.size = 0; |
| 1237 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1238 | } |
| 1239 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1240 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1241 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1242 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1243 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1244 | return offset & (segment_size - 1); |
| 1245 | } |
| 1246 | |
| 1247 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1248 | u64 offset, u64 length) |
| 1249 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1250 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1251 | |
| 1252 | offset &= segment_size - 1; |
| 1253 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1254 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1255 | if (offset + length > segment_size) |
| 1256 | length = segment_size - offset; |
| 1257 | |
| 1258 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1261 | static void zero_bvec(struct bio_vec *bv) |
| 1262 | { |
| 1263 | void *buf; |
| 1264 | unsigned long flags; |
| 1265 | |
| 1266 | buf = bvec_kmap_irq(bv, &flags); |
| 1267 | memset(buf, 0, bv->bv_len); |
| 1268 | flush_dcache_page(bv->bv_page); |
| 1269 | bvec_kunmap_irq(buf, &flags); |
| 1270 | } |
| 1271 | |
| 1272 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
| 1273 | { |
| 1274 | struct ceph_bio_iter it = *bio_pos; |
| 1275 | |
| 1276 | ceph_bio_iter_advance(&it, off); |
| 1277 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1278 | zero_bvec(&bv); |
| 1279 | })); |
| 1280 | } |
| 1281 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1282 | static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes) |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1283 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1284 | struct ceph_bvec_iter it = *bvec_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1285 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1286 | ceph_bvec_iter_advance(&it, off); |
| 1287 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1288 | zero_bvec(&bv); |
| 1289 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1293 | * Zero a range in @obj_req data buffer defined by a bio (list) or |
| 1294 | * bio_vec array. |
| 1295 | * |
| 1296 | * @off is relative to the start of the data buffer. |
| 1297 | */ |
| 1298 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1299 | u32 bytes) |
| 1300 | { |
| 1301 | switch (obj_req->type) { |
| 1302 | case OBJ_REQUEST_BIO: |
| 1303 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1304 | break; |
| 1305 | case OBJ_REQUEST_BVECS: |
| 1306 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1307 | break; |
| 1308 | default: |
| 1309 | rbd_assert(0); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | /* |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1314 | * The default/initial value for all object request flags is 0. For |
| 1315 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1316 | * again. |
| 1317 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1318 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1319 | { |
| 1320 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1321 | struct rbd_device *rbd_dev; |
| 1322 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1323 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1324 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1325 | obj_request); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1330 | { |
| 1331 | smp_mb(); |
| 1332 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1333 | } |
| 1334 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1335 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1336 | { |
| 1337 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1338 | struct rbd_device *rbd_dev = NULL; |
| 1339 | |
| 1340 | if (obj_request_img_data_test(obj_request)) |
| 1341 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1342 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1343 | obj_request); |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1348 | { |
| 1349 | smp_mb(); |
| 1350 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1351 | } |
| 1352 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1353 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1354 | { |
| 1355 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1356 | |
| 1357 | return obj_request->img_offset < |
| 1358 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1359 | } |
| 1360 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1361 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1362 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1363 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1364 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1365 | kref_get(&obj_request->kref); |
| 1366 | } |
| 1367 | |
| 1368 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1369 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1370 | { |
| 1371 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1372 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1373 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1374 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1375 | } |
| 1376 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1377 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1378 | { |
| 1379 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1380 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1381 | kref_get(&img_request->kref); |
| 1382 | } |
| 1383 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1384 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1385 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1386 | static void rbd_img_request_destroy(struct kref *kref); |
| 1387 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1388 | { |
| 1389 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1390 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1391 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1392 | if (img_request_child_test(img_request)) |
| 1393 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1394 | else |
| 1395 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1396 | } |
| 1397 | |
| 1398 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1399 | struct rbd_obj_request *obj_request) |
| 1400 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1401 | rbd_assert(obj_request->img_request == NULL); |
| 1402 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1403 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1404 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1405 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1406 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1407 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1408 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1409 | img_request->obj_request_count++; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1410 | img_request->pending_count++; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1411 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1412 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1413 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1417 | struct rbd_obj_request *obj_request) |
| 1418 | { |
| 1419 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1420 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1421 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1422 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1423 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1424 | rbd_assert(img_request->obj_request_count > 0); |
| 1425 | img_request->obj_request_count--; |
| 1426 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1427 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1428 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1429 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1430 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1431 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1432 | rbd_obj_request_put(obj_request); |
| 1433 | } |
| 1434 | |
| 1435 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1436 | { |
| 1437 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1438 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1439 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1440 | case OBJ_REQUEST_BVECS: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1441 | return true; |
| 1442 | default: |
| 1443 | return false; |
| 1444 | } |
| 1445 | } |
| 1446 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1447 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1448 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1449 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1450 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1451 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1452 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1453 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1454 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1455 | obj_request->length, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1456 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1460 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1461 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1462 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1463 | |
| 1464 | /* |
| 1465 | * If no error occurred, compute the aggregate transfer |
| 1466 | * count for the image request. We could instead use |
| 1467 | * atomic64_cmpxchg() to update it as each object request |
| 1468 | * completes; not clear which way is better off hand. |
| 1469 | */ |
| 1470 | if (!img_request->result) { |
| 1471 | struct rbd_obj_request *obj_request; |
| 1472 | u64 xferred = 0; |
| 1473 | |
| 1474 | for_each_obj_request(img_request, obj_request) |
| 1475 | xferred += obj_request->xferred; |
| 1476 | img_request->xferred = xferred; |
| 1477 | } |
| 1478 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1479 | if (img_request->callback) |
| 1480 | img_request->callback(img_request); |
| 1481 | else |
| 1482 | rbd_img_request_put(img_request); |
| 1483 | } |
| 1484 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1485 | /* |
| 1486 | * The default/initial value for all image request flags is 0. Each |
| 1487 | * is conditionally set to 1 at image request initialization time |
| 1488 | * and currently never change thereafter. |
| 1489 | */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1490 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1491 | { |
| 1492 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1493 | smp_mb(); |
| 1494 | } |
| 1495 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1496 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1497 | { |
| 1498 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1499 | smp_mb(); |
| 1500 | } |
| 1501 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1502 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1503 | { |
| 1504 | smp_mb(); |
| 1505 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1506 | } |
| 1507 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1508 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1509 | { |
| 1510 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1511 | smp_mb(); |
| 1512 | } |
| 1513 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1514 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1515 | { |
| 1516 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1517 | smp_mb(); |
| 1518 | } |
| 1519 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1520 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1521 | { |
| 1522 | smp_mb(); |
| 1523 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1524 | } |
| 1525 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1526 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
| 1527 | { |
| 1528 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1529 | |
| 1530 | return !obj_req->offset && |
| 1531 | obj_req->length == rbd_dev->layout.object_size; |
| 1532 | } |
| 1533 | |
| 1534 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
| 1535 | { |
| 1536 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1537 | |
| 1538 | return obj_req->offset + obj_req->length == |
| 1539 | rbd_dev->layout.object_size; |
| 1540 | } |
| 1541 | |
| 1542 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1543 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1544 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1545 | case OBJ_OP_READ: |
| 1546 | return false; |
| 1547 | case OBJ_OP_WRITE: |
| 1548 | case OBJ_OP_DISCARD: |
| 1549 | return true; |
| 1550 | default: |
| 1551 | rbd_assert(0); |
| 1552 | } |
| 1553 | } |
| 1554 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1555 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1556 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1557 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1558 | obj_request->callback); |
Ilya Dryomov | 2e584bc | 2018-01-15 17:24:51 +0100 | [diff] [blame] | 1559 | obj_request->callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1560 | } |
| 1561 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1562 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
| 1563 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1564 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1565 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1566 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1567 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1568 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1569 | osd_req->r_result, obj_req); |
| 1570 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1571 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1572 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1573 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1574 | obj_req->xferred = osd_req->r_result; |
| 1575 | else |
| 1576 | /* |
| 1577 | * Writes aren't allowed to return a data payload. In some |
| 1578 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1579 | * a stat response makes it through, but we don't care. |
| 1580 | */ |
| 1581 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1582 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1583 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1584 | } |
| 1585 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1586 | 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] | 1587 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1588 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1589 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1590 | rbd_assert(obj_request_img_data_test(obj_request)); |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1591 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1592 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1596 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1597 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1598 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1599 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Deepa Dinamani | 1134e09 | 2017-05-08 15:59:19 -0700 | [diff] [blame] | 1600 | ktime_get_real_ts(&osd_req->r_mtime); |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1601 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1602 | } |
| 1603 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1604 | static struct ceph_osd_request * |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1605 | rbd_osd_req_create(struct rbd_obj_request *obj_req, unsigned int num_ops) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1606 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1607 | struct rbd_img_request *img_req = obj_req->img_request; |
| 1608 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1609 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1610 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1611 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1612 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1613 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1614 | req = ceph_osdc_alloc_request(osdc, |
| 1615 | (rbd_img_is_write(img_req) ? img_req->snapc : NULL), |
| 1616 | num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1617 | if (!req) |
| 1618 | return NULL; |
| 1619 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1620 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1621 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1622 | |
| 1623 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1624 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1625 | rbd_dev->header.object_prefix, obj_req->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1626 | goto err_req; |
| 1627 | |
| 1628 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1629 | goto err_req; |
| 1630 | |
| 1631 | return req; |
| 1632 | |
| 1633 | err_req: |
| 1634 | ceph_osdc_put_request(req); |
| 1635 | return NULL; |
| 1636 | } |
| 1637 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1638 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1639 | { |
| 1640 | ceph_osdc_put_request(osd_req); |
| 1641 | } |
| 1642 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1643 | static struct rbd_obj_request * |
| 1644 | rbd_obj_request_create(enum obj_request_type type) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1645 | { |
| 1646 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1647 | |
| 1648 | rbd_assert(obj_request_type_valid(type)); |
| 1649 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1650 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1651 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1652 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1653 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1654 | obj_request->which = BAD_WHICH; |
| 1655 | obj_request->type = type; |
| 1656 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1657 | kref_init(&obj_request->kref); |
| 1658 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1659 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1660 | return obj_request; |
| 1661 | } |
| 1662 | |
| 1663 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1664 | { |
| 1665 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1666 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1667 | |
| 1668 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1669 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1670 | dout("%s: obj %p\n", __func__, obj_request); |
| 1671 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1672 | rbd_assert(obj_request->img_request == NULL); |
| 1673 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1674 | |
| 1675 | if (obj_request->osd_req) |
| 1676 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1677 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1678 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1679 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1680 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1681 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1682 | break; /* Nothing to do */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1683 | default: |
| 1684 | rbd_assert(0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1685 | } |
| 1686 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1687 | if (obj_request->copyup_bvecs) { |
| 1688 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1689 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1690 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1691 | } |
| 1692 | kfree(obj_request->copyup_bvecs); |
| 1693 | } |
Ilya Dryomov | f9dcbc4 | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1694 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1695 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1696 | } |
| 1697 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1698 | /* It's OK to call this for a device with no parent */ |
| 1699 | |
| 1700 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1701 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1702 | { |
| 1703 | rbd_dev_remove_parent(rbd_dev); |
| 1704 | rbd_spec_put(rbd_dev->parent_spec); |
| 1705 | rbd_dev->parent_spec = NULL; |
| 1706 | rbd_dev->parent_overlap = 0; |
| 1707 | } |
| 1708 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1709 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1710 | * Parent image reference counting is used to determine when an |
| 1711 | * image's parent fields can be safely torn down--after there are no |
| 1712 | * more in-flight requests to the parent image. When the last |
| 1713 | * reference is dropped, cleaning them up is safe. |
| 1714 | */ |
| 1715 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1716 | { |
| 1717 | int counter; |
| 1718 | |
| 1719 | if (!rbd_dev->parent_spec) |
| 1720 | return; |
| 1721 | |
| 1722 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1723 | if (counter > 0) |
| 1724 | return; |
| 1725 | |
| 1726 | /* Last reference; clean up parent data structures */ |
| 1727 | |
| 1728 | if (!counter) |
| 1729 | rbd_dev_unparent(rbd_dev); |
| 1730 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1731 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | /* |
| 1735 | * If an image has a non-zero parent overlap, get a reference to its |
| 1736 | * parent. |
| 1737 | * |
| 1738 | * Returns true if the rbd device has a parent with a non-zero |
| 1739 | * overlap and a reference for it was successfully taken, or |
| 1740 | * false otherwise. |
| 1741 | */ |
| 1742 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1743 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1744 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1745 | |
| 1746 | if (!rbd_dev->parent_spec) |
| 1747 | return false; |
| 1748 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1749 | down_read(&rbd_dev->header_rwsem); |
| 1750 | if (rbd_dev->parent_overlap) |
| 1751 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1752 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1753 | |
| 1754 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1755 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1756 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1757 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1758 | } |
| 1759 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1760 | /* |
| 1761 | * Caller is responsible for filling in the list of object requests |
| 1762 | * that comprises the image request, and the Linux request pointer |
| 1763 | * (if there is one). |
| 1764 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1765 | static struct rbd_img_request *rbd_img_request_create( |
| 1766 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1767 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1768 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1769 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1770 | { |
| 1771 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1772 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1773 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1774 | if (!img_request) |
| 1775 | return NULL; |
| 1776 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1777 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1778 | img_request->op_type = op_type; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1779 | img_request->offset = offset; |
| 1780 | img_request->length = length; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1781 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1782 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1783 | else |
| 1784 | img_request->snapc = snapc; |
| 1785 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1786 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1787 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1788 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1789 | spin_lock_init(&img_request->completion_lock); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1790 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 1791 | kref_init(&img_request->kref); |
| 1792 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1793 | 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] | 1794 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1795 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1796 | return img_request; |
| 1797 | } |
| 1798 | |
| 1799 | static void rbd_img_request_destroy(struct kref *kref) |
| 1800 | { |
| 1801 | struct rbd_img_request *img_request; |
| 1802 | struct rbd_obj_request *obj_request; |
| 1803 | struct rbd_obj_request *next_obj_request; |
| 1804 | |
| 1805 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1806 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1807 | dout("%s: img %p\n", __func__, img_request); |
| 1808 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1809 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1810 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1811 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1812 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1813 | if (img_request_layered_test(img_request)) { |
| 1814 | img_request_layered_clear(img_request); |
| 1815 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1816 | } |
| 1817 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1818 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1819 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1820 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1821 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1822 | } |
| 1823 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1824 | static struct rbd_img_request *rbd_parent_request_create( |
| 1825 | struct rbd_obj_request *obj_request, |
| 1826 | u64 img_offset, u64 length) |
| 1827 | { |
| 1828 | struct rbd_img_request *parent_request; |
| 1829 | struct rbd_device *rbd_dev; |
| 1830 | |
| 1831 | rbd_assert(obj_request->img_request); |
| 1832 | rbd_dev = obj_request->img_request->rbd_dev; |
| 1833 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1834 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1835 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1836 | if (!parent_request) |
| 1837 | return NULL; |
| 1838 | |
| 1839 | img_request_child_set(parent_request); |
| 1840 | rbd_obj_request_get(obj_request); |
| 1841 | parent_request->obj_request = obj_request; |
| 1842 | |
| 1843 | return parent_request; |
| 1844 | } |
| 1845 | |
| 1846 | static void rbd_parent_request_destroy(struct kref *kref) |
| 1847 | { |
| 1848 | struct rbd_img_request *parent_request; |
| 1849 | struct rbd_obj_request *orig_request; |
| 1850 | |
| 1851 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 1852 | orig_request = parent_request->obj_request; |
| 1853 | |
| 1854 | parent_request->obj_request = NULL; |
| 1855 | rbd_obj_request_put(orig_request); |
| 1856 | img_request_child_clear(parent_request); |
| 1857 | |
| 1858 | rbd_img_request_destroy(kref); |
| 1859 | } |
| 1860 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1861 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 1862 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1863 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1864 | unsigned int xferred; |
| 1865 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1866 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1867 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1868 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1869 | img_request = obj_request->img_request; |
| 1870 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1871 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 1872 | xferred = (unsigned int)obj_request->xferred; |
| 1873 | result = obj_request->result; |
| 1874 | if (result) { |
| 1875 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 1876 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1877 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1878 | obj_op_name(img_request->op_type), obj_request->length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1879 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1880 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1881 | result, xferred); |
| 1882 | if (!img_request->result) |
| 1883 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 1884 | /* |
| 1885 | * Need to end I/O on the entire obj_request worth of |
| 1886 | * bytes in case of error. |
| 1887 | */ |
| 1888 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1889 | } |
| 1890 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1891 | if (img_request_child_test(img_request)) { |
| 1892 | rbd_assert(img_request->obj_request != NULL); |
| 1893 | more = obj_request->which < img_request->obj_request_count - 1; |
| 1894 | } else { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1895 | blk_status_t status = errno_to_blk_status(result); |
| 1896 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1897 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1898 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1899 | more = blk_update_request(img_request->rq, status, xferred); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1900 | if (!more) |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1901 | __blk_mq_end_request(img_request->rq, status); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1902 | } |
| 1903 | |
| 1904 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1905 | } |
| 1906 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1907 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 1908 | { |
| 1909 | struct rbd_img_request *img_request; |
| 1910 | u32 which = obj_request->which; |
| 1911 | bool more = true; |
| 1912 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1913 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1914 | img_request = obj_request->img_request; |
| 1915 | |
| 1916 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 1917 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1918 | rbd_assert(img_request->obj_request_count > 0); |
| 1919 | rbd_assert(which != BAD_WHICH); |
| 1920 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1921 | |
| 1922 | spin_lock_irq(&img_request->completion_lock); |
| 1923 | if (which != img_request->next_completion) |
| 1924 | goto out; |
| 1925 | |
| 1926 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1927 | rbd_assert(more); |
| 1928 | rbd_assert(which < img_request->obj_request_count); |
| 1929 | |
| 1930 | if (!obj_request_done_test(obj_request)) |
| 1931 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1932 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1933 | which++; |
| 1934 | } |
| 1935 | |
| 1936 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 1937 | img_request->next_completion = which; |
| 1938 | out: |
| 1939 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1940 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1941 | |
| 1942 | if (!more) |
| 1943 | rbd_img_request_complete(img_request); |
| 1944 | } |
| 1945 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1946 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1947 | { |
| 1948 | switch (obj_req->type) { |
| 1949 | case OBJ_REQUEST_BIO: |
| 1950 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1951 | &obj_req->bio_pos, |
| 1952 | obj_req->length); |
| 1953 | break; |
| 1954 | case OBJ_REQUEST_BVECS: |
| 1955 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
| 1956 | obj_req->length); |
| 1957 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1958 | &obj_req->bvec_pos); |
| 1959 | break; |
| 1960 | default: |
| 1961 | rbd_assert(0); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1966 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1967 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1968 | if (!obj_req->osd_req) |
| 1969 | return -ENOMEM; |
| 1970 | |
| 1971 | osd_req_op_extent_init(obj_req->osd_req, 0, CEPH_OSD_OP_READ, |
| 1972 | obj_req->offset, obj_req->length, 0, 0); |
| 1973 | rbd_osd_req_setup_data(obj_req, 0); |
| 1974 | |
| 1975 | rbd_osd_req_format_read(obj_req); |
| 1976 | return 0; |
| 1977 | } |
| 1978 | |
| 1979 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1980 | unsigned int which) |
| 1981 | { |
| 1982 | struct page **pages; |
| 1983 | |
| 1984 | /* |
| 1985 | * The response data for a STAT call consists of: |
| 1986 | * le64 length; |
| 1987 | * struct { |
| 1988 | * le32 tv_sec; |
| 1989 | * le32 tv_nsec; |
| 1990 | * } mtime; |
| 1991 | */ |
| 1992 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1993 | if (IS_ERR(pages)) |
| 1994 | return PTR_ERR(pages); |
| 1995 | |
| 1996 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1997 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1998 | 8 + sizeof(struct ceph_timespec), |
| 1999 | 0, false, true); |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 2004 | unsigned int which) |
| 2005 | { |
| 2006 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2007 | u16 opcode; |
| 2008 | |
| 2009 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 2010 | rbd_dev->layout.object_size, |
| 2011 | rbd_dev->layout.object_size); |
| 2012 | |
| 2013 | if (rbd_obj_is_entire(obj_req)) |
| 2014 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2015 | else |
| 2016 | opcode = CEPH_OSD_OP_WRITE; |
| 2017 | |
| 2018 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
| 2019 | obj_req->offset, obj_req->length, 0, 0); |
| 2020 | rbd_osd_req_setup_data(obj_req, which++); |
| 2021 | |
| 2022 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 2023 | rbd_osd_req_format_write(obj_req); |
| 2024 | } |
| 2025 | |
| 2026 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
| 2027 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2028 | unsigned int num_osd_ops, which = 0; |
| 2029 | int ret; |
| 2030 | |
| 2031 | if (obj_request_overlaps_parent(obj_req)) { |
| 2032 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2033 | num_osd_ops = 3; /* stat + setallochint + write/writefull */ |
| 2034 | } else { |
| 2035 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2036 | num_osd_ops = 2; /* setallochint + write/writefull */ |
| 2037 | } |
| 2038 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2039 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2040 | if (!obj_req->osd_req) |
| 2041 | return -ENOMEM; |
| 2042 | |
| 2043 | if (obj_request_overlaps_parent(obj_req)) { |
| 2044 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 2045 | if (ret) |
| 2046 | return ret; |
| 2047 | } |
| 2048 | |
| 2049 | __rbd_obj_setup_write(obj_req, which); |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
| 2053 | static void __rbd_obj_setup_discard(struct rbd_obj_request *obj_req, |
| 2054 | unsigned int which) |
| 2055 | { |
| 2056 | u16 opcode; |
| 2057 | |
| 2058 | if (rbd_obj_is_entire(obj_req)) { |
| 2059 | if (obj_request_overlaps_parent(obj_req)) { |
| 2060 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2061 | } else { |
| 2062 | osd_req_op_init(obj_req->osd_req, which++, |
| 2063 | CEPH_OSD_OP_DELETE, 0); |
| 2064 | opcode = 0; |
| 2065 | } |
| 2066 | } else if (rbd_obj_is_tail(obj_req)) { |
| 2067 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2068 | } else { |
| 2069 | opcode = CEPH_OSD_OP_ZERO; |
| 2070 | } |
| 2071 | |
| 2072 | if (opcode) |
| 2073 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
| 2074 | obj_req->offset, obj_req->length, |
| 2075 | 0, 0); |
| 2076 | |
| 2077 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 2078 | rbd_osd_req_format_write(obj_req); |
| 2079 | } |
| 2080 | |
| 2081 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 2082 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2083 | unsigned int num_osd_ops, which = 0; |
| 2084 | int ret; |
| 2085 | |
| 2086 | if (rbd_obj_is_entire(obj_req)) { |
| 2087 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2088 | num_osd_ops = 1; /* truncate/delete */ |
| 2089 | } else { |
| 2090 | if (obj_request_overlaps_parent(obj_req)) { |
| 2091 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2092 | num_osd_ops = 2; /* stat + truncate/zero */ |
| 2093 | } else { |
| 2094 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2095 | num_osd_ops = 1; /* truncate/zero */ |
| 2096 | } |
| 2097 | } |
| 2098 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2099 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2100 | if (!obj_req->osd_req) |
| 2101 | return -ENOMEM; |
| 2102 | |
| 2103 | if (!rbd_obj_is_entire(obj_req) && |
| 2104 | obj_request_overlaps_parent(obj_req)) { |
| 2105 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 2106 | if (ret) |
| 2107 | return ret; |
| 2108 | } |
| 2109 | |
| 2110 | __rbd_obj_setup_discard(obj_req, which); |
| 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * For each object request in @img_req, allocate an OSD request, add |
| 2116 | * individual OSD ops and prepare them for submission. The number of |
| 2117 | * OSD ops depends on op_type and the overlap point (if any). |
| 2118 | */ |
| 2119 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 2120 | { |
| 2121 | struct rbd_obj_request *obj_req; |
| 2122 | int ret; |
| 2123 | |
| 2124 | for_each_obj_request(img_req, obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2125 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2126 | case OBJ_OP_READ: |
| 2127 | ret = rbd_obj_setup_read(obj_req); |
| 2128 | break; |
| 2129 | case OBJ_OP_WRITE: |
| 2130 | ret = rbd_obj_setup_write(obj_req); |
| 2131 | break; |
| 2132 | case OBJ_OP_DISCARD: |
| 2133 | ret = rbd_obj_setup_discard(obj_req); |
| 2134 | break; |
| 2135 | default: |
| 2136 | rbd_assert(0); |
| 2137 | } |
| 2138 | if (ret) |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2145 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2146 | * Split up an image request into one or more object requests, each |
| 2147 | * to a different object. The "type" parameter indicates whether |
| 2148 | * "data_desc" is the pointer to the head of a list of bio |
| 2149 | * structures, or the base of a page array. In either case this |
| 2150 | * function assumes data_desc describes memory sufficient to hold |
| 2151 | * all data described by the image request. |
| 2152 | */ |
| 2153 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2154 | enum obj_request_type type, |
| 2155 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2156 | { |
| 2157 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2158 | struct rbd_obj_request *obj_request = NULL; |
| 2159 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 2160 | struct ceph_bio_iter bio_it; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2161 | struct ceph_bvec_iter bvec_it; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2162 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2163 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2164 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2165 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2166 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2167 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2168 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2169 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2170 | rbd_assert(resid > 0); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2171 | |
| 2172 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 2173 | bio_it = *(struct ceph_bio_iter *)data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2174 | rbd_assert(img_offset == |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 2175 | bio_it.iter.bi_sector << SECTOR_SHIFT); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2176 | } else if (type == OBJ_REQUEST_BVECS) { |
| 2177 | bvec_it = *(struct ceph_bvec_iter *)data_desc; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2178 | } |
| 2179 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2180 | while (resid) { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2181 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2182 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2183 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2184 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2185 | obj_request = rbd_obj_request_create(type); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2186 | if (!obj_request) |
| 2187 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2188 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 2189 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 2190 | obj_request->offset = offset; |
| 2191 | obj_request->length = length; |
| 2192 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2193 | /* |
| 2194 | * set obj_request->img_request before creating the |
| 2195 | * osd_request so that it gets the right snapc |
| 2196 | */ |
| 2197 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2198 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2199 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 2200 | obj_request->bio_pos = bio_it; |
| 2201 | ceph_bio_iter_advance(&bio_it, length); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2202 | } else if (type == OBJ_REQUEST_BVECS) { |
| 2203 | obj_request->bvec_pos = bvec_it; |
| 2204 | ceph_bvec_iter_shorten(&obj_request->bvec_pos, length); |
| 2205 | ceph_bvec_iter_advance(&bvec_it, length); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2206 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2207 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2208 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2209 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2210 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2211 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2212 | resid -= length; |
| 2213 | } |
| 2214 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2215 | return __rbd_img_fill_request(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2216 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2217 | out_unwind: |
| 2218 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2219 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2220 | |
| 2221 | return -ENOMEM; |
| 2222 | } |
| 2223 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2224 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2225 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2226 | struct rbd_obj_request *obj_request; |
| 2227 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2228 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2229 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2230 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2231 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2232 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2233 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2234 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2235 | } |
| 2236 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2237 | static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req, |
| 2238 | u64 img_offset, u32 bytes) |
| 2239 | { |
| 2240 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2241 | struct rbd_img_request *child_img_req; |
| 2242 | int ret; |
| 2243 | |
| 2244 | child_img_req = rbd_parent_request_create(obj_req, img_offset, bytes); |
| 2245 | if (!child_img_req) |
| 2246 | return -ENOMEM; |
| 2247 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2248 | if (!rbd_img_is_write(img_req)) { |
| 2249 | switch (obj_req->type) { |
| 2250 | case OBJ_REQUEST_BIO: |
| 2251 | ret = rbd_img_request_fill(child_img_req, |
| 2252 | OBJ_REQUEST_BIO, |
| 2253 | &obj_req->bio_pos); |
| 2254 | break; |
| 2255 | case OBJ_REQUEST_BVECS: |
| 2256 | ret = rbd_img_request_fill(child_img_req, |
| 2257 | OBJ_REQUEST_BVECS, |
| 2258 | &obj_req->bvec_pos); |
| 2259 | break; |
| 2260 | default: |
| 2261 | rbd_assert(0); |
| 2262 | } |
| 2263 | } else { |
| 2264 | struct ceph_bvec_iter it = { |
| 2265 | .bvecs = obj_req->copyup_bvecs, |
| 2266 | .iter = { .bi_size = bytes }, |
| 2267 | }; |
| 2268 | |
| 2269 | ret = rbd_img_request_fill(child_img_req, OBJ_REQUEST_BVECS, |
| 2270 | &it); |
| 2271 | } |
| 2272 | if (ret) { |
| 2273 | rbd_img_request_put(child_img_req); |
| 2274 | return ret; |
| 2275 | } |
| 2276 | |
| 2277 | rbd_img_request_submit(child_img_req); |
| 2278 | return 0; |
| 2279 | } |
| 2280 | |
| 2281 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2282 | { |
| 2283 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2284 | int ret; |
| 2285 | |
| 2286 | if (obj_req->result == -ENOENT && |
| 2287 | obj_req->img_offset < rbd_dev->parent_overlap && |
| 2288 | !obj_req->tried_parent) { |
| 2289 | u64 obj_overlap = min(obj_req->length, |
| 2290 | rbd_dev->parent_overlap - obj_req->img_offset); |
| 2291 | |
| 2292 | obj_req->tried_parent = true; |
| 2293 | ret = rbd_obj_read_from_parent(obj_req, obj_req->img_offset, |
| 2294 | obj_overlap); |
| 2295 | if (ret) { |
| 2296 | obj_req->result = ret; |
| 2297 | return true; |
| 2298 | } |
| 2299 | return false; |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2304 | * length of the request. A short read also implies zero-fill |
| 2305 | * to the end of the request. In both cases we update xferred |
| 2306 | * count to indicate the whole request was satisfied. |
| 2307 | */ |
| 2308 | if (obj_req->result == -ENOENT || |
| 2309 | (!obj_req->result && obj_req->xferred < obj_req->length)) { |
| 2310 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2311 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
| 2312 | obj_req->length - obj_req->xferred); |
| 2313 | obj_req->result = 0; |
| 2314 | obj_req->xferred = obj_req->length; |
| 2315 | } |
| 2316 | |
| 2317 | return true; |
| 2318 | } |
| 2319 | |
| 2320 | /* |
| 2321 | * copyup_bvecs pages are never highmem pages |
| 2322 | */ |
| 2323 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2324 | { |
| 2325 | struct ceph_bvec_iter it = { |
| 2326 | .bvecs = bvecs, |
| 2327 | .iter = { .bi_size = bytes }, |
| 2328 | }; |
| 2329 | |
| 2330 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2331 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2332 | bv.bv_len)) |
| 2333 | return false; |
| 2334 | })); |
| 2335 | return true; |
| 2336 | } |
| 2337 | |
| 2338 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2339 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2340 | unsigned int num_osd_ops = obj_req->osd_req->r_num_ops; |
| 2341 | |
| 2342 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2343 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
| 2344 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2345 | |
| 2346 | /* |
| 2347 | * Create a copyup request with the same number of OSD ops as |
| 2348 | * the original request. The original request was stat + op(s), |
| 2349 | * the new copyup request will be copyup + the same op(s). |
| 2350 | */ |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2351 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2352 | if (!obj_req->osd_req) |
| 2353 | return -ENOMEM; |
| 2354 | |
| 2355 | /* |
| 2356 | * Only send non-zero copyup data to save some I/O and network |
| 2357 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2358 | * existing. |
| 2359 | */ |
| 2360 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2361 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2362 | bytes = 0; |
| 2363 | } |
| 2364 | |
| 2365 | osd_req_op_cls_init(obj_req->osd_req, 0, CEPH_OSD_OP_CALL, "rbd", |
| 2366 | "copyup"); |
| 2367 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
| 2368 | obj_req->copyup_bvecs, bytes); |
| 2369 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2370 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2371 | case OBJ_OP_WRITE: |
| 2372 | __rbd_obj_setup_write(obj_req, 1); |
| 2373 | break; |
| 2374 | case OBJ_OP_DISCARD: |
| 2375 | rbd_assert(!rbd_obj_is_entire(obj_req)); |
| 2376 | __rbd_obj_setup_discard(obj_req, 1); |
| 2377 | break; |
| 2378 | default: |
| 2379 | rbd_assert(0); |
| 2380 | } |
| 2381 | |
| 2382 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2383 | return 0; |
| 2384 | } |
| 2385 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2386 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2387 | { |
| 2388 | u32 i; |
| 2389 | |
| 2390 | rbd_assert(!obj_req->copyup_bvecs); |
| 2391 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2392 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2393 | sizeof(*obj_req->copyup_bvecs), |
| 2394 | GFP_NOIO); |
| 2395 | if (!obj_req->copyup_bvecs) |
| 2396 | return -ENOMEM; |
| 2397 | |
| 2398 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2399 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2400 | |
| 2401 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2402 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2403 | return -ENOMEM; |
| 2404 | |
| 2405 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2406 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2407 | obj_overlap -= len; |
| 2408 | } |
| 2409 | |
| 2410 | rbd_assert(!obj_overlap); |
| 2411 | return 0; |
| 2412 | } |
| 2413 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2414 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2415 | { |
| 2416 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2417 | u64 img_offset; |
| 2418 | u64 obj_overlap; |
| 2419 | int ret; |
| 2420 | |
| 2421 | if (!obj_request_overlaps_parent(obj_req)) { |
| 2422 | /* |
| 2423 | * The overlap has become 0 (most likely because the |
| 2424 | * image has been flattened). Use rbd_obj_issue_copyup() |
| 2425 | * to re-submit the original write request -- the copyup |
| 2426 | * operation itself will be a no-op, since someone must |
| 2427 | * have populated the child object while we weren't |
| 2428 | * looking. Move to WRITE_FLAT state as we'll be done |
| 2429 | * with the operation once the null copyup completes. |
| 2430 | */ |
| 2431 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2432 | return rbd_obj_issue_copyup(obj_req, 0); |
| 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Determine the byte range covered by the object in the |
| 2437 | * child image to which the original request was to be sent. |
| 2438 | */ |
| 2439 | img_offset = obj_req->img_offset - obj_req->offset; |
| 2440 | obj_overlap = rbd_dev->layout.object_size; |
| 2441 | |
| 2442 | /* |
| 2443 | * There is no defined parent data beyond the parent |
| 2444 | * overlap, so limit what we read at that boundary if |
| 2445 | * necessary. |
| 2446 | */ |
| 2447 | if (img_offset + obj_overlap > rbd_dev->parent_overlap) { |
| 2448 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2449 | obj_overlap = rbd_dev->parent_overlap - img_offset; |
| 2450 | } |
| 2451 | |
| 2452 | ret = setup_copyup_bvecs(obj_req, obj_overlap); |
| 2453 | if (ret) |
| 2454 | return ret; |
| 2455 | |
| 2456 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
| 2457 | return rbd_obj_read_from_parent(obj_req, img_offset, obj_overlap); |
| 2458 | } |
| 2459 | |
| 2460 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2461 | { |
| 2462 | int ret; |
| 2463 | |
| 2464 | again: |
| 2465 | switch (obj_req->write_state) { |
| 2466 | case RBD_OBJ_WRITE_GUARD: |
| 2467 | rbd_assert(!obj_req->xferred); |
| 2468 | if (obj_req->result == -ENOENT) { |
| 2469 | /* |
| 2470 | * The target object doesn't exist. Read the data for |
| 2471 | * the entire target object up to the overlap point (if |
| 2472 | * any) from the parent, so we can use it for a copyup. |
| 2473 | */ |
| 2474 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2475 | if (ret) { |
| 2476 | obj_req->result = ret; |
| 2477 | return true; |
| 2478 | } |
| 2479 | return false; |
| 2480 | } |
| 2481 | /* fall through */ |
| 2482 | case RBD_OBJ_WRITE_FLAT: |
| 2483 | if (!obj_req->result) |
| 2484 | /* |
| 2485 | * There is no such thing as a successful short |
| 2486 | * write -- indicate the whole request was satisfied. |
| 2487 | */ |
| 2488 | obj_req->xferred = obj_req->length; |
| 2489 | return true; |
| 2490 | case RBD_OBJ_WRITE_COPYUP: |
| 2491 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2492 | if (obj_req->result) |
| 2493 | goto again; |
| 2494 | |
| 2495 | rbd_assert(obj_req->xferred); |
| 2496 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2497 | if (ret) { |
| 2498 | obj_req->result = ret; |
| 2499 | return true; |
| 2500 | } |
| 2501 | return false; |
| 2502 | default: |
| 2503 | rbd_assert(0); |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | /* |
| 2508 | * Returns true if @obj_req is completed, or false otherwise. |
| 2509 | */ |
| 2510 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2511 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2512 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2513 | case OBJ_OP_READ: |
| 2514 | return rbd_obj_handle_read(obj_req); |
| 2515 | case OBJ_OP_WRITE: |
| 2516 | return rbd_obj_handle_write(obj_req); |
| 2517 | case OBJ_OP_DISCARD: |
| 2518 | if (rbd_obj_handle_write(obj_req)) { |
| 2519 | /* |
| 2520 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2521 | * a non-existent object is not a problem. |
| 2522 | */ |
| 2523 | if (obj_req->result == -ENOENT) { |
| 2524 | obj_req->result = 0; |
| 2525 | obj_req->xferred = obj_req->length; |
| 2526 | } |
| 2527 | return true; |
| 2528 | } |
| 2529 | return false; |
| 2530 | default: |
| 2531 | rbd_assert(0); |
| 2532 | } |
| 2533 | } |
| 2534 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2535 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2536 | { |
| 2537 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2538 | |
| 2539 | rbd_assert((!obj_req->result && |
| 2540 | obj_req->xferred == obj_req->length) || |
| 2541 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2542 | if (!obj_req->result) { |
| 2543 | img_req->xferred += obj_req->xferred; |
| 2544 | return; |
| 2545 | } |
| 2546 | |
| 2547 | rbd_warn(img_req->rbd_dev, |
| 2548 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
| 2549 | obj_op_name(img_req->op_type), obj_req->object_no, |
| 2550 | obj_req->offset, obj_req->length, obj_req->result, |
| 2551 | obj_req->xferred); |
| 2552 | if (!img_req->result) { |
| 2553 | img_req->result = obj_req->result; |
| 2554 | img_req->xferred = 0; |
| 2555 | } |
| 2556 | } |
| 2557 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2558 | static void rbd_img_end_child_request(struct rbd_img_request *img_req) |
| 2559 | { |
| 2560 | struct rbd_obj_request *obj_req = img_req->obj_request; |
| 2561 | |
| 2562 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2563 | |
| 2564 | obj_req->result = img_req->result; |
| 2565 | obj_req->xferred = img_req->xferred; |
| 2566 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2567 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2568 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2569 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2570 | { |
| 2571 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2572 | rbd_assert((!img_req->result && |
| 2573 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2574 | (img_req->result < 0 && !img_req->xferred)); |
| 2575 | |
| 2576 | blk_mq_end_request(img_req->rq, |
| 2577 | errno_to_blk_status(img_req->result)); |
| 2578 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2582 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2583 | struct rbd_img_request *img_req; |
| 2584 | |
| 2585 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2586 | if (!__rbd_obj_handle_request(obj_req)) |
| 2587 | return; |
| 2588 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2589 | img_req = obj_req->img_request; |
| 2590 | spin_lock(&img_req->completion_lock); |
| 2591 | rbd_obj_end_request(obj_req); |
| 2592 | rbd_assert(img_req->pending_count); |
| 2593 | if (--img_req->pending_count) { |
| 2594 | spin_unlock(&img_req->completion_lock); |
| 2595 | return; |
| 2596 | } |
| 2597 | |
| 2598 | spin_unlock(&img_req->completion_lock); |
| 2599 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2600 | obj_req = img_req->obj_request; |
| 2601 | rbd_img_end_child_request(img_req); |
| 2602 | goto again; |
| 2603 | } |
| 2604 | rbd_img_end_request(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2605 | } |
| 2606 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2607 | static const struct rbd_client_id rbd_empty_cid; |
| 2608 | |
| 2609 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2610 | const struct rbd_client_id *rhs) |
| 2611 | { |
| 2612 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2613 | } |
| 2614 | |
| 2615 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2616 | { |
| 2617 | struct rbd_client_id cid; |
| 2618 | |
| 2619 | mutex_lock(&rbd_dev->watch_mutex); |
| 2620 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2621 | cid.handle = rbd_dev->watch_cookie; |
| 2622 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2623 | return cid; |
| 2624 | } |
| 2625 | |
| 2626 | /* |
| 2627 | * lock_rwsem must be held for write |
| 2628 | */ |
| 2629 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2630 | const struct rbd_client_id *cid) |
| 2631 | { |
| 2632 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2633 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2634 | cid->gid, cid->handle); |
| 2635 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2636 | } |
| 2637 | |
| 2638 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2639 | { |
| 2640 | mutex_lock(&rbd_dev->watch_mutex); |
| 2641 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2642 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2643 | } |
| 2644 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2645 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2646 | { |
| 2647 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2648 | |
| 2649 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2650 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2651 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2652 | } |
| 2653 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2654 | /* |
| 2655 | * lock_rwsem must be held for write |
| 2656 | */ |
| 2657 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2658 | { |
| 2659 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2660 | char cookie[32]; |
| 2661 | int ret; |
| 2662 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2663 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2664 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2665 | |
| 2666 | format_lock_cookie(rbd_dev, cookie); |
| 2667 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2668 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2669 | RBD_LOCK_TAG, "", 0); |
| 2670 | if (ret) |
| 2671 | return ret; |
| 2672 | |
| 2673 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2674 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | /* |
| 2679 | * lock_rwsem must be held for write |
| 2680 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2681 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2682 | { |
| 2683 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2684 | int ret; |
| 2685 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2686 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2687 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2688 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2689 | 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] | 2690 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2691 | if (ret && ret != -ENOENT) |
| 2692 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2693 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2694 | /* treat errors as the image is unlocked */ |
| 2695 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2696 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2697 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2698 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2702 | enum rbd_notify_op notify_op, |
| 2703 | struct page ***preply_pages, |
| 2704 | size_t *preply_len) |
| 2705 | { |
| 2706 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2707 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2708 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 2709 | char buf[buf_size]; |
| 2710 | void *p = buf; |
| 2711 | |
| 2712 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2713 | |
| 2714 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2715 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2716 | ceph_encode_32(&p, notify_op); |
| 2717 | ceph_encode_64(&p, cid.gid); |
| 2718 | ceph_encode_64(&p, cid.handle); |
| 2719 | |
| 2720 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2721 | &rbd_dev->header_oloc, buf, buf_size, |
| 2722 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2723 | } |
| 2724 | |
| 2725 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2726 | enum rbd_notify_op notify_op) |
| 2727 | { |
| 2728 | struct page **reply_pages; |
| 2729 | size_t reply_len; |
| 2730 | |
| 2731 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2732 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2733 | } |
| 2734 | |
| 2735 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2736 | { |
| 2737 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2738 | acquired_lock_work); |
| 2739 | |
| 2740 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2741 | } |
| 2742 | |
| 2743 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2744 | { |
| 2745 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2746 | released_lock_work); |
| 2747 | |
| 2748 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2749 | } |
| 2750 | |
| 2751 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2752 | { |
| 2753 | struct page **reply_pages; |
| 2754 | size_t reply_len; |
| 2755 | bool lock_owner_responded = false; |
| 2756 | int ret; |
| 2757 | |
| 2758 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2759 | |
| 2760 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2761 | &reply_pages, &reply_len); |
| 2762 | if (ret && ret != -ETIMEDOUT) { |
| 2763 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2764 | goto out; |
| 2765 | } |
| 2766 | |
| 2767 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2768 | void *p = page_address(reply_pages[0]); |
| 2769 | void *const end = p + reply_len; |
| 2770 | u32 n; |
| 2771 | |
| 2772 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2773 | while (n--) { |
| 2774 | u8 struct_v; |
| 2775 | u32 len; |
| 2776 | |
| 2777 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2778 | p += 8 + 8; /* skip gid and cookie */ |
| 2779 | |
| 2780 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2781 | if (!len) |
| 2782 | continue; |
| 2783 | |
| 2784 | if (lock_owner_responded) { |
| 2785 | rbd_warn(rbd_dev, |
| 2786 | "duplicate lock owners detected"); |
| 2787 | ret = -EIO; |
| 2788 | goto out; |
| 2789 | } |
| 2790 | |
| 2791 | lock_owner_responded = true; |
| 2792 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2793 | &struct_v, &len); |
| 2794 | if (ret) { |
| 2795 | rbd_warn(rbd_dev, |
| 2796 | "failed to decode ResponseMessage: %d", |
| 2797 | ret); |
| 2798 | goto e_inval; |
| 2799 | } |
| 2800 | |
| 2801 | ret = ceph_decode_32(&p); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | if (!lock_owner_responded) { |
| 2806 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 2807 | ret = -ETIMEDOUT; |
| 2808 | } |
| 2809 | |
| 2810 | out: |
| 2811 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2812 | return ret; |
| 2813 | |
| 2814 | e_inval: |
| 2815 | ret = -EINVAL; |
| 2816 | goto out; |
| 2817 | } |
| 2818 | |
| 2819 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 2820 | { |
| 2821 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 2822 | |
| 2823 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 2824 | if (wake_all) |
| 2825 | wake_up_all(&rbd_dev->lock_waitq); |
| 2826 | else |
| 2827 | wake_up(&rbd_dev->lock_waitq); |
| 2828 | } |
| 2829 | |
| 2830 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 2831 | struct ceph_locker **lockers, u32 *num_lockers) |
| 2832 | { |
| 2833 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2834 | u8 lock_type; |
| 2835 | char *lock_tag; |
| 2836 | int ret; |
| 2837 | |
| 2838 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2839 | |
| 2840 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 2841 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2842 | &lock_type, &lock_tag, lockers, num_lockers); |
| 2843 | if (ret) |
| 2844 | return ret; |
| 2845 | |
| 2846 | if (*num_lockers == 0) { |
| 2847 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 2848 | goto out; |
| 2849 | } |
| 2850 | |
| 2851 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 2852 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 2853 | lock_tag); |
| 2854 | ret = -EBUSY; |
| 2855 | goto out; |
| 2856 | } |
| 2857 | |
| 2858 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 2859 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 2860 | ret = -EBUSY; |
| 2861 | goto out; |
| 2862 | } |
| 2863 | |
| 2864 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 2865 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 2866 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 2867 | (*lockers)[0].id.cookie); |
| 2868 | ret = -EBUSY; |
| 2869 | goto out; |
| 2870 | } |
| 2871 | |
| 2872 | out: |
| 2873 | kfree(lock_tag); |
| 2874 | return ret; |
| 2875 | } |
| 2876 | |
| 2877 | static int find_watcher(struct rbd_device *rbd_dev, |
| 2878 | const struct ceph_locker *locker) |
| 2879 | { |
| 2880 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2881 | struct ceph_watch_item *watchers; |
| 2882 | u32 num_watchers; |
| 2883 | u64 cookie; |
| 2884 | int i; |
| 2885 | int ret; |
| 2886 | |
| 2887 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 2888 | &rbd_dev->header_oloc, &watchers, |
| 2889 | &num_watchers); |
| 2890 | if (ret) |
| 2891 | return ret; |
| 2892 | |
| 2893 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 2894 | for (i = 0; i < num_watchers; i++) { |
| 2895 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 2896 | sizeof(locker->info.addr)) && |
| 2897 | watchers[i].cookie == cookie) { |
| 2898 | struct rbd_client_id cid = { |
| 2899 | .gid = le64_to_cpu(watchers[i].name.num), |
| 2900 | .handle = cookie, |
| 2901 | }; |
| 2902 | |
| 2903 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 2904 | rbd_dev, cid.gid, cid.handle); |
| 2905 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2906 | ret = 1; |
| 2907 | goto out; |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 2912 | ret = 0; |
| 2913 | out: |
| 2914 | kfree(watchers); |
| 2915 | return ret; |
| 2916 | } |
| 2917 | |
| 2918 | /* |
| 2919 | * lock_rwsem must be held for write |
| 2920 | */ |
| 2921 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 2922 | { |
| 2923 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 2924 | struct ceph_locker *lockers; |
| 2925 | u32 num_lockers; |
| 2926 | int ret; |
| 2927 | |
| 2928 | for (;;) { |
| 2929 | ret = rbd_lock(rbd_dev); |
| 2930 | if (ret != -EBUSY) |
| 2931 | return ret; |
| 2932 | |
| 2933 | /* determine if the current lock holder is still alive */ |
| 2934 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 2935 | if (ret) |
| 2936 | return ret; |
| 2937 | |
| 2938 | if (num_lockers == 0) |
| 2939 | goto again; |
| 2940 | |
| 2941 | ret = find_watcher(rbd_dev, lockers); |
| 2942 | if (ret) { |
| 2943 | if (ret > 0) |
| 2944 | ret = 0; /* have to request lock */ |
| 2945 | goto out; |
| 2946 | } |
| 2947 | |
| 2948 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 2949 | ENTITY_NAME(lockers[0].id.name)); |
| 2950 | |
| 2951 | ret = ceph_monc_blacklist_add(&client->monc, |
| 2952 | &lockers[0].info.addr); |
| 2953 | if (ret) { |
| 2954 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 2955 | ENTITY_NAME(lockers[0].id.name), ret); |
| 2956 | goto out; |
| 2957 | } |
| 2958 | |
| 2959 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 2960 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2961 | lockers[0].id.cookie, |
| 2962 | &lockers[0].id.name); |
| 2963 | if (ret && ret != -ENOENT) |
| 2964 | goto out; |
| 2965 | |
| 2966 | again: |
| 2967 | ceph_free_lockers(lockers, num_lockers); |
| 2968 | } |
| 2969 | |
| 2970 | out: |
| 2971 | ceph_free_lockers(lockers, num_lockers); |
| 2972 | return ret; |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 2977 | */ |
| 2978 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 2979 | int *pret) |
| 2980 | { |
| 2981 | enum rbd_lock_state lock_state; |
| 2982 | |
| 2983 | down_read(&rbd_dev->lock_rwsem); |
| 2984 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 2985 | rbd_dev->lock_state); |
| 2986 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 2987 | lock_state = rbd_dev->lock_state; |
| 2988 | up_read(&rbd_dev->lock_rwsem); |
| 2989 | return lock_state; |
| 2990 | } |
| 2991 | |
| 2992 | up_read(&rbd_dev->lock_rwsem); |
| 2993 | down_write(&rbd_dev->lock_rwsem); |
| 2994 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 2995 | rbd_dev->lock_state); |
| 2996 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 2997 | *pret = rbd_try_lock(rbd_dev); |
| 2998 | if (*pret) |
| 2999 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3000 | } |
| 3001 | |
| 3002 | lock_state = rbd_dev->lock_state; |
| 3003 | up_write(&rbd_dev->lock_rwsem); |
| 3004 | return lock_state; |
| 3005 | } |
| 3006 | |
| 3007 | static void rbd_acquire_lock(struct work_struct *work) |
| 3008 | { |
| 3009 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3010 | struct rbd_device, lock_dwork); |
| 3011 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3012 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3013 | |
| 3014 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3015 | again: |
| 3016 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3017 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3018 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3019 | wake_requests(rbd_dev, true); |
| 3020 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3021 | rbd_dev, lock_state, ret); |
| 3022 | return; |
| 3023 | } |
| 3024 | |
| 3025 | ret = rbd_request_lock(rbd_dev); |
| 3026 | if (ret == -ETIMEDOUT) { |
| 3027 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3028 | } else if (ret == -EROFS) { |
| 3029 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3030 | /* |
| 3031 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3032 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3033 | * want to block. |
| 3034 | */ |
| 3035 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3036 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3037 | /* wake "rbd map --exclusive" process */ |
| 3038 | wake_requests(rbd_dev, false); |
| 3039 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3040 | } else if (ret < 0) { |
| 3041 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3042 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3043 | RBD_RETRY_DELAY); |
| 3044 | } else { |
| 3045 | /* |
| 3046 | * lock owner acked, but resend if we don't see them |
| 3047 | * release the lock |
| 3048 | */ |
| 3049 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3050 | rbd_dev); |
| 3051 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3052 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3053 | } |
| 3054 | } |
| 3055 | |
| 3056 | /* |
| 3057 | * lock_rwsem must be held for write |
| 3058 | */ |
| 3059 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3060 | { |
| 3061 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3062 | rbd_dev->lock_state); |
| 3063 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3064 | return false; |
| 3065 | |
| 3066 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3067 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3068 | /* |
| 3069 | * Ensure that all in-flight IO is flushed. |
| 3070 | * |
| 3071 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3072 | * may be shared with other devices. |
| 3073 | */ |
| 3074 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3075 | up_read(&rbd_dev->lock_rwsem); |
| 3076 | |
| 3077 | down_write(&rbd_dev->lock_rwsem); |
| 3078 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3079 | rbd_dev->lock_state); |
| 3080 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3081 | return false; |
| 3082 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3083 | rbd_unlock(rbd_dev); |
| 3084 | /* |
| 3085 | * Give others a chance to grab the lock - we would re-acquire |
| 3086 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3087 | * otherwise. We need to ack our own notifications, so this |
| 3088 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3089 | * after wake_requests() in rbd_handle_released_lock(). |
| 3090 | */ |
| 3091 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3092 | return true; |
| 3093 | } |
| 3094 | |
| 3095 | static void rbd_release_lock_work(struct work_struct *work) |
| 3096 | { |
| 3097 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3098 | unlock_work); |
| 3099 | |
| 3100 | down_write(&rbd_dev->lock_rwsem); |
| 3101 | rbd_release_lock(rbd_dev); |
| 3102 | up_write(&rbd_dev->lock_rwsem); |
| 3103 | } |
| 3104 | |
| 3105 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3106 | void **p) |
| 3107 | { |
| 3108 | struct rbd_client_id cid = { 0 }; |
| 3109 | |
| 3110 | if (struct_v >= 2) { |
| 3111 | cid.gid = ceph_decode_64(p); |
| 3112 | cid.handle = ceph_decode_64(p); |
| 3113 | } |
| 3114 | |
| 3115 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3116 | cid.handle); |
| 3117 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3118 | down_write(&rbd_dev->lock_rwsem); |
| 3119 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3120 | /* |
| 3121 | * we already know that the remote client is |
| 3122 | * the owner |
| 3123 | */ |
| 3124 | up_write(&rbd_dev->lock_rwsem); |
| 3125 | return; |
| 3126 | } |
| 3127 | |
| 3128 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3129 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3130 | } else { |
| 3131 | down_read(&rbd_dev->lock_rwsem); |
| 3132 | } |
| 3133 | |
| 3134 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3135 | wake_requests(rbd_dev, false); |
| 3136 | up_read(&rbd_dev->lock_rwsem); |
| 3137 | } |
| 3138 | |
| 3139 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3140 | void **p) |
| 3141 | { |
| 3142 | struct rbd_client_id cid = { 0 }; |
| 3143 | |
| 3144 | if (struct_v >= 2) { |
| 3145 | cid.gid = ceph_decode_64(p); |
| 3146 | cid.handle = ceph_decode_64(p); |
| 3147 | } |
| 3148 | |
| 3149 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3150 | cid.handle); |
| 3151 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3152 | down_write(&rbd_dev->lock_rwsem); |
| 3153 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3154 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3155 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3156 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3157 | up_write(&rbd_dev->lock_rwsem); |
| 3158 | return; |
| 3159 | } |
| 3160 | |
| 3161 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3162 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3163 | } else { |
| 3164 | down_read(&rbd_dev->lock_rwsem); |
| 3165 | } |
| 3166 | |
| 3167 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3168 | wake_requests(rbd_dev, false); |
| 3169 | up_read(&rbd_dev->lock_rwsem); |
| 3170 | } |
| 3171 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3172 | /* |
| 3173 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3174 | * ResponseMessage is needed. |
| 3175 | */ |
| 3176 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3177 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3178 | { |
| 3179 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3180 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3181 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3182 | |
| 3183 | if (struct_v >= 2) { |
| 3184 | cid.gid = ceph_decode_64(p); |
| 3185 | cid.handle = ceph_decode_64(p); |
| 3186 | } |
| 3187 | |
| 3188 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3189 | cid.handle); |
| 3190 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3191 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3192 | |
| 3193 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3194 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3195 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3196 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3197 | goto out_unlock; |
| 3198 | |
| 3199 | /* |
| 3200 | * encode ResponseMessage(0) so the peer can detect |
| 3201 | * a missing owner |
| 3202 | */ |
| 3203 | result = 0; |
| 3204 | |
| 3205 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3206 | if (!rbd_dev->opts->exclusive) { |
| 3207 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3208 | __func__, rbd_dev); |
| 3209 | queue_work(rbd_dev->task_wq, |
| 3210 | &rbd_dev->unlock_work); |
| 3211 | } else { |
| 3212 | /* refuse to release the lock */ |
| 3213 | result = -EROFS; |
| 3214 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3215 | } |
| 3216 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3217 | |
| 3218 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3219 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3220 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3224 | u64 notify_id, u64 cookie, s32 *result) |
| 3225 | { |
| 3226 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3227 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3228 | char buf[buf_size]; |
| 3229 | int ret; |
| 3230 | |
| 3231 | if (result) { |
| 3232 | void *p = buf; |
| 3233 | |
| 3234 | /* encode ResponseMessage */ |
| 3235 | ceph_start_encoding(&p, 1, 1, |
| 3236 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3237 | ceph_encode_32(&p, *result); |
| 3238 | } else { |
| 3239 | buf_size = 0; |
| 3240 | } |
| 3241 | |
| 3242 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3243 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3244 | buf, buf_size); |
| 3245 | if (ret) |
| 3246 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3247 | } |
| 3248 | |
| 3249 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3250 | u64 cookie) |
| 3251 | { |
| 3252 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3253 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3254 | } |
| 3255 | |
| 3256 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3257 | u64 notify_id, u64 cookie, s32 result) |
| 3258 | { |
| 3259 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3260 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3261 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3262 | |
| 3263 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3264 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3265 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3266 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3267 | void *p = data; |
| 3268 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3269 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3270 | u32 len; |
| 3271 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3272 | int ret; |
| 3273 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3274 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3275 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3276 | if (data_len) { |
| 3277 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3278 | &struct_v, &len); |
| 3279 | if (ret) { |
| 3280 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3281 | ret); |
| 3282 | return; |
| 3283 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3284 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3285 | notify_op = ceph_decode_32(&p); |
| 3286 | } else { |
| 3287 | /* legacy notification for header updates */ |
| 3288 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3289 | len = 0; |
| 3290 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3291 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3292 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3293 | switch (notify_op) { |
| 3294 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3295 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3296 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3297 | break; |
| 3298 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3299 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3300 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3301 | break; |
| 3302 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3303 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3304 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3305 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3306 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3307 | else |
| 3308 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3309 | break; |
| 3310 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3311 | ret = rbd_dev_refresh(rbd_dev); |
| 3312 | if (ret) |
| 3313 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3314 | |
| 3315 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3316 | break; |
| 3317 | default: |
| 3318 | if (rbd_is_lock_owner(rbd_dev)) |
| 3319 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3320 | cookie, -EOPNOTSUPP); |
| 3321 | else |
| 3322 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3323 | break; |
| 3324 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3325 | } |
| 3326 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3327 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3328 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3329 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3330 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3331 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3332 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3333 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3334 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3335 | down_write(&rbd_dev->lock_rwsem); |
| 3336 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3337 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3338 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3339 | mutex_lock(&rbd_dev->watch_mutex); |
| 3340 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3341 | __rbd_unregister_watch(rbd_dev); |
| 3342 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3343 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3344 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3345 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3346 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3350 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3351 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3352 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3353 | { |
| 3354 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3355 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3356 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3357 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3358 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3359 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3360 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3361 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3362 | rbd_watch_errcb, rbd_dev); |
| 3363 | if (IS_ERR(handle)) |
| 3364 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3365 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3366 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3367 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3368 | } |
| 3369 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3370 | /* |
| 3371 | * watch_mutex must be locked |
| 3372 | */ |
| 3373 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3374 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3375 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3376 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3377 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3378 | rbd_assert(rbd_dev->watch_handle); |
| 3379 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3380 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3381 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3382 | if (ret) |
| 3383 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3384 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3385 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3388 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3389 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3390 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3391 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3392 | mutex_lock(&rbd_dev->watch_mutex); |
| 3393 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3394 | ret = __rbd_register_watch(rbd_dev); |
| 3395 | if (ret) |
| 3396 | goto out; |
| 3397 | |
| 3398 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3399 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3400 | |
| 3401 | out: |
| 3402 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3403 | return ret; |
| 3404 | } |
| 3405 | |
| 3406 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3407 | { |
| 3408 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3409 | |
| 3410 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3411 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3412 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3413 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3414 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3418 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3419 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3420 | cancel_tasks_sync(rbd_dev); |
| 3421 | |
| 3422 | mutex_lock(&rbd_dev->watch_mutex); |
| 3423 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3424 | __rbd_unregister_watch(rbd_dev); |
| 3425 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3426 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3427 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3428 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3429 | } |
| 3430 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3431 | /* |
| 3432 | * lock_rwsem must be held for write |
| 3433 | */ |
| 3434 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3435 | { |
| 3436 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3437 | char cookie[32]; |
| 3438 | int ret; |
| 3439 | |
| 3440 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3441 | |
| 3442 | format_lock_cookie(rbd_dev, cookie); |
| 3443 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3444 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3445 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3446 | RBD_LOCK_TAG, cookie); |
| 3447 | if (ret) { |
| 3448 | if (ret != -EOPNOTSUPP) |
| 3449 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3450 | ret); |
| 3451 | |
| 3452 | /* |
| 3453 | * Lock cookie cannot be updated on older OSDs, so do |
| 3454 | * a manual release and queue an acquire. |
| 3455 | */ |
| 3456 | if (rbd_release_lock(rbd_dev)) |
| 3457 | queue_delayed_work(rbd_dev->task_wq, |
| 3458 | &rbd_dev->lock_dwork, 0); |
| 3459 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3460 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3461 | } |
| 3462 | } |
| 3463 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3464 | static void rbd_reregister_watch(struct work_struct *work) |
| 3465 | { |
| 3466 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3467 | struct rbd_device, watch_dwork); |
| 3468 | int ret; |
| 3469 | |
| 3470 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3471 | |
| 3472 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3473 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3474 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3475 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3476 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3477 | |
| 3478 | ret = __rbd_register_watch(rbd_dev); |
| 3479 | if (ret) { |
| 3480 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3481 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3482 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3483 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3484 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3485 | queue_delayed_work(rbd_dev->task_wq, |
| 3486 | &rbd_dev->watch_dwork, |
| 3487 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3488 | } |
| 3489 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3490 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3494 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3495 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3496 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3497 | down_write(&rbd_dev->lock_rwsem); |
| 3498 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3499 | rbd_reacquire_lock(rbd_dev); |
| 3500 | up_write(&rbd_dev->lock_rwsem); |
| 3501 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3502 | ret = rbd_dev_refresh(rbd_dev); |
| 3503 | if (ret) |
| 3504 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3505 | } |
| 3506 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3507 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3508 | * Synchronous osd object method call. Returns the number of bytes |
| 3509 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3510 | */ |
| 3511 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3512 | struct ceph_object_id *oid, |
| 3513 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3514 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3515 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3516 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3517 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3518 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3519 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3520 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3521 | struct page *req_page = NULL; |
| 3522 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3523 | int ret; |
| 3524 | |
| 3525 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3526 | * Method calls are ultimately read operations. The result |
| 3527 | * should placed into the inbound buffer provided. They |
| 3528 | * also supply outbound data--parameters for the object |
| 3529 | * method. Currently if this is present it will be a |
| 3530 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3531 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3532 | if (outbound) { |
| 3533 | if (outbound_size > PAGE_SIZE) |
| 3534 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3535 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3536 | req_page = alloc_page(GFP_KERNEL); |
| 3537 | if (!req_page) |
| 3538 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3539 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3540 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3541 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3542 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3543 | reply_page = alloc_page(GFP_KERNEL); |
| 3544 | if (!reply_page) { |
| 3545 | if (req_page) |
| 3546 | __free_page(req_page); |
| 3547 | return -ENOMEM; |
| 3548 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3549 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3550 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3551 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3552 | reply_page, &inbound_size); |
| 3553 | if (!ret) { |
| 3554 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3555 | ret = inbound_size; |
| 3556 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3557 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3558 | if (req_page) |
| 3559 | __free_page(req_page); |
| 3560 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3561 | return ret; |
| 3562 | } |
| 3563 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3564 | /* |
| 3565 | * lock_rwsem must be held for read |
| 3566 | */ |
| 3567 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3568 | { |
| 3569 | DEFINE_WAIT(wait); |
| 3570 | |
| 3571 | do { |
| 3572 | /* |
| 3573 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3574 | * and cancel_delayed_work() in wake_requests(). |
| 3575 | */ |
| 3576 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3577 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3578 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3579 | TASK_UNINTERRUPTIBLE); |
| 3580 | up_read(&rbd_dev->lock_rwsem); |
| 3581 | schedule(); |
| 3582 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3583 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3584 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3585 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3586 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3587 | } |
| 3588 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3589 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3590 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3591 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3592 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3593 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3594 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3595 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3596 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3597 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3598 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3599 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3600 | int result; |
| 3601 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3602 | switch (req_op(rq)) { |
| 3603 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3604 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3605 | op_type = OBJ_OP_DISCARD; |
| 3606 | break; |
| 3607 | case REQ_OP_WRITE: |
| 3608 | op_type = OBJ_OP_WRITE; |
| 3609 | break; |
| 3610 | case REQ_OP_READ: |
| 3611 | op_type = OBJ_OP_READ; |
| 3612 | break; |
| 3613 | default: |
| 3614 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3615 | result = -EIO; |
| 3616 | goto err; |
| 3617 | } |
| 3618 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3619 | /* Ignore/skip any zero-length requests */ |
| 3620 | |
| 3621 | if (!length) { |
| 3622 | dout("%s: zero-length request\n", __func__); |
| 3623 | result = 0; |
| 3624 | goto err_rq; |
| 3625 | } |
| 3626 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3627 | rbd_assert(op_type == OBJ_OP_READ || |
| 3628 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3629 | |
| 3630 | /* |
| 3631 | * Quit early if the mapped snapshot no longer exists. It's |
| 3632 | * still possible the snapshot will have disappeared by the |
| 3633 | * time our request arrives at the osd, but there's no sense in |
| 3634 | * sending it if we already know. |
| 3635 | */ |
| 3636 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3637 | dout("request for non-existent snapshot"); |
| 3638 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3639 | result = -ENXIO; |
| 3640 | goto err_rq; |
| 3641 | } |
| 3642 | |
| 3643 | if (offset && length > U64_MAX - offset + 1) { |
| 3644 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3645 | length); |
| 3646 | result = -EINVAL; |
| 3647 | goto err_rq; /* Shouldn't happen */ |
| 3648 | } |
| 3649 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3650 | blk_mq_start_request(rq); |
| 3651 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3652 | down_read(&rbd_dev->header_rwsem); |
| 3653 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3654 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3655 | snapc = rbd_dev->header.snapc; |
| 3656 | ceph_get_snap_context(snapc); |
| 3657 | } |
| 3658 | up_read(&rbd_dev->header_rwsem); |
| 3659 | |
| 3660 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3661 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3662 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3663 | result = -EIO; |
| 3664 | goto err_rq; |
| 3665 | } |
| 3666 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3667 | must_be_locked = |
| 3668 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3669 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3670 | if (must_be_locked) { |
| 3671 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3672 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3673 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3674 | if (rbd_dev->opts->exclusive) { |
| 3675 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3676 | result = -EROFS; |
| 3677 | goto err_unlock; |
| 3678 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3679 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3680 | } |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3681 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3682 | result = -EBLACKLISTED; |
| 3683 | goto err_unlock; |
| 3684 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3687 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3688 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3689 | if (!img_request) { |
| 3690 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3691 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3692 | } |
| 3693 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3694 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3695 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3696 | if (op_type == OBJ_OP_DISCARD) |
| 3697 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 3698 | NULL); |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 3699 | else { |
| 3700 | struct ceph_bio_iter bio_it = { .bio = rq->bio, |
| 3701 | .iter = rq->bio->bi_iter }; |
| 3702 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3703 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 3704 | &bio_it); |
| 3705 | } |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3706 | if (result) |
| 3707 | goto err_img_request; |
| 3708 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3709 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3710 | if (must_be_locked) |
| 3711 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3712 | return; |
| 3713 | |
| 3714 | err_img_request: |
| 3715 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3716 | err_unlock: |
| 3717 | if (must_be_locked) |
| 3718 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3719 | err_rq: |
| 3720 | if (result) |
| 3721 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3722 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3723 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3724 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3725 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3726 | } |
| 3727 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3728 | 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] | 3729 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3730 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3731 | struct request *rq = bd->rq; |
| 3732 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3733 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3734 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3735 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3736 | } |
| 3737 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3738 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3739 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3740 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3741 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3742 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3743 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3746 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3747 | struct ceph_object_id *oid, |
| 3748 | struct ceph_object_locator *oloc, |
| 3749 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3750 | |
| 3751 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3752 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3753 | struct ceph_osd_request *req; |
| 3754 | struct page **pages; |
| 3755 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3756 | int ret; |
| 3757 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3758 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3759 | if (!req) |
| 3760 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3761 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3762 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3763 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3764 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3765 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3766 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3767 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3768 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3769 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3770 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3771 | if (IS_ERR(pages)) { |
| 3772 | ret = PTR_ERR(pages); |
| 3773 | goto out_req; |
| 3774 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3775 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3776 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3777 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3778 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3779 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3780 | ceph_osdc_start_request(osdc, req, false); |
| 3781 | ret = ceph_osdc_wait_request(osdc, req); |
| 3782 | if (ret >= 0) |
| 3783 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3784 | |
| 3785 | out_req: |
| 3786 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3787 | return ret; |
| 3788 | } |
| 3789 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3790 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3791 | * Read the complete header for the given rbd device. On successful |
| 3792 | * return, the rbd_dev->header field will contain up-to-date |
| 3793 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3794 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3795 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3796 | { |
| 3797 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 3798 | u32 snap_count = 0; |
| 3799 | u64 names_size = 0; |
| 3800 | u32 want_count; |
| 3801 | int ret; |
| 3802 | |
| 3803 | /* |
| 3804 | * The complete header will include an array of its 64-bit |
| 3805 | * snapshot ids, followed by the names of those snapshots as |
| 3806 | * a contiguous block of NUL-terminated strings. Note that |
| 3807 | * the number of snapshots could change by the time we read |
| 3808 | * it in, in which case we re-read it. |
| 3809 | */ |
| 3810 | do { |
| 3811 | size_t size; |
| 3812 | |
| 3813 | kfree(ondisk); |
| 3814 | |
| 3815 | size = sizeof (*ondisk); |
| 3816 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 3817 | size += names_size; |
| 3818 | ondisk = kmalloc(size, GFP_KERNEL); |
| 3819 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3820 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3821 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3822 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 3823 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3824 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3825 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 3826 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3827 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3828 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 3829 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3830 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3831 | } |
| 3832 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 3833 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3834 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3835 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3836 | } |
| 3837 | |
| 3838 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 3839 | want_count = snap_count; |
| 3840 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 3841 | } while (snap_count != want_count); |
| 3842 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3843 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 3844 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3845 | kfree(ondisk); |
| 3846 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3847 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3848 | } |
| 3849 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3850 | /* |
| 3851 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 3852 | * has disappeared from the (just updated) snapshot context. |
| 3853 | */ |
| 3854 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 3855 | { |
| 3856 | u64 snap_id; |
| 3857 | |
| 3858 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 3859 | return; |
| 3860 | |
| 3861 | snap_id = rbd_dev->spec->snap_id; |
| 3862 | if (snap_id == CEPH_NOSNAP) |
| 3863 | return; |
| 3864 | |
| 3865 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 3866 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 3867 | } |
| 3868 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3869 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 3870 | { |
| 3871 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3872 | |
| 3873 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3874 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 3875 | * try to update its size. If REMOVING is set, updating size |
| 3876 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3877 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3878 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 3879 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3880 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 3881 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 3882 | set_capacity(rbd_dev->disk, size); |
| 3883 | revalidate_disk(rbd_dev->disk); |
| 3884 | } |
| 3885 | } |
| 3886 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3887 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3888 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3889 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3890 | int ret; |
| 3891 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3892 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 3893 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 3894 | |
| 3895 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3896 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3897 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3898 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3899 | /* |
| 3900 | * If there is a parent, see if it has disappeared due to the |
| 3901 | * mapped image getting flattened. |
| 3902 | */ |
| 3903 | if (rbd_dev->parent) { |
| 3904 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 3905 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3906 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3907 | } |
| 3908 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3909 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3910 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3911 | } else { |
| 3912 | /* validate mapped snapshot's EXISTS flag */ |
| 3913 | rbd_exists_validate(rbd_dev); |
| 3914 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3915 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3916 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3917 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3918 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3919 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3920 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3921 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3922 | } |
| 3923 | |
Christoph Hellwig | d6296d3 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 3924 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 3925 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3926 | { |
| 3927 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 3928 | |
| 3929 | INIT_WORK(work, rbd_queue_workfn); |
| 3930 | return 0; |
| 3931 | } |
| 3932 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 3933 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3934 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3935 | .init_request = rbd_init_request, |
| 3936 | }; |
| 3937 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3938 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 3939 | { |
| 3940 | struct gendisk *disk; |
| 3941 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3942 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3943 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3944 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3945 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3946 | disk = alloc_disk(single_major ? |
| 3947 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 3948 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3949 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3950 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3951 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3952 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3953 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3954 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3955 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3956 | if (single_major) |
| 3957 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3958 | disk->fops = &rbd_bd_ops; |
| 3959 | disk->private_data = rbd_dev; |
| 3960 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3961 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 3962 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3963 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3964 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3965 | 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] | 3966 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 3967 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 3968 | |
| 3969 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 3970 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3971 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3972 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3973 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 3974 | if (IS_ERR(q)) { |
| 3975 | err = PTR_ERR(q); |
| 3976 | goto out_tag_set; |
| 3977 | } |
| 3978 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 3979 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 3980 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3981 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3982 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3983 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 3984 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 3985 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 3986 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 3987 | blk_queue_max_segment_size(q, UINT_MAX); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3988 | blk_queue_io_min(q, segment_size); |
| 3989 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3990 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3991 | /* enable the discard support */ |
| 3992 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 3993 | q->limits.discard_granularity = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 3994 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3995 | blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3996 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3997 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 3998 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3999 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4000 | /* |
| 4001 | * disk_release() expects a queue ref from add_disk() and will |
| 4002 | * put it. Hold an extra ref until add_disk() is called. |
| 4003 | */ |
| 4004 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4005 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4006 | q->queuedata = rbd_dev; |
| 4007 | |
| 4008 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4009 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4010 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4011 | out_tag_set: |
| 4012 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4013 | out_disk: |
| 4014 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4015 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4018 | /* |
| 4019 | sysfs |
| 4020 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4021 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4022 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4023 | { |
| 4024 | return container_of(dev, struct rbd_device, dev); |
| 4025 | } |
| 4026 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4027 | static ssize_t rbd_size_show(struct device *dev, |
| 4028 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4029 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4030 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4031 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4032 | return sprintf(buf, "%llu\n", |
| 4033 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4034 | } |
| 4035 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4036 | /* |
| 4037 | * Note this shows the features for whatever's mapped, which is not |
| 4038 | * necessarily the base image. |
| 4039 | */ |
| 4040 | static ssize_t rbd_features_show(struct device *dev, |
| 4041 | struct device_attribute *attr, char *buf) |
| 4042 | { |
| 4043 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4044 | |
| 4045 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4046 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4047 | } |
| 4048 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4049 | static ssize_t rbd_major_show(struct device *dev, |
| 4050 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4051 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4052 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4053 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4054 | if (rbd_dev->major) |
| 4055 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4056 | |
| 4057 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4058 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4059 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4060 | static ssize_t rbd_minor_show(struct device *dev, |
| 4061 | struct device_attribute *attr, char *buf) |
| 4062 | { |
| 4063 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4064 | |
| 4065 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4066 | } |
| 4067 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4068 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4069 | struct device_attribute *attr, char *buf) |
| 4070 | { |
| 4071 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4072 | struct ceph_entity_addr *client_addr = |
| 4073 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4074 | |
| 4075 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4076 | le32_to_cpu(client_addr->nonce)); |
| 4077 | } |
| 4078 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4079 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4080 | struct device_attribute *attr, char *buf) |
| 4081 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4082 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4083 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4084 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4085 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4086 | } |
| 4087 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4088 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4089 | struct device_attribute *attr, char *buf) |
| 4090 | { |
| 4091 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4092 | |
| 4093 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4094 | } |
| 4095 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4096 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4097 | struct device_attribute *attr, char *buf) |
| 4098 | { |
| 4099 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4100 | |
| 4101 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4102 | } |
| 4103 | |
| 4104 | static ssize_t rbd_pool_show(struct device *dev, |
| 4105 | struct device_attribute *attr, char *buf) |
| 4106 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4107 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4108 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4109 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4110 | } |
| 4111 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4112 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4113 | struct device_attribute *attr, char *buf) |
| 4114 | { |
| 4115 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4116 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4117 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4118 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4119 | } |
| 4120 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4121 | static ssize_t rbd_name_show(struct device *dev, |
| 4122 | struct device_attribute *attr, char *buf) |
| 4123 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4124 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4125 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4126 | if (rbd_dev->spec->image_name) |
| 4127 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4128 | |
| 4129 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4130 | } |
| 4131 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4132 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4133 | struct device_attribute *attr, char *buf) |
| 4134 | { |
| 4135 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4136 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4137 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4138 | } |
| 4139 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4140 | /* |
| 4141 | * Shows the name of the currently-mapped snapshot (or |
| 4142 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4143 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4144 | static ssize_t rbd_snap_show(struct device *dev, |
| 4145 | struct device_attribute *attr, |
| 4146 | char *buf) |
| 4147 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4148 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4149 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4150 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4151 | } |
| 4152 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4153 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4154 | struct device_attribute *attr, char *buf) |
| 4155 | { |
| 4156 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4157 | |
| 4158 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4159 | } |
| 4160 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4161 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4162 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4163 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4164 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4165 | */ |
| 4166 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4167 | struct device_attribute *attr, |
| 4168 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4169 | { |
| 4170 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4171 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4172 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4173 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4174 | return sprintf(buf, "(no parent image)\n"); |
| 4175 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4176 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4177 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4178 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4179 | count += sprintf(&buf[count], "%s" |
| 4180 | "pool_id %llu\npool_name %s\n" |
| 4181 | "image_id %s\nimage_name %s\n" |
| 4182 | "snap_id %llu\nsnap_name %s\n" |
| 4183 | "overlap %llu\n", |
| 4184 | !count ? "" : "\n", /* first? */ |
| 4185 | spec->pool_id, spec->pool_name, |
| 4186 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4187 | spec->snap_id, spec->snap_name, |
| 4188 | rbd_dev->parent_overlap); |
| 4189 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4190 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4191 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4192 | } |
| 4193 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4194 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4195 | struct device_attribute *attr, |
| 4196 | const char *buf, |
| 4197 | size_t size) |
| 4198 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4199 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4200 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4201 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4202 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4203 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4204 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4205 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4206 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4207 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4208 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4209 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4210 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4211 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4212 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4213 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4214 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4215 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4216 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4217 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4218 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4219 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4220 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4221 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4222 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4223 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4224 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4225 | |
| 4226 | static struct attribute *rbd_attrs[] = { |
| 4227 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4228 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4229 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4230 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4231 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4232 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4233 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4234 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4235 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4236 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4237 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4238 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4239 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4240 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4241 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4242 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4243 | NULL |
| 4244 | }; |
| 4245 | |
| 4246 | static struct attribute_group rbd_attr_group = { |
| 4247 | .attrs = rbd_attrs, |
| 4248 | }; |
| 4249 | |
| 4250 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4251 | &rbd_attr_group, |
| 4252 | NULL |
| 4253 | }; |
| 4254 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4255 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4256 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4257 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4258 | .name = "rbd", |
| 4259 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4260 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4261 | }; |
| 4262 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4263 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4264 | { |
| 4265 | kref_get(&spec->kref); |
| 4266 | |
| 4267 | return spec; |
| 4268 | } |
| 4269 | |
| 4270 | static void rbd_spec_free(struct kref *kref); |
| 4271 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4272 | { |
| 4273 | if (spec) |
| 4274 | kref_put(&spec->kref, rbd_spec_free); |
| 4275 | } |
| 4276 | |
| 4277 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4278 | { |
| 4279 | struct rbd_spec *spec; |
| 4280 | |
| 4281 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4282 | if (!spec) |
| 4283 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4284 | |
| 4285 | spec->pool_id = CEPH_NOPOOL; |
| 4286 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4287 | kref_init(&spec->kref); |
| 4288 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4289 | return spec; |
| 4290 | } |
| 4291 | |
| 4292 | static void rbd_spec_free(struct kref *kref) |
| 4293 | { |
| 4294 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4295 | |
| 4296 | kfree(spec->pool_name); |
| 4297 | kfree(spec->image_id); |
| 4298 | kfree(spec->image_name); |
| 4299 | kfree(spec->snap_name); |
| 4300 | kfree(spec); |
| 4301 | } |
| 4302 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4303 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4304 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4305 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4306 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4307 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4308 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4309 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4310 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4311 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4312 | rbd_put_client(rbd_dev->rbd_client); |
| 4313 | rbd_spec_put(rbd_dev->spec); |
| 4314 | kfree(rbd_dev->opts); |
| 4315 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | static void rbd_dev_release(struct device *dev) |
| 4319 | { |
| 4320 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4321 | bool need_put = !!rbd_dev->opts; |
| 4322 | |
| 4323 | if (need_put) { |
| 4324 | destroy_workqueue(rbd_dev->task_wq); |
| 4325 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4326 | } |
| 4327 | |
| 4328 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4329 | |
| 4330 | /* |
| 4331 | * This is racy, but way better than putting module outside of |
| 4332 | * the release callback. The race window is pretty small, so |
| 4333 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4334 | */ |
| 4335 | if (need_put) |
| 4336 | module_put(THIS_MODULE); |
| 4337 | } |
| 4338 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4339 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4340 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4341 | { |
| 4342 | struct rbd_device *rbd_dev; |
| 4343 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4344 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4345 | if (!rbd_dev) |
| 4346 | return NULL; |
| 4347 | |
| 4348 | spin_lock_init(&rbd_dev->lock); |
| 4349 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4350 | init_rwsem(&rbd_dev->header_rwsem); |
| 4351 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4352 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4353 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4354 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4355 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4356 | mutex_init(&rbd_dev->watch_mutex); |
| 4357 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4358 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4359 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4360 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4361 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4362 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4363 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4364 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4365 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4366 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4367 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4368 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4369 | rbd_dev->dev.type = &rbd_device_type; |
| 4370 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4371 | device_initialize(&rbd_dev->dev); |
| 4372 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4373 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4374 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4375 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4376 | return rbd_dev; |
| 4377 | } |
| 4378 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4379 | /* |
| 4380 | * Create a mapping rbd_dev. |
| 4381 | */ |
| 4382 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4383 | struct rbd_spec *spec, |
| 4384 | struct rbd_options *opts) |
| 4385 | { |
| 4386 | struct rbd_device *rbd_dev; |
| 4387 | |
| 4388 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4389 | if (!rbd_dev) |
| 4390 | return NULL; |
| 4391 | |
| 4392 | rbd_dev->opts = opts; |
| 4393 | |
| 4394 | /* get an id and fill in device name */ |
| 4395 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4396 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4397 | GFP_KERNEL); |
| 4398 | if (rbd_dev->dev_id < 0) |
| 4399 | goto fail_rbd_dev; |
| 4400 | |
| 4401 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4402 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4403 | rbd_dev->name); |
| 4404 | if (!rbd_dev->task_wq) |
| 4405 | goto fail_dev_id; |
| 4406 | |
| 4407 | /* we have a ref from do_rbd_add() */ |
| 4408 | __module_get(THIS_MODULE); |
| 4409 | |
| 4410 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4411 | return rbd_dev; |
| 4412 | |
| 4413 | fail_dev_id: |
| 4414 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4415 | fail_rbd_dev: |
| 4416 | rbd_dev_free(rbd_dev); |
| 4417 | return NULL; |
| 4418 | } |
| 4419 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4420 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4421 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4422 | if (rbd_dev) |
| 4423 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4424 | } |
| 4425 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4426 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4427 | * Get the size and object order for an image snapshot, or if |
| 4428 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4429 | * image. |
| 4430 | */ |
| 4431 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4432 | u8 *order, u64 *snap_size) |
| 4433 | { |
| 4434 | __le64 snapid = cpu_to_le64(snap_id); |
| 4435 | int ret; |
| 4436 | struct { |
| 4437 | u8 order; |
| 4438 | __le64 size; |
| 4439 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4440 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4441 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4442 | &rbd_dev->header_oloc, "get_size", |
| 4443 | &snapid, sizeof(snapid), |
| 4444 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4445 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4446 | if (ret < 0) |
| 4447 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4448 | if (ret < sizeof (size_buf)) |
| 4449 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4450 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4451 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4452 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4453 | dout(" order %u", (unsigned int)*order); |
| 4454 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4455 | *snap_size = le64_to_cpu(size_buf.size); |
| 4456 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4457 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4458 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4459 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4460 | |
| 4461 | return 0; |
| 4462 | } |
| 4463 | |
| 4464 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4465 | { |
| 4466 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4467 | &rbd_dev->header.obj_order, |
| 4468 | &rbd_dev->header.image_size); |
| 4469 | } |
| 4470 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4471 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4472 | { |
| 4473 | void *reply_buf; |
| 4474 | int ret; |
| 4475 | void *p; |
| 4476 | |
| 4477 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4478 | if (!reply_buf) |
| 4479 | return -ENOMEM; |
| 4480 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4481 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4482 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4483 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4484 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4485 | if (ret < 0) |
| 4486 | goto out; |
| 4487 | |
| 4488 | p = reply_buf; |
| 4489 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4490 | p + ret, NULL, GFP_NOIO); |
| 4491 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4492 | |
| 4493 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4494 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4495 | rbd_dev->header.object_prefix = NULL; |
| 4496 | } else { |
| 4497 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4498 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4499 | out: |
| 4500 | kfree(reply_buf); |
| 4501 | |
| 4502 | return ret; |
| 4503 | } |
| 4504 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4505 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4506 | u64 *snap_features) |
| 4507 | { |
| 4508 | __le64 snapid = cpu_to_le64(snap_id); |
| 4509 | struct { |
| 4510 | __le64 features; |
| 4511 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4512 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4513 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4514 | int ret; |
| 4515 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4516 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4517 | &rbd_dev->header_oloc, "get_features", |
| 4518 | &snapid, sizeof(snapid), |
| 4519 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4520 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4521 | if (ret < 0) |
| 4522 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4523 | if (ret < sizeof (features_buf)) |
| 4524 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4525 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4526 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4527 | if (unsup) { |
| 4528 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4529 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4530 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4531 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4532 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4533 | *snap_features = le64_to_cpu(features_buf.features); |
| 4534 | |
| 4535 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4536 | (unsigned long long)snap_id, |
| 4537 | (unsigned long long)*snap_features, |
| 4538 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4539 | |
| 4540 | return 0; |
| 4541 | } |
| 4542 | |
| 4543 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4544 | { |
| 4545 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4546 | &rbd_dev->header.features); |
| 4547 | } |
| 4548 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4549 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4550 | { |
| 4551 | struct rbd_spec *parent_spec; |
| 4552 | size_t size; |
| 4553 | void *reply_buf = NULL; |
| 4554 | __le64 snapid; |
| 4555 | void *p; |
| 4556 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4557 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4558 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4559 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4560 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4561 | int ret; |
| 4562 | |
| 4563 | parent_spec = rbd_spec_alloc(); |
| 4564 | if (!parent_spec) |
| 4565 | return -ENOMEM; |
| 4566 | |
| 4567 | size = sizeof (__le64) + /* pool_id */ |
| 4568 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4569 | sizeof (__le64) + /* snap_id */ |
| 4570 | sizeof (__le64); /* overlap */ |
| 4571 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4572 | if (!reply_buf) { |
| 4573 | ret = -ENOMEM; |
| 4574 | goto out_err; |
| 4575 | } |
| 4576 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4577 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4578 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4579 | &rbd_dev->header_oloc, "get_parent", |
| 4580 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4581 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4582 | if (ret < 0) |
| 4583 | goto out_err; |
| 4584 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4585 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4586 | end = reply_buf + ret; |
| 4587 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4588 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4589 | if (pool_id == CEPH_NOPOOL) { |
| 4590 | /* |
| 4591 | * Either the parent never existed, or we have |
| 4592 | * record of it but the image got flattened so it no |
| 4593 | * longer has a parent. When the parent of a |
| 4594 | * layered image disappears we immediately set the |
| 4595 | * overlap to 0. The effect of this is that all new |
| 4596 | * requests will be treated as if the image had no |
| 4597 | * parent. |
| 4598 | */ |
| 4599 | if (rbd_dev->parent_overlap) { |
| 4600 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4601 | rbd_dev_parent_put(rbd_dev); |
| 4602 | pr_info("%s: clone image has been flattened\n", |
| 4603 | rbd_dev->disk->disk_name); |
| 4604 | } |
| 4605 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4606 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4607 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4608 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4609 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4610 | |
| 4611 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4612 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4613 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4614 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4615 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4616 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4617 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4618 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4619 | if (IS_ERR(image_id)) { |
| 4620 | ret = PTR_ERR(image_id); |
| 4621 | goto out_err; |
| 4622 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4623 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4624 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 4625 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4626 | /* |
| 4627 | * The parent won't change (except when the clone is |
| 4628 | * flattened, already handled that). So we only need to |
| 4629 | * record the parent spec we have not already done so. |
| 4630 | */ |
| 4631 | if (!rbd_dev->parent_spec) { |
| 4632 | parent_spec->pool_id = pool_id; |
| 4633 | parent_spec->image_id = image_id; |
| 4634 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4635 | rbd_dev->parent_spec = parent_spec; |
| 4636 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 4637 | } else { |
| 4638 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4642 | * We always update the parent overlap. If it's zero we issue |
| 4643 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4644 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4645 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4646 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4647 | /* refresh, careful to warn just once */ |
| 4648 | if (rbd_dev->parent_overlap) |
| 4649 | rbd_warn(rbd_dev, |
| 4650 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4651 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4652 | /* initial probe */ |
| 4653 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4654 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4655 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4656 | rbd_dev->parent_overlap = overlap; |
| 4657 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4658 | out: |
| 4659 | ret = 0; |
| 4660 | out_err: |
| 4661 | kfree(reply_buf); |
| 4662 | rbd_spec_put(parent_spec); |
| 4663 | |
| 4664 | return ret; |
| 4665 | } |
| 4666 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4667 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 4668 | { |
| 4669 | struct { |
| 4670 | __le64 stripe_unit; |
| 4671 | __le64 stripe_count; |
| 4672 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 4673 | size_t size = sizeof (striping_info_buf); |
| 4674 | void *p; |
| 4675 | u64 obj_size; |
| 4676 | u64 stripe_unit; |
| 4677 | u64 stripe_count; |
| 4678 | int ret; |
| 4679 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4680 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4681 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 4682 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4683 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 4684 | if (ret < 0) |
| 4685 | return ret; |
| 4686 | if (ret < size) |
| 4687 | return -ERANGE; |
| 4688 | |
| 4689 | /* |
| 4690 | * We don't actually support the "fancy striping" feature |
| 4691 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 4692 | * defaults the behavior is the same as before. So find |
| 4693 | * out, and only fail if the image has non-default values. |
| 4694 | */ |
| 4695 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4696 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4697 | p = &striping_info_buf; |
| 4698 | stripe_unit = ceph_decode_64(&p); |
| 4699 | if (stripe_unit != obj_size) { |
| 4700 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 4701 | "(got %llu want %llu)", |
| 4702 | stripe_unit, obj_size); |
| 4703 | return -EINVAL; |
| 4704 | } |
| 4705 | stripe_count = ceph_decode_64(&p); |
| 4706 | if (stripe_count != 1) { |
| 4707 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 4708 | "(got %llu want 1)", stripe_count); |
| 4709 | return -EINVAL; |
| 4710 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4711 | rbd_dev->header.stripe_unit = stripe_unit; |
| 4712 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4713 | |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4717 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 4718 | { |
| 4719 | __le64 data_pool_id; |
| 4720 | int ret; |
| 4721 | |
| 4722 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4723 | &rbd_dev->header_oloc, "get_data_pool", |
| 4724 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 4725 | if (ret < 0) |
| 4726 | return ret; |
| 4727 | if (ret < sizeof(data_pool_id)) |
| 4728 | return -EBADMSG; |
| 4729 | |
| 4730 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 4731 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4735 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 4736 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4737 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4738 | size_t image_id_size; |
| 4739 | char *image_id; |
| 4740 | void *p; |
| 4741 | void *end; |
| 4742 | size_t size; |
| 4743 | void *reply_buf = NULL; |
| 4744 | size_t len = 0; |
| 4745 | char *image_name = NULL; |
| 4746 | int ret; |
| 4747 | |
| 4748 | rbd_assert(!rbd_dev->spec->image_name); |
| 4749 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4750 | len = strlen(rbd_dev->spec->image_id); |
| 4751 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4752 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 4753 | if (!image_id) |
| 4754 | return NULL; |
| 4755 | |
| 4756 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4757 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4758 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4759 | |
| 4760 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 4761 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4762 | if (!reply_buf) |
| 4763 | goto out; |
| 4764 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4765 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 4766 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 4767 | "dir_get_name", image_id, image_id_size, |
| 4768 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4769 | if (ret < 0) |
| 4770 | goto out; |
| 4771 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4772 | end = reply_buf + ret; |
| 4773 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4774 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 4775 | if (IS_ERR(image_name)) |
| 4776 | image_name = NULL; |
| 4777 | else |
| 4778 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 4779 | out: |
| 4780 | kfree(reply_buf); |
| 4781 | kfree(image_id); |
| 4782 | |
| 4783 | return image_name; |
| 4784 | } |
| 4785 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4786 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4787 | { |
| 4788 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4789 | const char *snap_name; |
| 4790 | u32 which = 0; |
| 4791 | |
| 4792 | /* Skip over names until we find the one we are looking for */ |
| 4793 | |
| 4794 | snap_name = rbd_dev->header.snap_names; |
| 4795 | while (which < snapc->num_snaps) { |
| 4796 | if (!strcmp(name, snap_name)) |
| 4797 | return snapc->snaps[which]; |
| 4798 | snap_name += strlen(snap_name) + 1; |
| 4799 | which++; |
| 4800 | } |
| 4801 | return CEPH_NOSNAP; |
| 4802 | } |
| 4803 | |
| 4804 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4805 | { |
| 4806 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4807 | u32 which; |
| 4808 | bool found = false; |
| 4809 | u64 snap_id; |
| 4810 | |
| 4811 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 4812 | const char *snap_name; |
| 4813 | |
| 4814 | snap_id = snapc->snaps[which]; |
| 4815 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 4816 | if (IS_ERR(snap_name)) { |
| 4817 | /* ignore no-longer existing snapshots */ |
| 4818 | if (PTR_ERR(snap_name) == -ENOENT) |
| 4819 | continue; |
| 4820 | else |
| 4821 | break; |
| 4822 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4823 | found = !strcmp(name, snap_name); |
| 4824 | kfree(snap_name); |
| 4825 | } |
| 4826 | return found ? snap_id : CEPH_NOSNAP; |
| 4827 | } |
| 4828 | |
| 4829 | /* |
| 4830 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 4831 | * no snapshot by that name is found, or if an error occurs. |
| 4832 | */ |
| 4833 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4834 | { |
| 4835 | if (rbd_dev->image_format == 1) |
| 4836 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 4837 | |
| 4838 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 4839 | } |
| 4840 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4841 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4842 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4843 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4844 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 4845 | { |
| 4846 | struct rbd_spec *spec = rbd_dev->spec; |
| 4847 | |
| 4848 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 4849 | rbd_assert(spec->image_id && spec->image_name); |
| 4850 | rbd_assert(spec->snap_name); |
| 4851 | |
| 4852 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 4853 | u64 snap_id; |
| 4854 | |
| 4855 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 4856 | if (snap_id == CEPH_NOSNAP) |
| 4857 | return -ENOENT; |
| 4858 | |
| 4859 | spec->snap_id = snap_id; |
| 4860 | } else { |
| 4861 | spec->snap_id = CEPH_NOSNAP; |
| 4862 | } |
| 4863 | |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | * A parent image will have all ids but none of the names. |
| 4869 | * |
| 4870 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 4871 | * can't figure out the name for an image id. |
| 4872 | */ |
| 4873 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4874 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4875 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4876 | struct rbd_spec *spec = rbd_dev->spec; |
| 4877 | const char *pool_name; |
| 4878 | const char *image_name; |
| 4879 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4880 | int ret; |
| 4881 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4882 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 4883 | rbd_assert(spec->image_id); |
| 4884 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4885 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4886 | /* 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] | 4887 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4888 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 4889 | if (!pool_name) { |
| 4890 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4891 | return -EIO; |
| 4892 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4893 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 4894 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4895 | return -ENOMEM; |
| 4896 | |
| 4897 | /* Fetch the image name; tolerate failure here */ |
| 4898 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4899 | image_name = rbd_dev_image_name(rbd_dev); |
| 4900 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4901 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4902 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4903 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4904 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4905 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 4906 | if (IS_ERR(snap_name)) { |
| 4907 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4908 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | spec->pool_name = pool_name; |
| 4912 | spec->image_name = image_name; |
| 4913 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4914 | |
| 4915 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4916 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4917 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4918 | kfree(image_name); |
| 4919 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4920 | return ret; |
| 4921 | } |
| 4922 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4923 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4924 | { |
| 4925 | size_t size; |
| 4926 | int ret; |
| 4927 | void *reply_buf; |
| 4928 | void *p; |
| 4929 | void *end; |
| 4930 | u64 seq; |
| 4931 | u32 snap_count; |
| 4932 | struct ceph_snap_context *snapc; |
| 4933 | u32 i; |
| 4934 | |
| 4935 | /* |
| 4936 | * We'll need room for the seq value (maximum snapshot id), |
| 4937 | * snapshot count, and array of that many snapshot ids. |
| 4938 | * For now we have a fixed upper limit on the number we're |
| 4939 | * prepared to receive. |
| 4940 | */ |
| 4941 | size = sizeof (__le64) + sizeof (__le32) + |
| 4942 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 4943 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 4944 | if (!reply_buf) |
| 4945 | return -ENOMEM; |
| 4946 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4947 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4948 | &rbd_dev->header_oloc, "get_snapcontext", |
| 4949 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4950 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4951 | if (ret < 0) |
| 4952 | goto out; |
| 4953 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4954 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4955 | end = reply_buf + ret; |
| 4956 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4957 | ceph_decode_64_safe(&p, end, seq, out); |
| 4958 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 4959 | |
| 4960 | /* |
| 4961 | * Make sure the reported number of snapshot ids wouldn't go |
| 4962 | * beyond the end of our buffer. But before checking that, |
| 4963 | * make sure the computed size of the snapshot context we |
| 4964 | * allocate is representable in a size_t. |
| 4965 | */ |
| 4966 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 4967 | / sizeof (u64)) { |
| 4968 | ret = -EINVAL; |
| 4969 | goto out; |
| 4970 | } |
| 4971 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 4972 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4973 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4974 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 4975 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4976 | if (!snapc) { |
| 4977 | ret = -ENOMEM; |
| 4978 | goto out; |
| 4979 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4980 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4981 | for (i = 0; i < snap_count; i++) |
| 4982 | snapc->snaps[i] = ceph_decode_64(&p); |
| 4983 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 4984 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4985 | rbd_dev->header.snapc = snapc; |
| 4986 | |
| 4987 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4988 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4989 | out: |
| 4990 | kfree(reply_buf); |
| 4991 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4992 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4993 | } |
| 4994 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4995 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 4996 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4997 | { |
| 4998 | size_t size; |
| 4999 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5000 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5001 | int ret; |
| 5002 | void *p; |
| 5003 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5004 | char *snap_name; |
| 5005 | |
| 5006 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5007 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5008 | if (!reply_buf) |
| 5009 | return ERR_PTR(-ENOMEM); |
| 5010 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5011 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5012 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5013 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5014 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5015 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5016 | if (ret < 0) { |
| 5017 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5018 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5019 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5020 | |
| 5021 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5022 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5023 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5024 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5025 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5026 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5027 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5028 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5029 | out: |
| 5030 | kfree(reply_buf); |
| 5031 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5032 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5033 | } |
| 5034 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5035 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5036 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5037 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5038 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5039 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5040 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5041 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5042 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5043 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5044 | if (first_time) { |
| 5045 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5046 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5047 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5048 | } |
| 5049 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5050 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5051 | if (ret && first_time) { |
| 5052 | kfree(rbd_dev->header.object_prefix); |
| 5053 | rbd_dev->header.object_prefix = NULL; |
| 5054 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5055 | |
| 5056 | return ret; |
| 5057 | } |
| 5058 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5059 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5060 | { |
| 5061 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5062 | |
| 5063 | if (rbd_dev->image_format == 1) |
| 5064 | return rbd_dev_v1_header_info(rbd_dev); |
| 5065 | |
| 5066 | return rbd_dev_v2_header_info(rbd_dev); |
| 5067 | } |
| 5068 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5069 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5070 | * Skips over white space at *buf, and updates *buf to point to the |
| 5071 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5072 | * the token (string of non-white space characters) found. Note |
| 5073 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5074 | */ |
| 5075 | static inline size_t next_token(const char **buf) |
| 5076 | { |
| 5077 | /* |
| 5078 | * These are the characters that produce nonzero for |
| 5079 | * isspace() in the "C" and "POSIX" locales. |
| 5080 | */ |
| 5081 | const char *spaces = " \f\n\r\t\v"; |
| 5082 | |
| 5083 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5084 | |
| 5085 | return strcspn(*buf, spaces); /* Return token length */ |
| 5086 | } |
| 5087 | |
| 5088 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5089 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5090 | * enough to hold a copy of it, and copies the token into the new |
| 5091 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5092 | * that a duplicate buffer is created even for a zero-length token. |
| 5093 | * |
| 5094 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5095 | * pointer if memory for the duplicate was not available. If |
| 5096 | * the lenp argument is a non-null pointer, the length of the token |
| 5097 | * (not including the '\0') is returned in *lenp. |
| 5098 | * |
| 5099 | * If successful, the *buf pointer will be updated to point beyond |
| 5100 | * the end of the found token. |
| 5101 | * |
| 5102 | * Note: uses GFP_KERNEL for allocation. |
| 5103 | */ |
| 5104 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5105 | { |
| 5106 | char *dup; |
| 5107 | size_t len; |
| 5108 | |
| 5109 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5110 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5111 | if (!dup) |
| 5112 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5113 | *(dup + len) = '\0'; |
| 5114 | *buf += len; |
| 5115 | |
| 5116 | if (lenp) |
| 5117 | *lenp = len; |
| 5118 | |
| 5119 | return dup; |
| 5120 | } |
| 5121 | |
| 5122 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5123 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5124 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5125 | * and the data written is passed here via a NUL-terminated buffer. |
| 5126 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5127 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5128 | * The information extracted from these options is recorded in |
| 5129 | * the other parameters which return dynamically-allocated |
| 5130 | * structures: |
| 5131 | * ceph_opts |
| 5132 | * The address of a pointer that will refer to a ceph options |
| 5133 | * structure. Caller must release the returned pointer using |
| 5134 | * ceph_destroy_options() when it is no longer needed. |
| 5135 | * rbd_opts |
| 5136 | * Address of an rbd options pointer. Fully initialized by |
| 5137 | * this function; caller must release with kfree(). |
| 5138 | * spec |
| 5139 | * Address of an rbd image specification pointer. Fully |
| 5140 | * initialized by this function based on parsed options. |
| 5141 | * Caller must release with rbd_spec_put(). |
| 5142 | * |
| 5143 | * The options passed take this form: |
| 5144 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5145 | * where: |
| 5146 | * <mon_addrs> |
| 5147 | * A comma-separated list of one or more monitor addresses. |
| 5148 | * A monitor address is an ip address, optionally followed |
| 5149 | * by a port number (separated by a colon). |
| 5150 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5151 | * <options> |
| 5152 | * A comma-separated list of ceph and/or rbd options. |
| 5153 | * <pool_name> |
| 5154 | * The name of the rados pool containing the rbd image. |
| 5155 | * <image_name> |
| 5156 | * The name of the image in that pool to map. |
| 5157 | * <snap_id> |
| 5158 | * An optional snapshot id. If provided, the mapping will |
| 5159 | * present data from the image at the time that snapshot was |
| 5160 | * created. The image head is used if no snapshot id is |
| 5161 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5162 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5163 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5164 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5165 | struct rbd_options **opts, |
| 5166 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5167 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5168 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5169 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5170 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5171 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5172 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5173 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5174 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5175 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5176 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5177 | |
| 5178 | /* The first four tokens are required */ |
| 5179 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5180 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5181 | if (!len) { |
| 5182 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5183 | return -EINVAL; |
| 5184 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5185 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5186 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5187 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5188 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5189 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5190 | options = dup_token(&buf, NULL); |
| 5191 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5192 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5193 | if (!*options) { |
| 5194 | rbd_warn(NULL, "no options provided"); |
| 5195 | goto out_err; |
| 5196 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5197 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5198 | spec = rbd_spec_alloc(); |
| 5199 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5200 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5201 | |
| 5202 | spec->pool_name = dup_token(&buf, NULL); |
| 5203 | if (!spec->pool_name) |
| 5204 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5205 | if (!*spec->pool_name) { |
| 5206 | rbd_warn(NULL, "no pool name provided"); |
| 5207 | goto out_err; |
| 5208 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5209 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5210 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5211 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5212 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5213 | if (!*spec->image_name) { |
| 5214 | rbd_warn(NULL, "no image name provided"); |
| 5215 | goto out_err; |
| 5216 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5217 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5218 | /* |
| 5219 | * Snapshot name is optional; default is to use "-" |
| 5220 | * (indicating the head/no snapshot). |
| 5221 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5222 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5223 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5224 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5225 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5226 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5227 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5228 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5229 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5230 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5231 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5232 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5233 | *(snap_name + len) = '\0'; |
| 5234 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5235 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5236 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5237 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5238 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5239 | if (!rbd_opts) |
| 5240 | goto out_mem; |
| 5241 | |
| 5242 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5243 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5244 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5245 | rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5246 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5247 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5248 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5249 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5250 | if (IS_ERR(copts)) { |
| 5251 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5252 | goto out_err; |
| 5253 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5254 | kfree(options); |
| 5255 | |
| 5256 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5257 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5258 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5259 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5260 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5261 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5262 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5263 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5264 | kfree(rbd_opts); |
| 5265 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5266 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5267 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5268 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5269 | } |
| 5270 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5271 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5272 | * Return pool id (>= 0) or a negative error code. |
| 5273 | */ |
| 5274 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5275 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5276 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5277 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5278 | int tries = 0; |
| 5279 | int ret; |
| 5280 | |
| 5281 | again: |
| 5282 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5283 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5284 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5285 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5286 | if (ret < 0) |
| 5287 | return ret; |
| 5288 | |
| 5289 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5290 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5291 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5292 | newest_epoch, |
| 5293 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5294 | goto again; |
| 5295 | } else { |
| 5296 | /* the osdmap we have is new enough */ |
| 5297 | return -ENOENT; |
| 5298 | } |
| 5299 | } |
| 5300 | |
| 5301 | return ret; |
| 5302 | } |
| 5303 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5304 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5305 | { |
| 5306 | down_write(&rbd_dev->lock_rwsem); |
| 5307 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5308 | rbd_unlock(rbd_dev); |
| 5309 | up_write(&rbd_dev->lock_rwsem); |
| 5310 | } |
| 5311 | |
| 5312 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5313 | { |
| 5314 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5315 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5316 | return -EINVAL; |
| 5317 | } |
| 5318 | |
| 5319 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5320 | down_read(&rbd_dev->lock_rwsem); |
| 5321 | rbd_wait_state_locked(rbd_dev); |
| 5322 | up_read(&rbd_dev->lock_rwsem); |
| 5323 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 5324 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5325 | return -EROFS; |
| 5326 | } |
| 5327 | |
| 5328 | return 0; |
| 5329 | } |
| 5330 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5331 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5332 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5333 | * name given to it by the user. Internally, that identifier is |
| 5334 | * what's used to specify the names of objects related to the image. |
| 5335 | * |
| 5336 | * A special "rbd id" object is used to map an rbd image name to its |
| 5337 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5338 | * with the supplied name. |
| 5339 | * |
| 5340 | * This function will record the given rbd_dev's image_id field if |
| 5341 | * it can be determined, and in that case will return 0. If any |
| 5342 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5343 | * image_id field will be unchanged (and should be NULL). |
| 5344 | */ |
| 5345 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5346 | { |
| 5347 | int ret; |
| 5348 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5349 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5350 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5351 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5352 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5353 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5354 | * When probing a parent image, the image id is already |
| 5355 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5356 | * need to fetch the image id again in this case. We |
| 5357 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5358 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5359 | if (rbd_dev->spec->image_id) { |
| 5360 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5361 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5362 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5363 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5364 | |
| 5365 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5366 | * First, see if the format 2 image id file exists, and if |
| 5367 | * so, get the image's persistent id from it. |
| 5368 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5369 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5370 | rbd_dev->spec->image_name); |
| 5371 | if (ret) |
| 5372 | return ret; |
| 5373 | |
| 5374 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5375 | |
| 5376 | /* Response will be an encoded string, which includes a length */ |
| 5377 | |
| 5378 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5379 | response = kzalloc(size, GFP_NOIO); |
| 5380 | if (!response) { |
| 5381 | ret = -ENOMEM; |
| 5382 | goto out; |
| 5383 | } |
| 5384 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5385 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5386 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5387 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5388 | "get_id", NULL, 0, |
| 5389 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5390 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5391 | if (ret == -ENOENT) { |
| 5392 | image_id = kstrdup("", GFP_KERNEL); |
| 5393 | ret = image_id ? 0 : -ENOMEM; |
| 5394 | if (!ret) |
| 5395 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5396 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5397 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5398 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5399 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5400 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5401 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5402 | if (!ret) |
| 5403 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | if (!ret) { |
| 5407 | rbd_dev->spec->image_id = image_id; |
| 5408 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5409 | } |
| 5410 | out: |
| 5411 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5412 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5413 | return ret; |
| 5414 | } |
| 5415 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5416 | /* |
| 5417 | * Undo whatever state changes are made by v1 or v2 header info |
| 5418 | * call. |
| 5419 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5420 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5421 | { |
| 5422 | struct rbd_image_header *header; |
| 5423 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5424 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5425 | |
| 5426 | /* Free dynamic fields from the header, then zero it out */ |
| 5427 | |
| 5428 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5429 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5430 | kfree(header->snap_sizes); |
| 5431 | kfree(header->snap_names); |
| 5432 | kfree(header->object_prefix); |
| 5433 | memset(header, 0, sizeof (*header)); |
| 5434 | } |
| 5435 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5436 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5437 | { |
| 5438 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5439 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5440 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5441 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5442 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5443 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5444 | /* |
| 5445 | * Get the and check features for the image. Currently the |
| 5446 | * features are assumed to never change. |
| 5447 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5448 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5449 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5450 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5451 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5452 | /* If the image supports fancy striping, get its parameters */ |
| 5453 | |
| 5454 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5455 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5456 | if (ret < 0) |
| 5457 | goto out_err; |
| 5458 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5459 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5460 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5461 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5462 | if (ret) |
| 5463 | goto out_err; |
| 5464 | } |
| 5465 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5466 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5467 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5468 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5469 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5470 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5471 | kfree(rbd_dev->header.object_prefix); |
| 5472 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5473 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5474 | } |
| 5475 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5476 | /* |
| 5477 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5478 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5479 | * length of the already discovered part of the parent chain. |
| 5480 | */ |
| 5481 | 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] | 5482 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5483 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5484 | int ret; |
| 5485 | |
| 5486 | if (!rbd_dev->parent_spec) |
| 5487 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5488 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5489 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5490 | pr_info("parent chain is too long (%d)\n", depth); |
| 5491 | ret = -EINVAL; |
| 5492 | goto out_err; |
| 5493 | } |
| 5494 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5495 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5496 | if (!parent) { |
| 5497 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5498 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | /* |
| 5502 | * Images related by parent/child relationships always share |
| 5503 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5504 | */ |
| 5505 | __rbd_get_client(rbd_dev->rbd_client); |
| 5506 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5507 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5508 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5509 | if (ret < 0) |
| 5510 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5511 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5512 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5513 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5514 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5515 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5516 | out_err: |
| 5517 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5518 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5519 | return ret; |
| 5520 | } |
| 5521 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5522 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5523 | { |
| 5524 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5525 | rbd_dev_mapping_clear(rbd_dev); |
| 5526 | rbd_free_disk(rbd_dev); |
| 5527 | if (!single_major) |
| 5528 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5529 | } |
| 5530 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5531 | /* |
| 5532 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5533 | * upon return. |
| 5534 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5535 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5536 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5537 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5538 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5539 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5540 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5541 | if (!single_major) { |
| 5542 | ret = register_blkdev(0, rbd_dev->name); |
| 5543 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5544 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5545 | |
| 5546 | rbd_dev->major = ret; |
| 5547 | rbd_dev->minor = 0; |
| 5548 | } else { |
| 5549 | rbd_dev->major = rbd_major; |
| 5550 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5551 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5552 | |
| 5553 | /* Set up the blkdev mapping. */ |
| 5554 | |
| 5555 | ret = rbd_init_disk(rbd_dev); |
| 5556 | if (ret) |
| 5557 | goto err_out_blkdev; |
| 5558 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5559 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5560 | if (ret) |
| 5561 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5562 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5563 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5564 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5565 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5566 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5567 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5568 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5569 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5570 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5571 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5572 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5573 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5574 | err_out_mapping: |
| 5575 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5576 | err_out_disk: |
| 5577 | rbd_free_disk(rbd_dev); |
| 5578 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5579 | if (!single_major) |
| 5580 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5581 | err_out_unlock: |
| 5582 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5583 | return ret; |
| 5584 | } |
| 5585 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5586 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5587 | { |
| 5588 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5589 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5590 | |
| 5591 | /* Record the header object name for this rbd image. */ |
| 5592 | |
| 5593 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5594 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5595 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5596 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5597 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5598 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5599 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5600 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5601 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5602 | } |
| 5603 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5604 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5605 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5606 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5607 | if (rbd_dev->opts) |
| 5608 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5609 | rbd_dev->image_format = 0; |
| 5610 | kfree(rbd_dev->spec->image_id); |
| 5611 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5612 | } |
| 5613 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5614 | /* |
| 5615 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5616 | * device. If this image is the one being mapped (i.e., not a |
| 5617 | * parent), initiate a watch on its header object before using that |
| 5618 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5619 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5620 | 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] | 5621 | { |
| 5622 | int ret; |
| 5623 | |
| 5624 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5625 | * Get the id from the image id object. Unless there's an |
| 5626 | * error, rbd_dev->spec->image_id will be filled in with |
| 5627 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5628 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5629 | */ |
| 5630 | ret = rbd_dev_image_id(rbd_dev); |
| 5631 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5632 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5633 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5634 | ret = rbd_dev_header_name(rbd_dev); |
| 5635 | if (ret) |
| 5636 | goto err_out_format; |
| 5637 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5638 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5639 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5640 | if (ret) { |
| 5641 | if (ret == -ENOENT) |
| 5642 | pr_info("image %s/%s does not exist\n", |
| 5643 | rbd_dev->spec->pool_name, |
| 5644 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5645 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5646 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5647 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5648 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5649 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5650 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5651 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5652 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5653 | /* |
| 5654 | * If this image is the one being mapped, we have pool name and |
| 5655 | * id, image name and id, and snap name - need to fill snap id. |
| 5656 | * Otherwise this is a parent image, identified by pool, image |
| 5657 | * and snap ids - need to fill in names for those ids. |
| 5658 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5659 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5660 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5661 | else |
| 5662 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5663 | if (ret) { |
| 5664 | if (ret == -ENOENT) |
| 5665 | pr_info("snap %s/%s@%s does not exist\n", |
| 5666 | rbd_dev->spec->pool_name, |
| 5667 | rbd_dev->spec->image_name, |
| 5668 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5669 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5670 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5671 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5672 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5673 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5674 | if (ret) |
| 5675 | goto err_out_probe; |
| 5676 | |
| 5677 | /* |
| 5678 | * Need to warn users if this image is the one being |
| 5679 | * mapped and has a parent. |
| 5680 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5681 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5682 | rbd_warn(rbd_dev, |
| 5683 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 5684 | } |
| 5685 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5686 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5687 | if (ret) |
| 5688 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5689 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5690 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5691 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5692 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5693 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5694 | err_out_probe: |
| 5695 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5696 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5697 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5698 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5699 | err_out_format: |
| 5700 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5701 | kfree(rbd_dev->spec->image_id); |
| 5702 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5703 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5704 | } |
| 5705 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5706 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 5707 | const char *buf, |
| 5708 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5709 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5710 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5711 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5712 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5713 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5714 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5715 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5716 | |
| 5717 | if (!try_module_get(THIS_MODULE)) |
| 5718 | return -ENODEV; |
| 5719 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5720 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5721 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5722 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5723 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5724 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5725 | rbdc = rbd_get_client(ceph_opts); |
| 5726 | if (IS_ERR(rbdc)) { |
| 5727 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5728 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5729 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5730 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5731 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5732 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5733 | if (rc < 0) { |
| 5734 | if (rc == -ENOENT) |
| 5735 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5736 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5737 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5738 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5739 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5740 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5741 | if (!rbd_dev) { |
| 5742 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5743 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5744 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5745 | rbdc = NULL; /* rbd_dev now owns this */ |
| 5746 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5747 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5748 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5749 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 5750 | if (!rbd_dev->config_info) { |
| 5751 | rc = -ENOMEM; |
| 5752 | goto err_out_rbd_dev; |
| 5753 | } |
| 5754 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5755 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5756 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5757 | if (rc < 0) { |
| 5758 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5759 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5760 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 5761 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5762 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5763 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5764 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5765 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5766 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5767 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5768 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5769 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5770 | if (rbd_dev->opts->exclusive) { |
| 5771 | rc = rbd_add_acquire_lock(rbd_dev); |
| 5772 | if (rc) |
| 5773 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5774 | } |
| 5775 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5776 | /* Everything's ready. Announce the disk to the world. */ |
| 5777 | |
| 5778 | rc = device_add(&rbd_dev->dev); |
| 5779 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5780 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5781 | |
| 5782 | add_disk(rbd_dev->disk); |
| 5783 | /* see rbd_init_disk() */ |
| 5784 | blk_put_queue(rbd_dev->disk->queue); |
| 5785 | |
| 5786 | spin_lock(&rbd_dev_list_lock); |
| 5787 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5788 | spin_unlock(&rbd_dev_list_lock); |
| 5789 | |
| 5790 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5791 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5792 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5793 | rc = count; |
| 5794 | out: |
| 5795 | module_put(THIS_MODULE); |
| 5796 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5797 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5798 | err_out_image_lock: |
| 5799 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5800 | err_out_device_setup: |
| 5801 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5802 | err_out_image_probe: |
| 5803 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5804 | err_out_rbd_dev: |
| 5805 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5806 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5807 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5808 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5809 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5810 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5811 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5812 | } |
| 5813 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5814 | static ssize_t rbd_add(struct bus_type *bus, |
| 5815 | const char *buf, |
| 5816 | size_t count) |
| 5817 | { |
| 5818 | if (single_major) |
| 5819 | return -EINVAL; |
| 5820 | |
| 5821 | return do_rbd_add(bus, buf, count); |
| 5822 | } |
| 5823 | |
| 5824 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 5825 | const char *buf, |
| 5826 | size_t count) |
| 5827 | { |
| 5828 | return do_rbd_add(bus, buf, count); |
| 5829 | } |
| 5830 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5831 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 5832 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5833 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5834 | struct rbd_device *first = rbd_dev; |
| 5835 | struct rbd_device *second = first->parent; |
| 5836 | struct rbd_device *third; |
| 5837 | |
| 5838 | /* |
| 5839 | * Follow to the parent with no grandparent and |
| 5840 | * remove it. |
| 5841 | */ |
| 5842 | while (second && (third = second->parent)) { |
| 5843 | first = second; |
| 5844 | second = third; |
| 5845 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5846 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5847 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5848 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5849 | first->parent = NULL; |
| 5850 | first->parent_overlap = 0; |
| 5851 | |
| 5852 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5853 | rbd_spec_put(first->parent_spec); |
| 5854 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5855 | } |
| 5856 | } |
| 5857 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5858 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 5859 | const char *buf, |
| 5860 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5861 | { |
| 5862 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5863 | struct list_head *tmp; |
| 5864 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5865 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5866 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5867 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5868 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5869 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5870 | dev_id = -1; |
| 5871 | opt_buf[0] = '\0'; |
| 5872 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 5873 | if (dev_id < 0) { |
| 5874 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5875 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5876 | } |
| 5877 | if (opt_buf[0] != '\0') { |
| 5878 | if (!strcmp(opt_buf, "force")) { |
| 5879 | force = true; |
| 5880 | } else { |
| 5881 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 5882 | return -EINVAL; |
| 5883 | } |
| 5884 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5885 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5886 | ret = -ENOENT; |
| 5887 | spin_lock(&rbd_dev_list_lock); |
| 5888 | list_for_each(tmp, &rbd_dev_list) { |
| 5889 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 5890 | if (rbd_dev->dev_id == dev_id) { |
| 5891 | ret = 0; |
| 5892 | break; |
| 5893 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5894 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5895 | if (!ret) { |
| 5896 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5897 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5898 | ret = -EBUSY; |
| 5899 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5900 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 5901 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5902 | spin_unlock_irq(&rbd_dev->lock); |
| 5903 | } |
| 5904 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5905 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5906 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5907 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5908 | if (force) { |
| 5909 | /* |
| 5910 | * Prevent new IO from being queued and wait for existing |
| 5911 | * IO to complete/fail. |
| 5912 | */ |
| 5913 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 5914 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 5915 | } |
| 5916 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5917 | del_gendisk(rbd_dev->disk); |
| 5918 | spin_lock(&rbd_dev_list_lock); |
| 5919 | list_del_init(&rbd_dev->node); |
| 5920 | spin_unlock(&rbd_dev_list_lock); |
| 5921 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 5922 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5923 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5924 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5925 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5926 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5927 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5928 | } |
| 5929 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5930 | static ssize_t rbd_remove(struct bus_type *bus, |
| 5931 | const char *buf, |
| 5932 | size_t count) |
| 5933 | { |
| 5934 | if (single_major) |
| 5935 | return -EINVAL; |
| 5936 | |
| 5937 | return do_rbd_remove(bus, buf, count); |
| 5938 | } |
| 5939 | |
| 5940 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 5941 | const char *buf, |
| 5942 | size_t count) |
| 5943 | { |
| 5944 | return do_rbd_remove(bus, buf, count); |
| 5945 | } |
| 5946 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5947 | /* |
| 5948 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5949 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5950 | */ |
| 5951 | static int rbd_sysfs_init(void) |
| 5952 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5953 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5954 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5955 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 5956 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5957 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5958 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5959 | ret = bus_register(&rbd_bus_type); |
| 5960 | if (ret < 0) |
| 5961 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5962 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5963 | return ret; |
| 5964 | } |
| 5965 | |
| 5966 | static void rbd_sysfs_cleanup(void) |
| 5967 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5968 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5969 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5970 | } |
| 5971 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5972 | static int rbd_slab_init(void) |
| 5973 | { |
| 5974 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5975 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5976 | if (!rbd_img_request_cache) |
| 5977 | return -ENOMEM; |
| 5978 | |
| 5979 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5980 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 5981 | if (!rbd_obj_request_cache) |
| 5982 | goto out_err; |
| 5983 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5984 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5985 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5986 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5987 | kmem_cache_destroy(rbd_img_request_cache); |
| 5988 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5989 | return -ENOMEM; |
| 5990 | } |
| 5991 | |
| 5992 | static void rbd_slab_exit(void) |
| 5993 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5994 | rbd_assert(rbd_obj_request_cache); |
| 5995 | kmem_cache_destroy(rbd_obj_request_cache); |
| 5996 | rbd_obj_request_cache = NULL; |
| 5997 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5998 | rbd_assert(rbd_img_request_cache); |
| 5999 | kmem_cache_destroy(rbd_img_request_cache); |
| 6000 | rbd_img_request_cache = NULL; |
| 6001 | } |
| 6002 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6003 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6004 | { |
| 6005 | int rc; |
| 6006 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6007 | if (!libceph_compatible(NULL)) { |
| 6008 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6009 | return -EINVAL; |
| 6010 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6011 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6012 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6013 | if (rc) |
| 6014 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6015 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6016 | /* |
| 6017 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6018 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6019 | */ |
| 6020 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6021 | if (!rbd_wq) { |
| 6022 | rc = -ENOMEM; |
| 6023 | goto err_out_slab; |
| 6024 | } |
| 6025 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6026 | if (single_major) { |
| 6027 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6028 | if (rbd_major < 0) { |
| 6029 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6030 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6031 | } |
| 6032 | } |
| 6033 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6034 | rc = rbd_sysfs_init(); |
| 6035 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6036 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6037 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6038 | if (single_major) |
| 6039 | pr_info("loaded (major %d)\n", rbd_major); |
| 6040 | else |
| 6041 | pr_info("loaded\n"); |
| 6042 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6043 | return 0; |
| 6044 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6045 | err_out_blkdev: |
| 6046 | if (single_major) |
| 6047 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6048 | err_out_wq: |
| 6049 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6050 | err_out_slab: |
| 6051 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6052 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6053 | } |
| 6054 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6055 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6056 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6057 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6058 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6059 | if (single_major) |
| 6060 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6061 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6062 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6063 | } |
| 6064 | |
| 6065 | module_init(rbd_init); |
| 6066 | module_exit(rbd_exit); |
| 6067 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6068 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6069 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6070 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6071 | /* following authorship retained from original osdblk.c */ |
| 6072 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6073 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6074 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6075 | MODULE_LICENSE("GPL"); |