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; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 212 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 213 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 214 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 215 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 216 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 217 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 218 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 219 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 220 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 221 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 222 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 223 | }; |
| 224 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 225 | /* |
| 226 | * Writes go through the following state machine to deal with |
| 227 | * layering: |
| 228 | * |
| 229 | * need copyup |
| 230 | * RBD_OBJ_WRITE_GUARD ---------------> RBD_OBJ_WRITE_COPYUP |
| 231 | * | ^ | |
| 232 | * v \------------------------------/ |
| 233 | * done |
| 234 | * ^ |
| 235 | * | |
| 236 | * RBD_OBJ_WRITE_FLAT |
| 237 | * |
| 238 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
| 239 | * there is a parent or not. |
| 240 | */ |
| 241 | enum rbd_obj_write_state { |
| 242 | RBD_OBJ_WRITE_FLAT = 1, |
| 243 | RBD_OBJ_WRITE_GUARD, |
| 244 | RBD_OBJ_WRITE_COPYUP, |
| 245 | }; |
| 246 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 247 | struct rbd_obj_request { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 248 | u64 object_no; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 249 | u64 offset; /* object start byte */ |
| 250 | u64 length; /* bytes from offset */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 251 | union { |
| 252 | bool tried_parent; /* for reads */ |
| 253 | enum rbd_obj_write_state write_state; /* for writes */ |
| 254 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 255 | |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 256 | struct rbd_img_request *img_request; |
| 257 | u64 img_offset; |
| 258 | /* links for img_request->obj_requests list */ |
| 259 | struct list_head links; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 260 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 261 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 262 | struct ceph_bio_iter bio_pos; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 263 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 264 | struct ceph_bvec_iter bvec_pos; |
| 265 | u32 bvec_count; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 266 | }; |
| 267 | }; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 268 | struct bio_vec *copyup_bvecs; |
| 269 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 270 | |
| 271 | struct ceph_osd_request *osd_req; |
| 272 | |
| 273 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 274 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 275 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | struct kref kref; |
| 277 | }; |
| 278 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 279 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 280 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 281 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 282 | }; |
| 283 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 284 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 285 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 286 | enum obj_operation_type op_type; |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 287 | enum obj_request_type data_type; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 288 | u64 offset; /* starting image byte offset */ |
| 289 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 290 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 291 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 292 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 293 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 294 | }; |
| 295 | union { |
| 296 | struct request *rq; /* block request */ |
| 297 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 298 | }; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 299 | spinlock_t completion_lock; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 300 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 301 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | |
| 303 | u32 obj_request_count; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 304 | u32 pending_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 305 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 306 | |
| 307 | struct kref kref; |
| 308 | }; |
| 309 | |
| 310 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 311 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 312 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 313 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 314 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 315 | enum rbd_watch_state { |
| 316 | RBD_WATCH_STATE_UNREGISTERED, |
| 317 | RBD_WATCH_STATE_REGISTERED, |
| 318 | RBD_WATCH_STATE_ERROR, |
| 319 | }; |
| 320 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 321 | enum rbd_lock_state { |
| 322 | RBD_LOCK_STATE_UNLOCKED, |
| 323 | RBD_LOCK_STATE_LOCKED, |
| 324 | RBD_LOCK_STATE_RELEASING, |
| 325 | }; |
| 326 | |
| 327 | /* WatchNotify::ClientId */ |
| 328 | struct rbd_client_id { |
| 329 | u64 gid; |
| 330 | u64 handle; |
| 331 | }; |
| 332 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 333 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 334 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 335 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 336 | }; |
| 337 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 338 | /* |
| 339 | * a single device |
| 340 | */ |
| 341 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 342 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 343 | |
| 344 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 345 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 346 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 347 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 348 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 349 | struct rbd_client *rbd_client; |
| 350 | |
| 351 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 352 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 353 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 354 | |
| 355 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 356 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 357 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 358 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 359 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 360 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 361 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 362 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 363 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 364 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 365 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 366 | struct mutex watch_mutex; |
| 367 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 368 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 369 | u64 watch_cookie; |
| 370 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 371 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 372 | struct rw_semaphore lock_rwsem; |
| 373 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 374 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 375 | struct rbd_client_id owner_cid; |
| 376 | struct work_struct acquired_lock_work; |
| 377 | struct work_struct released_lock_work; |
| 378 | struct delayed_work lock_dwork; |
| 379 | struct work_struct unlock_work; |
| 380 | wait_queue_head_t lock_waitq; |
| 381 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 382 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 383 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 384 | struct rbd_spec *parent_spec; |
| 385 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 386 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 387 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 388 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 389 | /* Block layer tags. */ |
| 390 | struct blk_mq_tag_set tag_set; |
| 391 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 392 | /* protects updating the header */ |
| 393 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 394 | |
| 395 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 396 | |
| 397 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 398 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 399 | /* sysfs related */ |
| 400 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 401 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 402 | }; |
| 403 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 404 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 405 | * Flag bits for rbd_dev->flags: |
| 406 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 407 | * by rbd_dev->lock |
| 408 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 409 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 410 | enum rbd_dev_flags { |
| 411 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 412 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 413 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 414 | }; |
| 415 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 416 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 417 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 418 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 419 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 420 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 421 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 422 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 423 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 424 | /* Slab caches for frequently-allocated structures */ |
| 425 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 426 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 427 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 428 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 429 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 430 | static DEFINE_IDA(rbd_dev_id_ida); |
| 431 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 432 | static struct workqueue_struct *rbd_wq; |
| 433 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 434 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 435 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 436 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 437 | static bool single_major = true; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 438 | module_param(single_major, bool, S_IRUGO); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 439 | 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] | 440 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 441 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 442 | size_t count); |
| 443 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 444 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 445 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 446 | size_t count); |
| 447 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 448 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 449 | 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] | 450 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 451 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 452 | static int rbd_dev_id_to_minor(int dev_id) |
| 453 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 454 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | static int minor_to_rbd_dev_id(int minor) |
| 458 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 459 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 460 | } |
| 461 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 462 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 463 | { |
| 464 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 465 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 466 | } |
| 467 | |
| 468 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 469 | { |
| 470 | bool is_lock_owner; |
| 471 | |
| 472 | down_read(&rbd_dev->lock_rwsem); |
| 473 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 474 | up_read(&rbd_dev->lock_rwsem); |
| 475 | return is_lock_owner; |
| 476 | } |
| 477 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 478 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 479 | { |
| 480 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 481 | } |
| 482 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 483 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 484 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 485 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 486 | 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] | 487 | 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] | 488 | |
| 489 | static struct attribute *rbd_bus_attrs[] = { |
| 490 | &bus_attr_add.attr, |
| 491 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 492 | &bus_attr_add_single_major.attr, |
| 493 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 494 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 495 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 496 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 497 | |
| 498 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 499 | struct attribute *attr, int index) |
| 500 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 501 | if (!single_major && |
| 502 | (attr == &bus_attr_add_single_major.attr || |
| 503 | attr == &bus_attr_remove_single_major.attr)) |
| 504 | return 0; |
| 505 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 506 | return attr->mode; |
| 507 | } |
| 508 | |
| 509 | static const struct attribute_group rbd_bus_group = { |
| 510 | .attrs = rbd_bus_attrs, |
| 511 | .is_visible = rbd_bus_is_visible, |
| 512 | }; |
| 513 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 514 | |
| 515 | static struct bus_type rbd_bus_type = { |
| 516 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 517 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | static void rbd_root_dev_release(struct device *dev) |
| 521 | { |
| 522 | } |
| 523 | |
| 524 | static struct device rbd_root_dev = { |
| 525 | .init_name = "rbd", |
| 526 | .release = rbd_root_dev_release, |
| 527 | }; |
| 528 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 529 | static __printf(2, 3) |
| 530 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 531 | { |
| 532 | struct va_format vaf; |
| 533 | va_list args; |
| 534 | |
| 535 | va_start(args, fmt); |
| 536 | vaf.fmt = fmt; |
| 537 | vaf.va = &args; |
| 538 | |
| 539 | if (!rbd_dev) |
| 540 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 541 | else if (rbd_dev->disk) |
| 542 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 543 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 544 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 545 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 546 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 547 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 548 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 549 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 550 | else /* punt */ |
| 551 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 552 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 553 | va_end(args); |
| 554 | } |
| 555 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 556 | #ifdef RBD_DEBUG |
| 557 | #define rbd_assert(expr) \ |
| 558 | if (unlikely(!(expr))) { \ |
| 559 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 560 | "at line %d:\n\n" \ |
| 561 | "\trbd_assert(%s);\n\n", \ |
| 562 | __func__, __LINE__, #expr); \ |
| 563 | BUG(); \ |
| 564 | } |
| 565 | #else /* !RBD_DEBUG */ |
| 566 | # define rbd_assert(expr) ((void) 0) |
| 567 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 568 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 569 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 570 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 571 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 572 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 573 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 574 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 575 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 576 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 577 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 578 | u8 *order, u64 *snap_size); |
| 579 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 580 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 581 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 582 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 583 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 584 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 585 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 586 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 587 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 588 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 589 | removing = true; |
| 590 | else |
| 591 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 592 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 593 | if (removing) |
| 594 | return -ENOENT; |
| 595 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 596 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 597 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 601 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 602 | { |
| 603 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 604 | unsigned long open_count_before; |
| 605 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 606 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 607 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 608 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 609 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 610 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 611 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 612 | } |
| 613 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 614 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 615 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 616 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 617 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 618 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 619 | return -EFAULT; |
| 620 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 621 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 622 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 623 | return -EROFS; |
| 624 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 625 | /* Let blkdev_roset() handle it */ |
| 626 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 630 | unsigned int cmd, unsigned long arg) |
| 631 | { |
| 632 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 633 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 634 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 635 | switch (cmd) { |
| 636 | case BLKROSET: |
| 637 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 638 | break; |
| 639 | default: |
| 640 | ret = -ENOTTY; |
| 641 | } |
| 642 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | #ifdef CONFIG_COMPAT |
| 647 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 648 | unsigned int cmd, unsigned long arg) |
| 649 | { |
| 650 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 651 | } |
| 652 | #endif /* CONFIG_COMPAT */ |
| 653 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 654 | static const struct block_device_operations rbd_bd_ops = { |
| 655 | .owner = THIS_MODULE, |
| 656 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 657 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 658 | .ioctl = rbd_ioctl, |
| 659 | #ifdef CONFIG_COMPAT |
| 660 | .compat_ioctl = rbd_compat_ioctl, |
| 661 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 662 | }; |
| 663 | |
| 664 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 665 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 666 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 667 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 668 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 669 | { |
| 670 | struct rbd_client *rbdc; |
| 671 | int ret = -ENOMEM; |
| 672 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 673 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 674 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 675 | if (!rbdc) |
| 676 | goto out_opt; |
| 677 | |
| 678 | kref_init(&rbdc->kref); |
| 679 | INIT_LIST_HEAD(&rbdc->node); |
| 680 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 681 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 682 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 683 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 684 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 685 | |
| 686 | ret = ceph_open_session(rbdc->client); |
| 687 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 688 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 689 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 690 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 691 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 692 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 693 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 694 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 695 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 696 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 697 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 698 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 699 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 700 | kfree(rbdc); |
| 701 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 702 | if (ceph_opts) |
| 703 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 704 | dout("%s: error %d\n", __func__, ret); |
| 705 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 706 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 709 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 710 | { |
| 711 | kref_get(&rbdc->kref); |
| 712 | |
| 713 | return rbdc; |
| 714 | } |
| 715 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 716 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 717 | * Find a ceph client with specific addr and configuration. If |
| 718 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 720 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | { |
| 722 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 723 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 724 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 725 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 726 | return NULL; |
| 727 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 728 | spin_lock(&rbd_client_list_lock); |
| 729 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 730 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 731 | __rbd_get_client(client_node); |
| 732 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 733 | found = true; |
| 734 | break; |
| 735 | } |
| 736 | } |
| 737 | spin_unlock(&rbd_client_list_lock); |
| 738 | |
| 739 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 743 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 744 | */ |
| 745 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 746 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 747 | Opt_last_int, |
| 748 | /* int args above */ |
| 749 | Opt_last_string, |
| 750 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 751 | Opt_read_only, |
| 752 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 753 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 754 | Opt_exclusive, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 755 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 756 | }; |
| 757 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 758 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 759 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 760 | /* int args above */ |
| 761 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 762 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 763 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 764 | {Opt_read_write, "read_write"}, |
| 765 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 766 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 767 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 768 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 769 | }; |
| 770 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 771 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 772 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 773 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 774 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 775 | bool exclusive; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 776 | }; |
| 777 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 778 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 779 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 780 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 781 | #define RBD_EXCLUSIVE_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 782 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 783 | static int parse_rbd_opts_token(char *c, void *private) |
| 784 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 785 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 786 | substring_t argstr[MAX_OPT_ARGS]; |
| 787 | int token, intval, ret; |
| 788 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 789 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 790 | if (token < Opt_last_int) { |
| 791 | ret = match_int(&argstr[0], &intval); |
| 792 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 793 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | return ret; |
| 795 | } |
| 796 | dout("got int token %d val %d\n", token, intval); |
| 797 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 798 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 799 | } else { |
| 800 | dout("got token %d\n", token); |
| 801 | } |
| 802 | |
| 803 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 804 | case Opt_queue_depth: |
| 805 | if (intval < 1) { |
| 806 | pr_err("queue_depth out of range\n"); |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | rbd_opts->queue_depth = intval; |
| 810 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 811 | case Opt_read_only: |
| 812 | rbd_opts->read_only = true; |
| 813 | break; |
| 814 | case Opt_read_write: |
| 815 | rbd_opts->read_only = false; |
| 816 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 817 | case Opt_lock_on_read: |
| 818 | rbd_opts->lock_on_read = true; |
| 819 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 820 | case Opt_exclusive: |
| 821 | rbd_opts->exclusive = true; |
| 822 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 823 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 824 | /* libceph prints "bad option" msg */ |
| 825 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 827 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 828 | return 0; |
| 829 | } |
| 830 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 831 | static char* obj_op_name(enum obj_operation_type op_type) |
| 832 | { |
| 833 | switch (op_type) { |
| 834 | case OBJ_OP_READ: |
| 835 | return "read"; |
| 836 | case OBJ_OP_WRITE: |
| 837 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 838 | case OBJ_OP_DISCARD: |
| 839 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 840 | default: |
| 841 | return "???"; |
| 842 | } |
| 843 | } |
| 844 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 845 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 846 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 847 | * not exist create it. Either way, ceph_opts is consumed by this |
| 848 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 849 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 850 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 851 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 852 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 853 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 854 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 855 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 856 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 857 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 858 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 859 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 860 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 861 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 862 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* |
| 866 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 867 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 868 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 869 | */ |
| 870 | static void rbd_client_release(struct kref *kref) |
| 871 | { |
| 872 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 873 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 874 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 875 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 876 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 877 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 878 | |
| 879 | ceph_destroy_client(rbdc->client); |
| 880 | kfree(rbdc); |
| 881 | } |
| 882 | |
| 883 | /* |
| 884 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 885 | * it. |
| 886 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 887 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 888 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 889 | if (rbdc) |
| 890 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 891 | } |
| 892 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 893 | static bool rbd_image_format_valid(u32 image_format) |
| 894 | { |
| 895 | return image_format == 1 || image_format == 2; |
| 896 | } |
| 897 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 898 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 899 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 900 | size_t size; |
| 901 | u32 snap_count; |
| 902 | |
| 903 | /* The header has to start with the magic rbd header text */ |
| 904 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 905 | return false; |
| 906 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 907 | /* The bio layer requires at least sector-sized I/O */ |
| 908 | |
| 909 | if (ondisk->options.order < SECTOR_SHIFT) |
| 910 | return false; |
| 911 | |
| 912 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 913 | |
| 914 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 915 | return false; |
| 916 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 917 | /* |
| 918 | * The size of a snapshot header has to fit in a size_t, and |
| 919 | * that limits the number of snapshots. |
| 920 | */ |
| 921 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 922 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 923 | if (snap_count > size / sizeof (__le64)) |
| 924 | return false; |
| 925 | |
| 926 | /* |
| 927 | * Not only that, but the size of the entire the snapshot |
| 928 | * header must also be representable in a size_t. |
| 929 | */ |
| 930 | size -= snap_count * sizeof (__le64); |
| 931 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 932 | return false; |
| 933 | |
| 934 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 935 | } |
| 936 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 937 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 938 | * returns the size of an object in the image |
| 939 | */ |
| 940 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 941 | { |
| 942 | return 1U << header->obj_order; |
| 943 | } |
| 944 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 945 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 946 | { |
| 947 | if (rbd_dev->header.stripe_unit == 0 || |
| 948 | rbd_dev->header.stripe_count == 0) { |
| 949 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 950 | rbd_dev->header.stripe_count = 1; |
| 951 | } |
| 952 | |
| 953 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 954 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 955 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 956 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 957 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 958 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 959 | } |
| 960 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 961 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 962 | * Fill an rbd image header with information from the given format 1 |
| 963 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 964 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 965 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 966 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 967 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 968 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 969 | bool first_time = header->object_prefix == NULL; |
| 970 | struct ceph_snap_context *snapc; |
| 971 | char *object_prefix = NULL; |
| 972 | char *snap_names = NULL; |
| 973 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 974 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 975 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 976 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 977 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 978 | /* Allocate this now to avoid having to handle failure below */ |
| 979 | |
| 980 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 981 | object_prefix = kstrndup(ondisk->object_prefix, |
| 982 | sizeof(ondisk->object_prefix), |
| 983 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 984 | if (!object_prefix) |
| 985 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 989 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 990 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 991 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 992 | if (!snapc) |
| 993 | goto out_err; |
| 994 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 995 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 996 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 997 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 998 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 999 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1000 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1001 | if (snap_names_len > (u64)SIZE_MAX) |
| 1002 | goto out_2big; |
| 1003 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1004 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1005 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1006 | |
| 1007 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1008 | snap_sizes = kmalloc_array(snap_count, |
| 1009 | sizeof(*header->snap_sizes), |
| 1010 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1011 | if (!snap_sizes) |
| 1012 | goto out_err; |
| 1013 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1014 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1015 | * Copy the names, and fill in each snapshot's id |
| 1016 | * and size. |
| 1017 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1018 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1019 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1020 | * snap_names_len bytes beyond the end of the |
| 1021 | * snapshot id array, this memcpy() is safe. |
| 1022 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1023 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1024 | snaps = ondisk->snaps; |
| 1025 | for (i = 0; i < snap_count; i++) { |
| 1026 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1027 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1028 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1029 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1030 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1031 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1032 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1033 | if (first_time) { |
| 1034 | header->object_prefix = object_prefix; |
| 1035 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1036 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | } else { |
| 1038 | ceph_put_snap_context(header->snapc); |
| 1039 | kfree(header->snap_names); |
| 1040 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1044 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1045 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | header->snapc = snapc; |
| 1047 | header->snap_names = snap_names; |
| 1048 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1049 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1050 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1051 | out_2big: |
| 1052 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1053 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1054 | kfree(snap_sizes); |
| 1055 | kfree(snap_names); |
| 1056 | ceph_put_snap_context(snapc); |
| 1057 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1058 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1062 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1063 | { |
| 1064 | const char *snap_name; |
| 1065 | |
| 1066 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1067 | |
| 1068 | /* Skip over names until we find the one we are looking for */ |
| 1069 | |
| 1070 | snap_name = rbd_dev->header.snap_names; |
| 1071 | while (which--) |
| 1072 | snap_name += strlen(snap_name) + 1; |
| 1073 | |
| 1074 | return kstrdup(snap_name, GFP_KERNEL); |
| 1075 | } |
| 1076 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1077 | /* |
| 1078 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1079 | * Note that result is for snapshots in *descending* order. |
| 1080 | */ |
| 1081 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1082 | { |
| 1083 | u64 snap_id1 = *(u64 *)s1; |
| 1084 | u64 snap_id2 = *(u64 *)s2; |
| 1085 | |
| 1086 | if (snap_id1 < snap_id2) |
| 1087 | return 1; |
| 1088 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Search a snapshot context to see if the given snapshot id is |
| 1093 | * present. |
| 1094 | * |
| 1095 | * Returns the position of the snapshot id in the array if it's found, |
| 1096 | * or BAD_SNAP_INDEX otherwise. |
| 1097 | * |
| 1098 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1099 | * reverse order, highest snapshot id first. |
| 1100 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1101 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1102 | { |
| 1103 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1104 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1105 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1106 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1107 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1108 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1109 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1110 | } |
| 1111 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1112 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1113 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1114 | { |
| 1115 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1116 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1117 | |
| 1118 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1119 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1120 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1121 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1122 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1123 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1124 | } |
| 1125 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1126 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1127 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1128 | if (snap_id == CEPH_NOSNAP) |
| 1129 | return RBD_SNAP_HEAD_NAME; |
| 1130 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1131 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1132 | if (rbd_dev->image_format == 1) |
| 1133 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1134 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1135 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1136 | } |
| 1137 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1138 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1139 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1140 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1141 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1142 | if (snap_id == CEPH_NOSNAP) { |
| 1143 | *snap_size = rbd_dev->header.image_size; |
| 1144 | } else if (rbd_dev->image_format == 1) { |
| 1145 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1146 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1147 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1148 | if (which == BAD_SNAP_INDEX) |
| 1149 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1150 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1151 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1152 | } else { |
| 1153 | u64 size = 0; |
| 1154 | int ret; |
| 1155 | |
| 1156 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1157 | if (ret) |
| 1158 | return ret; |
| 1159 | |
| 1160 | *snap_size = size; |
| 1161 | } |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
| 1165 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1166 | u64 *snap_features) |
| 1167 | { |
| 1168 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1169 | if (snap_id == CEPH_NOSNAP) { |
| 1170 | *snap_features = rbd_dev->header.features; |
| 1171 | } else if (rbd_dev->image_format == 1) { |
| 1172 | *snap_features = 0; /* No features for format 1 */ |
| 1173 | } else { |
| 1174 | u64 features = 0; |
| 1175 | int ret; |
| 1176 | |
| 1177 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1178 | if (ret) |
| 1179 | return ret; |
| 1180 | |
| 1181 | *snap_features = features; |
| 1182 | } |
| 1183 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1186 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1187 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1188 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1189 | u64 size = 0; |
| 1190 | u64 features = 0; |
| 1191 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1192 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1193 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1194 | if (ret) |
| 1195 | return ret; |
| 1196 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1197 | if (ret) |
| 1198 | return ret; |
| 1199 | |
| 1200 | rbd_dev->mapping.size = size; |
| 1201 | rbd_dev->mapping.features = features; |
| 1202 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1203 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1206 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1207 | { |
| 1208 | rbd_dev->mapping.size = 0; |
| 1209 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1210 | } |
| 1211 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1212 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1213 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1214 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1215 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1216 | return offset & (segment_size - 1); |
| 1217 | } |
| 1218 | |
| 1219 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1220 | u64 offset, u64 length) |
| 1221 | { |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1222 | u64 segment_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1223 | |
| 1224 | offset &= segment_size - 1; |
| 1225 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1226 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1227 | if (offset + length > segment_size) |
| 1228 | length = segment_size - offset; |
| 1229 | |
| 1230 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1231 | } |
| 1232 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1233 | static void zero_bvec(struct bio_vec *bv) |
| 1234 | { |
| 1235 | void *buf; |
| 1236 | unsigned long flags; |
| 1237 | |
| 1238 | buf = bvec_kmap_irq(bv, &flags); |
| 1239 | memset(buf, 0, bv->bv_len); |
| 1240 | flush_dcache_page(bv->bv_page); |
| 1241 | bvec_kunmap_irq(buf, &flags); |
| 1242 | } |
| 1243 | |
| 1244 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
| 1245 | { |
| 1246 | struct ceph_bio_iter it = *bio_pos; |
| 1247 | |
| 1248 | ceph_bio_iter_advance(&it, off); |
| 1249 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1250 | zero_bvec(&bv); |
| 1251 | })); |
| 1252 | } |
| 1253 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1254 | 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] | 1255 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1256 | struct ceph_bvec_iter it = *bvec_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1257 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1258 | ceph_bvec_iter_advance(&it, off); |
| 1259 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1260 | zero_bvec(&bv); |
| 1261 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1265 | * Zero a range in @obj_req data buffer defined by a bio (list) or |
| 1266 | * bio_vec array. |
| 1267 | * |
| 1268 | * @off is relative to the start of the data buffer. |
| 1269 | */ |
| 1270 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1271 | u32 bytes) |
| 1272 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1273 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1274 | case OBJ_REQUEST_BIO: |
| 1275 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1276 | break; |
| 1277 | case OBJ_REQUEST_BVECS: |
| 1278 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1279 | break; |
| 1280 | default: |
| 1281 | rbd_assert(0); |
| 1282 | } |
| 1283 | } |
| 1284 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1285 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1286 | { |
| 1287 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1288 | |
| 1289 | return obj_request->img_offset < |
| 1290 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1291 | } |
| 1292 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1293 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1294 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1295 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1296 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1297 | kref_get(&obj_request->kref); |
| 1298 | } |
| 1299 | |
| 1300 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1301 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1302 | { |
| 1303 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1304 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1305 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1306 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1307 | } |
| 1308 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1309 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1310 | { |
| 1311 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1312 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1313 | kref_get(&img_request->kref); |
| 1314 | } |
| 1315 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1316 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1317 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1318 | static void rbd_img_request_destroy(struct kref *kref); |
| 1319 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1320 | { |
| 1321 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1322 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1323 | kref_read(&img_request->kref)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1324 | if (img_request_child_test(img_request)) |
| 1325 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1326 | else |
| 1327 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1331 | struct rbd_obj_request *obj_request) |
| 1332 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1333 | rbd_assert(obj_request->img_request == NULL); |
| 1334 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1335 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1336 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1337 | img_request->obj_request_count++; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1338 | img_request->pending_count++; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1339 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1340 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1344 | struct rbd_obj_request *obj_request) |
| 1345 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1346 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1347 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1348 | rbd_assert(img_request->obj_request_count > 0); |
| 1349 | img_request->obj_request_count--; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1350 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1351 | rbd_obj_request_put(obj_request); |
| 1352 | } |
| 1353 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1354 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1355 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1356 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1357 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1358 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
| 1359 | obj_request, obj_request->object_no, obj_request->offset, |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1360 | obj_request->length, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1361 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1362 | } |
| 1363 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1364 | /* |
| 1365 | * The default/initial value for all image request flags is 0. Each |
| 1366 | * is conditionally set to 1 at image request initialization time |
| 1367 | * and currently never change thereafter. |
| 1368 | */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1369 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1370 | { |
| 1371 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1372 | smp_mb(); |
| 1373 | } |
| 1374 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1375 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1376 | { |
| 1377 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1378 | smp_mb(); |
| 1379 | } |
| 1380 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1381 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1382 | { |
| 1383 | smp_mb(); |
| 1384 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1385 | } |
| 1386 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1387 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1388 | { |
| 1389 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1390 | smp_mb(); |
| 1391 | } |
| 1392 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1393 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1394 | { |
| 1395 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1396 | smp_mb(); |
| 1397 | } |
| 1398 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1399 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1400 | { |
| 1401 | smp_mb(); |
| 1402 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1403 | } |
| 1404 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1405 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
| 1406 | { |
| 1407 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1408 | |
| 1409 | return !obj_req->offset && |
| 1410 | obj_req->length == rbd_dev->layout.object_size; |
| 1411 | } |
| 1412 | |
| 1413 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
| 1414 | { |
| 1415 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1416 | |
| 1417 | return obj_req->offset + obj_req->length == |
| 1418 | rbd_dev->layout.object_size; |
| 1419 | } |
| 1420 | |
| 1421 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1422 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1423 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1424 | case OBJ_OP_READ: |
| 1425 | return false; |
| 1426 | case OBJ_OP_WRITE: |
| 1427 | case OBJ_OP_DISCARD: |
| 1428 | return true; |
| 1429 | default: |
| 1430 | rbd_assert(0); |
| 1431 | } |
| 1432 | } |
| 1433 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1434 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
| 1435 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1436 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1437 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1438 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1439 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1440 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1441 | osd_req->r_result, obj_req); |
| 1442 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1443 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1444 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1445 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1446 | obj_req->xferred = osd_req->r_result; |
| 1447 | else |
| 1448 | /* |
| 1449 | * Writes aren't allowed to return a data payload. In some |
| 1450 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1451 | * a stat response makes it through, but we don't care. |
| 1452 | */ |
| 1453 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1454 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1455 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1456 | } |
| 1457 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1458 | 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] | 1459 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1460 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1461 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1462 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1463 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1467 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1468 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1469 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1470 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Deepa Dinamani | 1134e09 | 2017-05-08 15:59:19 -0700 | [diff] [blame] | 1471 | ktime_get_real_ts(&osd_req->r_mtime); |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1472 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1473 | } |
| 1474 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1475 | static struct ceph_osd_request * |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1476 | 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] | 1477 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1478 | struct rbd_img_request *img_req = obj_req->img_request; |
| 1479 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1480 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1481 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1482 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1483 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1484 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1485 | req = ceph_osdc_alloc_request(osdc, |
| 1486 | (rbd_img_is_write(img_req) ? img_req->snapc : NULL), |
| 1487 | num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1488 | if (!req) |
| 1489 | return NULL; |
| 1490 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1491 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1492 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1493 | |
| 1494 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1495 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1496 | rbd_dev->header.object_prefix, obj_req->object_no)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1497 | goto err_req; |
| 1498 | |
| 1499 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1500 | goto err_req; |
| 1501 | |
| 1502 | return req; |
| 1503 | |
| 1504 | err_req: |
| 1505 | ceph_osdc_put_request(req); |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1509 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1510 | { |
| 1511 | ceph_osdc_put_request(osd_req); |
| 1512 | } |
| 1513 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1514 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1515 | { |
| 1516 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1517 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1518 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1519 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1520 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1521 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1522 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1523 | kref_init(&obj_request->kref); |
| 1524 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1525 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1526 | return obj_request; |
| 1527 | } |
| 1528 | |
| 1529 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1530 | { |
| 1531 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1532 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1533 | |
| 1534 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1535 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1536 | dout("%s: obj %p\n", __func__, obj_request); |
| 1537 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1538 | if (obj_request->osd_req) |
| 1539 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1540 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1541 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1542 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1543 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1544 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1545 | break; /* Nothing to do */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1546 | default: |
| 1547 | rbd_assert(0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1548 | } |
| 1549 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1550 | if (obj_request->copyup_bvecs) { |
| 1551 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1552 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1553 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1554 | } |
| 1555 | kfree(obj_request->copyup_bvecs); |
| 1556 | } |
Ilya Dryomov | f9dcbc4 | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1557 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1558 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1559 | } |
| 1560 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1561 | /* It's OK to call this for a device with no parent */ |
| 1562 | |
| 1563 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1564 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1565 | { |
| 1566 | rbd_dev_remove_parent(rbd_dev); |
| 1567 | rbd_spec_put(rbd_dev->parent_spec); |
| 1568 | rbd_dev->parent_spec = NULL; |
| 1569 | rbd_dev->parent_overlap = 0; |
| 1570 | } |
| 1571 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1572 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1573 | * Parent image reference counting is used to determine when an |
| 1574 | * image's parent fields can be safely torn down--after there are no |
| 1575 | * more in-flight requests to the parent image. When the last |
| 1576 | * reference is dropped, cleaning them up is safe. |
| 1577 | */ |
| 1578 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1579 | { |
| 1580 | int counter; |
| 1581 | |
| 1582 | if (!rbd_dev->parent_spec) |
| 1583 | return; |
| 1584 | |
| 1585 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1586 | if (counter > 0) |
| 1587 | return; |
| 1588 | |
| 1589 | /* Last reference; clean up parent data structures */ |
| 1590 | |
| 1591 | if (!counter) |
| 1592 | rbd_dev_unparent(rbd_dev); |
| 1593 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1594 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * If an image has a non-zero parent overlap, get a reference to its |
| 1599 | * parent. |
| 1600 | * |
| 1601 | * Returns true if the rbd device has a parent with a non-zero |
| 1602 | * overlap and a reference for it was successfully taken, or |
| 1603 | * false otherwise. |
| 1604 | */ |
| 1605 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1606 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1607 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1608 | |
| 1609 | if (!rbd_dev->parent_spec) |
| 1610 | return false; |
| 1611 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1612 | down_read(&rbd_dev->header_rwsem); |
| 1613 | if (rbd_dev->parent_overlap) |
| 1614 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1615 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1616 | |
| 1617 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1618 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1619 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1620 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1621 | } |
| 1622 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1623 | /* |
| 1624 | * Caller is responsible for filling in the list of object requests |
| 1625 | * that comprises the image request, and the Linux request pointer |
| 1626 | * (if there is one). |
| 1627 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1628 | static struct rbd_img_request *rbd_img_request_create( |
| 1629 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1630 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1631 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1632 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1633 | { |
| 1634 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1635 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1636 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1637 | if (!img_request) |
| 1638 | return NULL; |
| 1639 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1640 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1641 | img_request->op_type = op_type; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1642 | img_request->offset = offset; |
| 1643 | img_request->length = length; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1644 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1645 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1646 | else |
| 1647 | img_request->snapc = snapc; |
| 1648 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1649 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1650 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1651 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1652 | spin_lock_init(&img_request->completion_lock); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1653 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 1654 | kref_init(&img_request->kref); |
| 1655 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1656 | 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] | 1657 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1658 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1659 | return img_request; |
| 1660 | } |
| 1661 | |
| 1662 | static void rbd_img_request_destroy(struct kref *kref) |
| 1663 | { |
| 1664 | struct rbd_img_request *img_request; |
| 1665 | struct rbd_obj_request *obj_request; |
| 1666 | struct rbd_obj_request *next_obj_request; |
| 1667 | |
| 1668 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1669 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1670 | dout("%s: img %p\n", __func__, img_request); |
| 1671 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1672 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1673 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1674 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1675 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1676 | if (img_request_layered_test(img_request)) { |
| 1677 | img_request_layered_clear(img_request); |
| 1678 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1679 | } |
| 1680 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1681 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1682 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1683 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1684 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1685 | } |
| 1686 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1687 | static struct rbd_img_request *rbd_parent_request_create( |
| 1688 | struct rbd_obj_request *obj_request, |
| 1689 | u64 img_offset, u64 length) |
| 1690 | { |
| 1691 | struct rbd_img_request *parent_request; |
| 1692 | struct rbd_device *rbd_dev; |
| 1693 | |
| 1694 | rbd_assert(obj_request->img_request); |
| 1695 | rbd_dev = obj_request->img_request->rbd_dev; |
| 1696 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1697 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1698 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1699 | if (!parent_request) |
| 1700 | return NULL; |
| 1701 | |
| 1702 | img_request_child_set(parent_request); |
| 1703 | rbd_obj_request_get(obj_request); |
| 1704 | parent_request->obj_request = obj_request; |
| 1705 | |
| 1706 | return parent_request; |
| 1707 | } |
| 1708 | |
| 1709 | static void rbd_parent_request_destroy(struct kref *kref) |
| 1710 | { |
| 1711 | struct rbd_img_request *parent_request; |
| 1712 | struct rbd_obj_request *orig_request; |
| 1713 | |
| 1714 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 1715 | orig_request = parent_request->obj_request; |
| 1716 | |
| 1717 | parent_request->obj_request = NULL; |
| 1718 | rbd_obj_request_put(orig_request); |
| 1719 | img_request_child_clear(parent_request); |
| 1720 | |
| 1721 | rbd_img_request_destroy(kref); |
| 1722 | } |
| 1723 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1724 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1725 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1726 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1727 | case OBJ_REQUEST_BIO: |
| 1728 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1729 | &obj_req->bio_pos, |
| 1730 | obj_req->length); |
| 1731 | break; |
| 1732 | case OBJ_REQUEST_BVECS: |
| 1733 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
| 1734 | obj_req->length); |
| 1735 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1736 | &obj_req->bvec_pos); |
| 1737 | break; |
| 1738 | default: |
| 1739 | rbd_assert(0); |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1744 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1745 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1746 | if (!obj_req->osd_req) |
| 1747 | return -ENOMEM; |
| 1748 | |
| 1749 | osd_req_op_extent_init(obj_req->osd_req, 0, CEPH_OSD_OP_READ, |
| 1750 | obj_req->offset, obj_req->length, 0, 0); |
| 1751 | rbd_osd_req_setup_data(obj_req, 0); |
| 1752 | |
| 1753 | rbd_osd_req_format_read(obj_req); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | |
| 1757 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1758 | unsigned int which) |
| 1759 | { |
| 1760 | struct page **pages; |
| 1761 | |
| 1762 | /* |
| 1763 | * The response data for a STAT call consists of: |
| 1764 | * le64 length; |
| 1765 | * struct { |
| 1766 | * le32 tv_sec; |
| 1767 | * le32 tv_nsec; |
| 1768 | * } mtime; |
| 1769 | */ |
| 1770 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1771 | if (IS_ERR(pages)) |
| 1772 | return PTR_ERR(pages); |
| 1773 | |
| 1774 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1775 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1776 | 8 + sizeof(struct ceph_timespec), |
| 1777 | 0, false, true); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 1782 | unsigned int which) |
| 1783 | { |
| 1784 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1785 | u16 opcode; |
| 1786 | |
| 1787 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 1788 | rbd_dev->layout.object_size, |
| 1789 | rbd_dev->layout.object_size); |
| 1790 | |
| 1791 | if (rbd_obj_is_entire(obj_req)) |
| 1792 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1793 | else |
| 1794 | opcode = CEPH_OSD_OP_WRITE; |
| 1795 | |
| 1796 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
| 1797 | obj_req->offset, obj_req->length, 0, 0); |
| 1798 | rbd_osd_req_setup_data(obj_req, which++); |
| 1799 | |
| 1800 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1801 | rbd_osd_req_format_write(obj_req); |
| 1802 | } |
| 1803 | |
| 1804 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
| 1805 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1806 | unsigned int num_osd_ops, which = 0; |
| 1807 | int ret; |
| 1808 | |
| 1809 | if (obj_request_overlaps_parent(obj_req)) { |
| 1810 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1811 | num_osd_ops = 3; /* stat + setallochint + write/writefull */ |
| 1812 | } else { |
| 1813 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1814 | num_osd_ops = 2; /* setallochint + write/writefull */ |
| 1815 | } |
| 1816 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1817 | 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] | 1818 | if (!obj_req->osd_req) |
| 1819 | return -ENOMEM; |
| 1820 | |
| 1821 | if (obj_request_overlaps_parent(obj_req)) { |
| 1822 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1823 | if (ret) |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
| 1827 | __rbd_obj_setup_write(obj_req, which); |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | static void __rbd_obj_setup_discard(struct rbd_obj_request *obj_req, |
| 1832 | unsigned int which) |
| 1833 | { |
| 1834 | u16 opcode; |
| 1835 | |
| 1836 | if (rbd_obj_is_entire(obj_req)) { |
| 1837 | if (obj_request_overlaps_parent(obj_req)) { |
| 1838 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1839 | } else { |
| 1840 | osd_req_op_init(obj_req->osd_req, which++, |
| 1841 | CEPH_OSD_OP_DELETE, 0); |
| 1842 | opcode = 0; |
| 1843 | } |
| 1844 | } else if (rbd_obj_is_tail(obj_req)) { |
| 1845 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1846 | } else { |
| 1847 | opcode = CEPH_OSD_OP_ZERO; |
| 1848 | } |
| 1849 | |
| 1850 | if (opcode) |
| 1851 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
| 1852 | obj_req->offset, obj_req->length, |
| 1853 | 0, 0); |
| 1854 | |
| 1855 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1856 | rbd_osd_req_format_write(obj_req); |
| 1857 | } |
| 1858 | |
| 1859 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 1860 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1861 | unsigned int num_osd_ops, which = 0; |
| 1862 | int ret; |
| 1863 | |
| 1864 | if (rbd_obj_is_entire(obj_req)) { |
| 1865 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1866 | num_osd_ops = 1; /* truncate/delete */ |
| 1867 | } else { |
| 1868 | if (obj_request_overlaps_parent(obj_req)) { |
| 1869 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1870 | num_osd_ops = 2; /* stat + truncate/zero */ |
| 1871 | } else { |
| 1872 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1873 | num_osd_ops = 1; /* truncate/zero */ |
| 1874 | } |
| 1875 | } |
| 1876 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1877 | 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] | 1878 | if (!obj_req->osd_req) |
| 1879 | return -ENOMEM; |
| 1880 | |
| 1881 | if (!rbd_obj_is_entire(obj_req) && |
| 1882 | obj_request_overlaps_parent(obj_req)) { |
| 1883 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1884 | if (ret) |
| 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | __rbd_obj_setup_discard(obj_req, which); |
| 1889 | return 0; |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * For each object request in @img_req, allocate an OSD request, add |
| 1894 | * individual OSD ops and prepare them for submission. The number of |
| 1895 | * OSD ops depends on op_type and the overlap point (if any). |
| 1896 | */ |
| 1897 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 1898 | { |
| 1899 | struct rbd_obj_request *obj_req; |
| 1900 | int ret; |
| 1901 | |
| 1902 | for_each_obj_request(img_req, obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1903 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1904 | case OBJ_OP_READ: |
| 1905 | ret = rbd_obj_setup_read(obj_req); |
| 1906 | break; |
| 1907 | case OBJ_OP_WRITE: |
| 1908 | ret = rbd_obj_setup_write(obj_req); |
| 1909 | break; |
| 1910 | case OBJ_OP_DISCARD: |
| 1911 | ret = rbd_obj_setup_discard(obj_req); |
| 1912 | break; |
| 1913 | default: |
| 1914 | rbd_assert(0); |
| 1915 | } |
| 1916 | if (ret) |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1923 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1924 | * Split up an image request into one or more object requests, each |
| 1925 | * to a different object. The "type" parameter indicates whether |
| 1926 | * "data_desc" is the pointer to the head of a list of bio |
| 1927 | * structures, or the base of a page array. In either case this |
| 1928 | * function assumes data_desc describes memory sufficient to hold |
| 1929 | * all data described by the image request. |
| 1930 | */ |
| 1931 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 1932 | enum obj_request_type type, |
| 1933 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1934 | { |
| 1935 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 1936 | struct rbd_obj_request *obj_request = NULL; |
| 1937 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1938 | struct ceph_bio_iter bio_it; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1939 | struct ceph_bvec_iter bvec_it; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1940 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1941 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1942 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1943 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 1944 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1945 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1946 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1947 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 1948 | rbd_assert(resid > 0); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1949 | |
| 1950 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1951 | bio_it = *(struct ceph_bio_iter *)data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1952 | rbd_assert(img_offset == |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1953 | bio_it.iter.bi_sector << SECTOR_SHIFT); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1954 | } else if (type == OBJ_REQUEST_BVECS) { |
| 1955 | bvec_it = *(struct ceph_bvec_iter *)data_desc; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1956 | } |
| 1957 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1958 | while (resid) { |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1959 | u64 object_no = img_offset >> rbd_dev->header.obj_order; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1960 | u64 offset = rbd_segment_offset(rbd_dev, img_offset); |
| 1961 | u64 length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1962 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1963 | obj_request = rbd_obj_request_create(); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1964 | if (!obj_request) |
| 1965 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 1966 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1967 | obj_request->object_no = object_no; |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1968 | obj_request->offset = offset; |
| 1969 | obj_request->length = length; |
| 1970 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 1971 | /* |
| 1972 | * set obj_request->img_request before creating the |
| 1973 | * osd_request so that it gets the right snapc |
| 1974 | */ |
| 1975 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1976 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1977 | if (type == OBJ_REQUEST_BIO) { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1978 | obj_request->bio_pos = bio_it; |
| 1979 | ceph_bio_iter_advance(&bio_it, length); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1980 | } else if (type == OBJ_REQUEST_BVECS) { |
| 1981 | obj_request->bvec_pos = bvec_it; |
| 1982 | ceph_bvec_iter_shorten(&obj_request->bvec_pos, length); |
| 1983 | ceph_bvec_iter_advance(&bvec_it, length); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1984 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1985 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1986 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1987 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1988 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1989 | resid -= length; |
| 1990 | } |
| 1991 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 1992 | img_request->data_type = type; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1993 | return __rbd_img_fill_request(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1994 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1995 | out_unwind: |
| 1996 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 1997 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1998 | |
| 1999 | return -ENOMEM; |
| 2000 | } |
| 2001 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2002 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2003 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2004 | struct rbd_obj_request *obj_request; |
| 2005 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2006 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2007 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2008 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2009 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2010 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2011 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2012 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2013 | } |
| 2014 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2015 | static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req, |
| 2016 | u64 img_offset, u32 bytes) |
| 2017 | { |
| 2018 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2019 | struct rbd_img_request *child_img_req; |
| 2020 | int ret; |
| 2021 | |
| 2022 | child_img_req = rbd_parent_request_create(obj_req, img_offset, bytes); |
| 2023 | if (!child_img_req) |
| 2024 | return -ENOMEM; |
| 2025 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2026 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame^] | 2027 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2028 | case OBJ_REQUEST_BIO: |
| 2029 | ret = rbd_img_request_fill(child_img_req, |
| 2030 | OBJ_REQUEST_BIO, |
| 2031 | &obj_req->bio_pos); |
| 2032 | break; |
| 2033 | case OBJ_REQUEST_BVECS: |
| 2034 | ret = rbd_img_request_fill(child_img_req, |
| 2035 | OBJ_REQUEST_BVECS, |
| 2036 | &obj_req->bvec_pos); |
| 2037 | break; |
| 2038 | default: |
| 2039 | rbd_assert(0); |
| 2040 | } |
| 2041 | } else { |
| 2042 | struct ceph_bvec_iter it = { |
| 2043 | .bvecs = obj_req->copyup_bvecs, |
| 2044 | .iter = { .bi_size = bytes }, |
| 2045 | }; |
| 2046 | |
| 2047 | ret = rbd_img_request_fill(child_img_req, OBJ_REQUEST_BVECS, |
| 2048 | &it); |
| 2049 | } |
| 2050 | if (ret) { |
| 2051 | rbd_img_request_put(child_img_req); |
| 2052 | return ret; |
| 2053 | } |
| 2054 | |
| 2055 | rbd_img_request_submit(child_img_req); |
| 2056 | return 0; |
| 2057 | } |
| 2058 | |
| 2059 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2060 | { |
| 2061 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2062 | int ret; |
| 2063 | |
| 2064 | if (obj_req->result == -ENOENT && |
| 2065 | obj_req->img_offset < rbd_dev->parent_overlap && |
| 2066 | !obj_req->tried_parent) { |
| 2067 | u64 obj_overlap = min(obj_req->length, |
| 2068 | rbd_dev->parent_overlap - obj_req->img_offset); |
| 2069 | |
| 2070 | obj_req->tried_parent = true; |
| 2071 | ret = rbd_obj_read_from_parent(obj_req, obj_req->img_offset, |
| 2072 | obj_overlap); |
| 2073 | if (ret) { |
| 2074 | obj_req->result = ret; |
| 2075 | return true; |
| 2076 | } |
| 2077 | return false; |
| 2078 | } |
| 2079 | |
| 2080 | /* |
| 2081 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2082 | * length of the request. A short read also implies zero-fill |
| 2083 | * to the end of the request. In both cases we update xferred |
| 2084 | * count to indicate the whole request was satisfied. |
| 2085 | */ |
| 2086 | if (obj_req->result == -ENOENT || |
| 2087 | (!obj_req->result && obj_req->xferred < obj_req->length)) { |
| 2088 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2089 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
| 2090 | obj_req->length - obj_req->xferred); |
| 2091 | obj_req->result = 0; |
| 2092 | obj_req->xferred = obj_req->length; |
| 2093 | } |
| 2094 | |
| 2095 | return true; |
| 2096 | } |
| 2097 | |
| 2098 | /* |
| 2099 | * copyup_bvecs pages are never highmem pages |
| 2100 | */ |
| 2101 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2102 | { |
| 2103 | struct ceph_bvec_iter it = { |
| 2104 | .bvecs = bvecs, |
| 2105 | .iter = { .bi_size = bytes }, |
| 2106 | }; |
| 2107 | |
| 2108 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2109 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2110 | bv.bv_len)) |
| 2111 | return false; |
| 2112 | })); |
| 2113 | return true; |
| 2114 | } |
| 2115 | |
| 2116 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2117 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2118 | unsigned int num_osd_ops = obj_req->osd_req->r_num_ops; |
| 2119 | |
| 2120 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2121 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
| 2122 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2123 | |
| 2124 | /* |
| 2125 | * Create a copyup request with the same number of OSD ops as |
| 2126 | * the original request. The original request was stat + op(s), |
| 2127 | * the new copyup request will be copyup + the same op(s). |
| 2128 | */ |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2129 | 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] | 2130 | if (!obj_req->osd_req) |
| 2131 | return -ENOMEM; |
| 2132 | |
| 2133 | /* |
| 2134 | * Only send non-zero copyup data to save some I/O and network |
| 2135 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2136 | * existing. |
| 2137 | */ |
| 2138 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2139 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2140 | bytes = 0; |
| 2141 | } |
| 2142 | |
| 2143 | osd_req_op_cls_init(obj_req->osd_req, 0, CEPH_OSD_OP_CALL, "rbd", |
| 2144 | "copyup"); |
| 2145 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
| 2146 | obj_req->copyup_bvecs, bytes); |
| 2147 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2148 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2149 | case OBJ_OP_WRITE: |
| 2150 | __rbd_obj_setup_write(obj_req, 1); |
| 2151 | break; |
| 2152 | case OBJ_OP_DISCARD: |
| 2153 | rbd_assert(!rbd_obj_is_entire(obj_req)); |
| 2154 | __rbd_obj_setup_discard(obj_req, 1); |
| 2155 | break; |
| 2156 | default: |
| 2157 | rbd_assert(0); |
| 2158 | } |
| 2159 | |
| 2160 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2161 | return 0; |
| 2162 | } |
| 2163 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2164 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2165 | { |
| 2166 | u32 i; |
| 2167 | |
| 2168 | rbd_assert(!obj_req->copyup_bvecs); |
| 2169 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2170 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2171 | sizeof(*obj_req->copyup_bvecs), |
| 2172 | GFP_NOIO); |
| 2173 | if (!obj_req->copyup_bvecs) |
| 2174 | return -ENOMEM; |
| 2175 | |
| 2176 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2177 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2178 | |
| 2179 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2180 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2181 | return -ENOMEM; |
| 2182 | |
| 2183 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2184 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2185 | obj_overlap -= len; |
| 2186 | } |
| 2187 | |
| 2188 | rbd_assert(!obj_overlap); |
| 2189 | return 0; |
| 2190 | } |
| 2191 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2192 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2193 | { |
| 2194 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2195 | u64 img_offset; |
| 2196 | u64 obj_overlap; |
| 2197 | int ret; |
| 2198 | |
| 2199 | if (!obj_request_overlaps_parent(obj_req)) { |
| 2200 | /* |
| 2201 | * The overlap has become 0 (most likely because the |
| 2202 | * image has been flattened). Use rbd_obj_issue_copyup() |
| 2203 | * to re-submit the original write request -- the copyup |
| 2204 | * operation itself will be a no-op, since someone must |
| 2205 | * have populated the child object while we weren't |
| 2206 | * looking. Move to WRITE_FLAT state as we'll be done |
| 2207 | * with the operation once the null copyup completes. |
| 2208 | */ |
| 2209 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2210 | return rbd_obj_issue_copyup(obj_req, 0); |
| 2211 | } |
| 2212 | |
| 2213 | /* |
| 2214 | * Determine the byte range covered by the object in the |
| 2215 | * child image to which the original request was to be sent. |
| 2216 | */ |
| 2217 | img_offset = obj_req->img_offset - obj_req->offset; |
| 2218 | obj_overlap = rbd_dev->layout.object_size; |
| 2219 | |
| 2220 | /* |
| 2221 | * There is no defined parent data beyond the parent |
| 2222 | * overlap, so limit what we read at that boundary if |
| 2223 | * necessary. |
| 2224 | */ |
| 2225 | if (img_offset + obj_overlap > rbd_dev->parent_overlap) { |
| 2226 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2227 | obj_overlap = rbd_dev->parent_overlap - img_offset; |
| 2228 | } |
| 2229 | |
| 2230 | ret = setup_copyup_bvecs(obj_req, obj_overlap); |
| 2231 | if (ret) |
| 2232 | return ret; |
| 2233 | |
| 2234 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
| 2235 | return rbd_obj_read_from_parent(obj_req, img_offset, obj_overlap); |
| 2236 | } |
| 2237 | |
| 2238 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2239 | { |
| 2240 | int ret; |
| 2241 | |
| 2242 | again: |
| 2243 | switch (obj_req->write_state) { |
| 2244 | case RBD_OBJ_WRITE_GUARD: |
| 2245 | rbd_assert(!obj_req->xferred); |
| 2246 | if (obj_req->result == -ENOENT) { |
| 2247 | /* |
| 2248 | * The target object doesn't exist. Read the data for |
| 2249 | * the entire target object up to the overlap point (if |
| 2250 | * any) from the parent, so we can use it for a copyup. |
| 2251 | */ |
| 2252 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2253 | if (ret) { |
| 2254 | obj_req->result = ret; |
| 2255 | return true; |
| 2256 | } |
| 2257 | return false; |
| 2258 | } |
| 2259 | /* fall through */ |
| 2260 | case RBD_OBJ_WRITE_FLAT: |
| 2261 | if (!obj_req->result) |
| 2262 | /* |
| 2263 | * There is no such thing as a successful short |
| 2264 | * write -- indicate the whole request was satisfied. |
| 2265 | */ |
| 2266 | obj_req->xferred = obj_req->length; |
| 2267 | return true; |
| 2268 | case RBD_OBJ_WRITE_COPYUP: |
| 2269 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2270 | if (obj_req->result) |
| 2271 | goto again; |
| 2272 | |
| 2273 | rbd_assert(obj_req->xferred); |
| 2274 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2275 | if (ret) { |
| 2276 | obj_req->result = ret; |
| 2277 | return true; |
| 2278 | } |
| 2279 | return false; |
| 2280 | default: |
| 2281 | rbd_assert(0); |
| 2282 | } |
| 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Returns true if @obj_req is completed, or false otherwise. |
| 2287 | */ |
| 2288 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2289 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2290 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2291 | case OBJ_OP_READ: |
| 2292 | return rbd_obj_handle_read(obj_req); |
| 2293 | case OBJ_OP_WRITE: |
| 2294 | return rbd_obj_handle_write(obj_req); |
| 2295 | case OBJ_OP_DISCARD: |
| 2296 | if (rbd_obj_handle_write(obj_req)) { |
| 2297 | /* |
| 2298 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2299 | * a non-existent object is not a problem. |
| 2300 | */ |
| 2301 | if (obj_req->result == -ENOENT) { |
| 2302 | obj_req->result = 0; |
| 2303 | obj_req->xferred = obj_req->length; |
| 2304 | } |
| 2305 | return true; |
| 2306 | } |
| 2307 | return false; |
| 2308 | default: |
| 2309 | rbd_assert(0); |
| 2310 | } |
| 2311 | } |
| 2312 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2313 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2314 | { |
| 2315 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2316 | |
| 2317 | rbd_assert((!obj_req->result && |
| 2318 | obj_req->xferred == obj_req->length) || |
| 2319 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2320 | if (!obj_req->result) { |
| 2321 | img_req->xferred += obj_req->xferred; |
| 2322 | return; |
| 2323 | } |
| 2324 | |
| 2325 | rbd_warn(img_req->rbd_dev, |
| 2326 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
| 2327 | obj_op_name(img_req->op_type), obj_req->object_no, |
| 2328 | obj_req->offset, obj_req->length, obj_req->result, |
| 2329 | obj_req->xferred); |
| 2330 | if (!img_req->result) { |
| 2331 | img_req->result = obj_req->result; |
| 2332 | img_req->xferred = 0; |
| 2333 | } |
| 2334 | } |
| 2335 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2336 | static void rbd_img_end_child_request(struct rbd_img_request *img_req) |
| 2337 | { |
| 2338 | struct rbd_obj_request *obj_req = img_req->obj_request; |
| 2339 | |
| 2340 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2341 | |
| 2342 | obj_req->result = img_req->result; |
| 2343 | obj_req->xferred = img_req->xferred; |
| 2344 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2345 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2346 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2347 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2348 | { |
| 2349 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2350 | rbd_assert((!img_req->result && |
| 2351 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2352 | (img_req->result < 0 && !img_req->xferred)); |
| 2353 | |
| 2354 | blk_mq_end_request(img_req->rq, |
| 2355 | errno_to_blk_status(img_req->result)); |
| 2356 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2360 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2361 | struct rbd_img_request *img_req; |
| 2362 | |
| 2363 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2364 | if (!__rbd_obj_handle_request(obj_req)) |
| 2365 | return; |
| 2366 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2367 | img_req = obj_req->img_request; |
| 2368 | spin_lock(&img_req->completion_lock); |
| 2369 | rbd_obj_end_request(obj_req); |
| 2370 | rbd_assert(img_req->pending_count); |
| 2371 | if (--img_req->pending_count) { |
| 2372 | spin_unlock(&img_req->completion_lock); |
| 2373 | return; |
| 2374 | } |
| 2375 | |
| 2376 | spin_unlock(&img_req->completion_lock); |
| 2377 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2378 | obj_req = img_req->obj_request; |
| 2379 | rbd_img_end_child_request(img_req); |
| 2380 | goto again; |
| 2381 | } |
| 2382 | rbd_img_end_request(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2383 | } |
| 2384 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2385 | static const struct rbd_client_id rbd_empty_cid; |
| 2386 | |
| 2387 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2388 | const struct rbd_client_id *rhs) |
| 2389 | { |
| 2390 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2391 | } |
| 2392 | |
| 2393 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2394 | { |
| 2395 | struct rbd_client_id cid; |
| 2396 | |
| 2397 | mutex_lock(&rbd_dev->watch_mutex); |
| 2398 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2399 | cid.handle = rbd_dev->watch_cookie; |
| 2400 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2401 | return cid; |
| 2402 | } |
| 2403 | |
| 2404 | /* |
| 2405 | * lock_rwsem must be held for write |
| 2406 | */ |
| 2407 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2408 | const struct rbd_client_id *cid) |
| 2409 | { |
| 2410 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2411 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2412 | cid->gid, cid->handle); |
| 2413 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2414 | } |
| 2415 | |
| 2416 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2417 | { |
| 2418 | mutex_lock(&rbd_dev->watch_mutex); |
| 2419 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2420 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2421 | } |
| 2422 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2423 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2424 | { |
| 2425 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2426 | |
| 2427 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2428 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2429 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2430 | } |
| 2431 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2432 | /* |
| 2433 | * lock_rwsem must be held for write |
| 2434 | */ |
| 2435 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2436 | { |
| 2437 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2438 | char cookie[32]; |
| 2439 | int ret; |
| 2440 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2441 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2442 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2443 | |
| 2444 | format_lock_cookie(rbd_dev, cookie); |
| 2445 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2446 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2447 | RBD_LOCK_TAG, "", 0); |
| 2448 | if (ret) |
| 2449 | return ret; |
| 2450 | |
| 2451 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2452 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2453 | return 0; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * lock_rwsem must be held for write |
| 2458 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2459 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2460 | { |
| 2461 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2462 | int ret; |
| 2463 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2464 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2465 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2466 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2467 | 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] | 2468 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2469 | if (ret && ret != -ENOENT) |
| 2470 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2471 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2472 | /* treat errors as the image is unlocked */ |
| 2473 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2474 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2475 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2476 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2480 | enum rbd_notify_op notify_op, |
| 2481 | struct page ***preply_pages, |
| 2482 | size_t *preply_len) |
| 2483 | { |
| 2484 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2485 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2486 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 2487 | char buf[buf_size]; |
| 2488 | void *p = buf; |
| 2489 | |
| 2490 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2491 | |
| 2492 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2493 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2494 | ceph_encode_32(&p, notify_op); |
| 2495 | ceph_encode_64(&p, cid.gid); |
| 2496 | ceph_encode_64(&p, cid.handle); |
| 2497 | |
| 2498 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2499 | &rbd_dev->header_oloc, buf, buf_size, |
| 2500 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2501 | } |
| 2502 | |
| 2503 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2504 | enum rbd_notify_op notify_op) |
| 2505 | { |
| 2506 | struct page **reply_pages; |
| 2507 | size_t reply_len; |
| 2508 | |
| 2509 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2510 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2511 | } |
| 2512 | |
| 2513 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2514 | { |
| 2515 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2516 | acquired_lock_work); |
| 2517 | |
| 2518 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2519 | } |
| 2520 | |
| 2521 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2522 | { |
| 2523 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2524 | released_lock_work); |
| 2525 | |
| 2526 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2527 | } |
| 2528 | |
| 2529 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2530 | { |
| 2531 | struct page **reply_pages; |
| 2532 | size_t reply_len; |
| 2533 | bool lock_owner_responded = false; |
| 2534 | int ret; |
| 2535 | |
| 2536 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2537 | |
| 2538 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2539 | &reply_pages, &reply_len); |
| 2540 | if (ret && ret != -ETIMEDOUT) { |
| 2541 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2542 | goto out; |
| 2543 | } |
| 2544 | |
| 2545 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2546 | void *p = page_address(reply_pages[0]); |
| 2547 | void *const end = p + reply_len; |
| 2548 | u32 n; |
| 2549 | |
| 2550 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2551 | while (n--) { |
| 2552 | u8 struct_v; |
| 2553 | u32 len; |
| 2554 | |
| 2555 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2556 | p += 8 + 8; /* skip gid and cookie */ |
| 2557 | |
| 2558 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2559 | if (!len) |
| 2560 | continue; |
| 2561 | |
| 2562 | if (lock_owner_responded) { |
| 2563 | rbd_warn(rbd_dev, |
| 2564 | "duplicate lock owners detected"); |
| 2565 | ret = -EIO; |
| 2566 | goto out; |
| 2567 | } |
| 2568 | |
| 2569 | lock_owner_responded = true; |
| 2570 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2571 | &struct_v, &len); |
| 2572 | if (ret) { |
| 2573 | rbd_warn(rbd_dev, |
| 2574 | "failed to decode ResponseMessage: %d", |
| 2575 | ret); |
| 2576 | goto e_inval; |
| 2577 | } |
| 2578 | |
| 2579 | ret = ceph_decode_32(&p); |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | if (!lock_owner_responded) { |
| 2584 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 2585 | ret = -ETIMEDOUT; |
| 2586 | } |
| 2587 | |
| 2588 | out: |
| 2589 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2590 | return ret; |
| 2591 | |
| 2592 | e_inval: |
| 2593 | ret = -EINVAL; |
| 2594 | goto out; |
| 2595 | } |
| 2596 | |
| 2597 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 2598 | { |
| 2599 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 2600 | |
| 2601 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 2602 | if (wake_all) |
| 2603 | wake_up_all(&rbd_dev->lock_waitq); |
| 2604 | else |
| 2605 | wake_up(&rbd_dev->lock_waitq); |
| 2606 | } |
| 2607 | |
| 2608 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 2609 | struct ceph_locker **lockers, u32 *num_lockers) |
| 2610 | { |
| 2611 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2612 | u8 lock_type; |
| 2613 | char *lock_tag; |
| 2614 | int ret; |
| 2615 | |
| 2616 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2617 | |
| 2618 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 2619 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2620 | &lock_type, &lock_tag, lockers, num_lockers); |
| 2621 | if (ret) |
| 2622 | return ret; |
| 2623 | |
| 2624 | if (*num_lockers == 0) { |
| 2625 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 2626 | goto out; |
| 2627 | } |
| 2628 | |
| 2629 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 2630 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 2631 | lock_tag); |
| 2632 | ret = -EBUSY; |
| 2633 | goto out; |
| 2634 | } |
| 2635 | |
| 2636 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 2637 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 2638 | ret = -EBUSY; |
| 2639 | goto out; |
| 2640 | } |
| 2641 | |
| 2642 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 2643 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 2644 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 2645 | (*lockers)[0].id.cookie); |
| 2646 | ret = -EBUSY; |
| 2647 | goto out; |
| 2648 | } |
| 2649 | |
| 2650 | out: |
| 2651 | kfree(lock_tag); |
| 2652 | return ret; |
| 2653 | } |
| 2654 | |
| 2655 | static int find_watcher(struct rbd_device *rbd_dev, |
| 2656 | const struct ceph_locker *locker) |
| 2657 | { |
| 2658 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2659 | struct ceph_watch_item *watchers; |
| 2660 | u32 num_watchers; |
| 2661 | u64 cookie; |
| 2662 | int i; |
| 2663 | int ret; |
| 2664 | |
| 2665 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 2666 | &rbd_dev->header_oloc, &watchers, |
| 2667 | &num_watchers); |
| 2668 | if (ret) |
| 2669 | return ret; |
| 2670 | |
| 2671 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 2672 | for (i = 0; i < num_watchers; i++) { |
| 2673 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 2674 | sizeof(locker->info.addr)) && |
| 2675 | watchers[i].cookie == cookie) { |
| 2676 | struct rbd_client_id cid = { |
| 2677 | .gid = le64_to_cpu(watchers[i].name.num), |
| 2678 | .handle = cookie, |
| 2679 | }; |
| 2680 | |
| 2681 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 2682 | rbd_dev, cid.gid, cid.handle); |
| 2683 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2684 | ret = 1; |
| 2685 | goto out; |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 2690 | ret = 0; |
| 2691 | out: |
| 2692 | kfree(watchers); |
| 2693 | return ret; |
| 2694 | } |
| 2695 | |
| 2696 | /* |
| 2697 | * lock_rwsem must be held for write |
| 2698 | */ |
| 2699 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 2700 | { |
| 2701 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 2702 | struct ceph_locker *lockers; |
| 2703 | u32 num_lockers; |
| 2704 | int ret; |
| 2705 | |
| 2706 | for (;;) { |
| 2707 | ret = rbd_lock(rbd_dev); |
| 2708 | if (ret != -EBUSY) |
| 2709 | return ret; |
| 2710 | |
| 2711 | /* determine if the current lock holder is still alive */ |
| 2712 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 2713 | if (ret) |
| 2714 | return ret; |
| 2715 | |
| 2716 | if (num_lockers == 0) |
| 2717 | goto again; |
| 2718 | |
| 2719 | ret = find_watcher(rbd_dev, lockers); |
| 2720 | if (ret) { |
| 2721 | if (ret > 0) |
| 2722 | ret = 0; /* have to request lock */ |
| 2723 | goto out; |
| 2724 | } |
| 2725 | |
| 2726 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 2727 | ENTITY_NAME(lockers[0].id.name)); |
| 2728 | |
| 2729 | ret = ceph_monc_blacklist_add(&client->monc, |
| 2730 | &lockers[0].info.addr); |
| 2731 | if (ret) { |
| 2732 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 2733 | ENTITY_NAME(lockers[0].id.name), ret); |
| 2734 | goto out; |
| 2735 | } |
| 2736 | |
| 2737 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 2738 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2739 | lockers[0].id.cookie, |
| 2740 | &lockers[0].id.name); |
| 2741 | if (ret && ret != -ENOENT) |
| 2742 | goto out; |
| 2743 | |
| 2744 | again: |
| 2745 | ceph_free_lockers(lockers, num_lockers); |
| 2746 | } |
| 2747 | |
| 2748 | out: |
| 2749 | ceph_free_lockers(lockers, num_lockers); |
| 2750 | return ret; |
| 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 2755 | */ |
| 2756 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 2757 | int *pret) |
| 2758 | { |
| 2759 | enum rbd_lock_state lock_state; |
| 2760 | |
| 2761 | down_read(&rbd_dev->lock_rwsem); |
| 2762 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 2763 | rbd_dev->lock_state); |
| 2764 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 2765 | lock_state = rbd_dev->lock_state; |
| 2766 | up_read(&rbd_dev->lock_rwsem); |
| 2767 | return lock_state; |
| 2768 | } |
| 2769 | |
| 2770 | up_read(&rbd_dev->lock_rwsem); |
| 2771 | down_write(&rbd_dev->lock_rwsem); |
| 2772 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 2773 | rbd_dev->lock_state); |
| 2774 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 2775 | *pret = rbd_try_lock(rbd_dev); |
| 2776 | if (*pret) |
| 2777 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 2778 | } |
| 2779 | |
| 2780 | lock_state = rbd_dev->lock_state; |
| 2781 | up_write(&rbd_dev->lock_rwsem); |
| 2782 | return lock_state; |
| 2783 | } |
| 2784 | |
| 2785 | static void rbd_acquire_lock(struct work_struct *work) |
| 2786 | { |
| 2787 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 2788 | struct rbd_device, lock_dwork); |
| 2789 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 2790 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2791 | |
| 2792 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2793 | again: |
| 2794 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 2795 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 2796 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 2797 | wake_requests(rbd_dev, true); |
| 2798 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 2799 | rbd_dev, lock_state, ret); |
| 2800 | return; |
| 2801 | } |
| 2802 | |
| 2803 | ret = rbd_request_lock(rbd_dev); |
| 2804 | if (ret == -ETIMEDOUT) { |
| 2805 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2806 | } else if (ret == -EROFS) { |
| 2807 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 2808 | /* |
| 2809 | * If this is rbd_add_acquire_lock(), we want to fail |
| 2810 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 2811 | * want to block. |
| 2812 | */ |
| 2813 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 2814 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 2815 | /* wake "rbd map --exclusive" process */ |
| 2816 | wake_requests(rbd_dev, false); |
| 2817 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2818 | } else if (ret < 0) { |
| 2819 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 2820 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 2821 | RBD_RETRY_DELAY); |
| 2822 | } else { |
| 2823 | /* |
| 2824 | * lock owner acked, but resend if we don't see them |
| 2825 | * release the lock |
| 2826 | */ |
| 2827 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 2828 | rbd_dev); |
| 2829 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 2830 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * lock_rwsem must be held for write |
| 2836 | */ |
| 2837 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 2838 | { |
| 2839 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 2840 | rbd_dev->lock_state); |
| 2841 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 2842 | return false; |
| 2843 | |
| 2844 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 2845 | downgrade_write(&rbd_dev->lock_rwsem); |
| 2846 | /* |
| 2847 | * Ensure that all in-flight IO is flushed. |
| 2848 | * |
| 2849 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 2850 | * may be shared with other devices. |
| 2851 | */ |
| 2852 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 2853 | up_read(&rbd_dev->lock_rwsem); |
| 2854 | |
| 2855 | down_write(&rbd_dev->lock_rwsem); |
| 2856 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 2857 | rbd_dev->lock_state); |
| 2858 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 2859 | return false; |
| 2860 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2861 | rbd_unlock(rbd_dev); |
| 2862 | /* |
| 2863 | * Give others a chance to grab the lock - we would re-acquire |
| 2864 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 2865 | * otherwise. We need to ack our own notifications, so this |
| 2866 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 2867 | * after wake_requests() in rbd_handle_released_lock(). |
| 2868 | */ |
| 2869 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2870 | return true; |
| 2871 | } |
| 2872 | |
| 2873 | static void rbd_release_lock_work(struct work_struct *work) |
| 2874 | { |
| 2875 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2876 | unlock_work); |
| 2877 | |
| 2878 | down_write(&rbd_dev->lock_rwsem); |
| 2879 | rbd_release_lock(rbd_dev); |
| 2880 | up_write(&rbd_dev->lock_rwsem); |
| 2881 | } |
| 2882 | |
| 2883 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 2884 | void **p) |
| 2885 | { |
| 2886 | struct rbd_client_id cid = { 0 }; |
| 2887 | |
| 2888 | if (struct_v >= 2) { |
| 2889 | cid.gid = ceph_decode_64(p); |
| 2890 | cid.handle = ceph_decode_64(p); |
| 2891 | } |
| 2892 | |
| 2893 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 2894 | cid.handle); |
| 2895 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 2896 | down_write(&rbd_dev->lock_rwsem); |
| 2897 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 2898 | /* |
| 2899 | * we already know that the remote client is |
| 2900 | * the owner |
| 2901 | */ |
| 2902 | up_write(&rbd_dev->lock_rwsem); |
| 2903 | return; |
| 2904 | } |
| 2905 | |
| 2906 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2907 | downgrade_write(&rbd_dev->lock_rwsem); |
| 2908 | } else { |
| 2909 | down_read(&rbd_dev->lock_rwsem); |
| 2910 | } |
| 2911 | |
| 2912 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 2913 | wake_requests(rbd_dev, false); |
| 2914 | up_read(&rbd_dev->lock_rwsem); |
| 2915 | } |
| 2916 | |
| 2917 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 2918 | void **p) |
| 2919 | { |
| 2920 | struct rbd_client_id cid = { 0 }; |
| 2921 | |
| 2922 | if (struct_v >= 2) { |
| 2923 | cid.gid = ceph_decode_64(p); |
| 2924 | cid.handle = ceph_decode_64(p); |
| 2925 | } |
| 2926 | |
| 2927 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 2928 | cid.handle); |
| 2929 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 2930 | down_write(&rbd_dev->lock_rwsem); |
| 2931 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 2932 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 2933 | __func__, rbd_dev, cid.gid, cid.handle, |
| 2934 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 2935 | up_write(&rbd_dev->lock_rwsem); |
| 2936 | return; |
| 2937 | } |
| 2938 | |
| 2939 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2940 | downgrade_write(&rbd_dev->lock_rwsem); |
| 2941 | } else { |
| 2942 | down_read(&rbd_dev->lock_rwsem); |
| 2943 | } |
| 2944 | |
| 2945 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 2946 | wake_requests(rbd_dev, false); |
| 2947 | up_read(&rbd_dev->lock_rwsem); |
| 2948 | } |
| 2949 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2950 | /* |
| 2951 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 2952 | * ResponseMessage is needed. |
| 2953 | */ |
| 2954 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 2955 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2956 | { |
| 2957 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 2958 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2959 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2960 | |
| 2961 | if (struct_v >= 2) { |
| 2962 | cid.gid = ceph_decode_64(p); |
| 2963 | cid.handle = ceph_decode_64(p); |
| 2964 | } |
| 2965 | |
| 2966 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 2967 | cid.handle); |
| 2968 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2969 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2970 | |
| 2971 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2972 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 2973 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 2974 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 2975 | goto out_unlock; |
| 2976 | |
| 2977 | /* |
| 2978 | * encode ResponseMessage(0) so the peer can detect |
| 2979 | * a missing owner |
| 2980 | */ |
| 2981 | result = 0; |
| 2982 | |
| 2983 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2984 | if (!rbd_dev->opts->exclusive) { |
| 2985 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 2986 | __func__, rbd_dev); |
| 2987 | queue_work(rbd_dev->task_wq, |
| 2988 | &rbd_dev->unlock_work); |
| 2989 | } else { |
| 2990 | /* refuse to release the lock */ |
| 2991 | result = -EROFS; |
| 2992 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2993 | } |
| 2994 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2995 | |
| 2996 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2997 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2998 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3002 | u64 notify_id, u64 cookie, s32 *result) |
| 3003 | { |
| 3004 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3005 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3006 | char buf[buf_size]; |
| 3007 | int ret; |
| 3008 | |
| 3009 | if (result) { |
| 3010 | void *p = buf; |
| 3011 | |
| 3012 | /* encode ResponseMessage */ |
| 3013 | ceph_start_encoding(&p, 1, 1, |
| 3014 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3015 | ceph_encode_32(&p, *result); |
| 3016 | } else { |
| 3017 | buf_size = 0; |
| 3018 | } |
| 3019 | |
| 3020 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3021 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3022 | buf, buf_size); |
| 3023 | if (ret) |
| 3024 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3025 | } |
| 3026 | |
| 3027 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3028 | u64 cookie) |
| 3029 | { |
| 3030 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3031 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3032 | } |
| 3033 | |
| 3034 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3035 | u64 notify_id, u64 cookie, s32 result) |
| 3036 | { |
| 3037 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3038 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3039 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3040 | |
| 3041 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3042 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3043 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3044 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3045 | void *p = data; |
| 3046 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3047 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3048 | u32 len; |
| 3049 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3050 | int ret; |
| 3051 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3052 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3053 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3054 | if (data_len) { |
| 3055 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3056 | &struct_v, &len); |
| 3057 | if (ret) { |
| 3058 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3059 | ret); |
| 3060 | return; |
| 3061 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3062 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3063 | notify_op = ceph_decode_32(&p); |
| 3064 | } else { |
| 3065 | /* legacy notification for header updates */ |
| 3066 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3067 | len = 0; |
| 3068 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3069 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3070 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3071 | switch (notify_op) { |
| 3072 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3073 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3074 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3075 | break; |
| 3076 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3077 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3078 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3079 | break; |
| 3080 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3081 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3082 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3083 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3084 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3085 | else |
| 3086 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3087 | break; |
| 3088 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3089 | ret = rbd_dev_refresh(rbd_dev); |
| 3090 | if (ret) |
| 3091 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3092 | |
| 3093 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3094 | break; |
| 3095 | default: |
| 3096 | if (rbd_is_lock_owner(rbd_dev)) |
| 3097 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3098 | cookie, -EOPNOTSUPP); |
| 3099 | else |
| 3100 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3101 | break; |
| 3102 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3103 | } |
| 3104 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3105 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3106 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3107 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3108 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3109 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3110 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3111 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3112 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3113 | down_write(&rbd_dev->lock_rwsem); |
| 3114 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3115 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3116 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3117 | mutex_lock(&rbd_dev->watch_mutex); |
| 3118 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3119 | __rbd_unregister_watch(rbd_dev); |
| 3120 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3121 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3122 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3123 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3124 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3128 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3129 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3130 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3131 | { |
| 3132 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3133 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3134 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3135 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3136 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3137 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3138 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3139 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3140 | rbd_watch_errcb, rbd_dev); |
| 3141 | if (IS_ERR(handle)) |
| 3142 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3143 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3144 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3145 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3146 | } |
| 3147 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3148 | /* |
| 3149 | * watch_mutex must be locked |
| 3150 | */ |
| 3151 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3152 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3153 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3154 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3155 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3156 | rbd_assert(rbd_dev->watch_handle); |
| 3157 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3158 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3159 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3160 | if (ret) |
| 3161 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3162 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3163 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3166 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3167 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3168 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3169 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3170 | mutex_lock(&rbd_dev->watch_mutex); |
| 3171 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3172 | ret = __rbd_register_watch(rbd_dev); |
| 3173 | if (ret) |
| 3174 | goto out; |
| 3175 | |
| 3176 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3177 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3178 | |
| 3179 | out: |
| 3180 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3181 | return ret; |
| 3182 | } |
| 3183 | |
| 3184 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3185 | { |
| 3186 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3187 | |
| 3188 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3189 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3190 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3191 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3192 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3193 | } |
| 3194 | |
| 3195 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3196 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3197 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3198 | cancel_tasks_sync(rbd_dev); |
| 3199 | |
| 3200 | mutex_lock(&rbd_dev->watch_mutex); |
| 3201 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3202 | __rbd_unregister_watch(rbd_dev); |
| 3203 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3204 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3205 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3206 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3207 | } |
| 3208 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3209 | /* |
| 3210 | * lock_rwsem must be held for write |
| 3211 | */ |
| 3212 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3213 | { |
| 3214 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3215 | char cookie[32]; |
| 3216 | int ret; |
| 3217 | |
| 3218 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3219 | |
| 3220 | format_lock_cookie(rbd_dev, cookie); |
| 3221 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3222 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3223 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3224 | RBD_LOCK_TAG, cookie); |
| 3225 | if (ret) { |
| 3226 | if (ret != -EOPNOTSUPP) |
| 3227 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3228 | ret); |
| 3229 | |
| 3230 | /* |
| 3231 | * Lock cookie cannot be updated on older OSDs, so do |
| 3232 | * a manual release and queue an acquire. |
| 3233 | */ |
| 3234 | if (rbd_release_lock(rbd_dev)) |
| 3235 | queue_delayed_work(rbd_dev->task_wq, |
| 3236 | &rbd_dev->lock_dwork, 0); |
| 3237 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3238 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3239 | } |
| 3240 | } |
| 3241 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3242 | static void rbd_reregister_watch(struct work_struct *work) |
| 3243 | { |
| 3244 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3245 | struct rbd_device, watch_dwork); |
| 3246 | int ret; |
| 3247 | |
| 3248 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3249 | |
| 3250 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3251 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3252 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3253 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3254 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3255 | |
| 3256 | ret = __rbd_register_watch(rbd_dev); |
| 3257 | if (ret) { |
| 3258 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3259 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3260 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3261 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3262 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3263 | queue_delayed_work(rbd_dev->task_wq, |
| 3264 | &rbd_dev->watch_dwork, |
| 3265 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3266 | } |
| 3267 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3268 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3272 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3273 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3274 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3275 | down_write(&rbd_dev->lock_rwsem); |
| 3276 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3277 | rbd_reacquire_lock(rbd_dev); |
| 3278 | up_write(&rbd_dev->lock_rwsem); |
| 3279 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3280 | ret = rbd_dev_refresh(rbd_dev); |
| 3281 | if (ret) |
| 3282 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3283 | } |
| 3284 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3285 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3286 | * Synchronous osd object method call. Returns the number of bytes |
| 3287 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3288 | */ |
| 3289 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3290 | struct ceph_object_id *oid, |
| 3291 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3292 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3293 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3294 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3295 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3296 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3297 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3298 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3299 | struct page *req_page = NULL; |
| 3300 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3301 | int ret; |
| 3302 | |
| 3303 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3304 | * Method calls are ultimately read operations. The result |
| 3305 | * should placed into the inbound buffer provided. They |
| 3306 | * also supply outbound data--parameters for the object |
| 3307 | * method. Currently if this is present it will be a |
| 3308 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3309 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3310 | if (outbound) { |
| 3311 | if (outbound_size > PAGE_SIZE) |
| 3312 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3313 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3314 | req_page = alloc_page(GFP_KERNEL); |
| 3315 | if (!req_page) |
| 3316 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3317 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3318 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3319 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3320 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3321 | reply_page = alloc_page(GFP_KERNEL); |
| 3322 | if (!reply_page) { |
| 3323 | if (req_page) |
| 3324 | __free_page(req_page); |
| 3325 | return -ENOMEM; |
| 3326 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3327 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3328 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3329 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3330 | reply_page, &inbound_size); |
| 3331 | if (!ret) { |
| 3332 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3333 | ret = inbound_size; |
| 3334 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3335 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3336 | if (req_page) |
| 3337 | __free_page(req_page); |
| 3338 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3339 | return ret; |
| 3340 | } |
| 3341 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3342 | /* |
| 3343 | * lock_rwsem must be held for read |
| 3344 | */ |
| 3345 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3346 | { |
| 3347 | DEFINE_WAIT(wait); |
| 3348 | |
| 3349 | do { |
| 3350 | /* |
| 3351 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3352 | * and cancel_delayed_work() in wake_requests(). |
| 3353 | */ |
| 3354 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3355 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3356 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3357 | TASK_UNINTERRUPTIBLE); |
| 3358 | up_read(&rbd_dev->lock_rwsem); |
| 3359 | schedule(); |
| 3360 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3361 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3362 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3363 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3364 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3365 | } |
| 3366 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3367 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3368 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3369 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3370 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3371 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3372 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3373 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3374 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3375 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3376 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3377 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3378 | int result; |
| 3379 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3380 | switch (req_op(rq)) { |
| 3381 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3382 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3383 | op_type = OBJ_OP_DISCARD; |
| 3384 | break; |
| 3385 | case REQ_OP_WRITE: |
| 3386 | op_type = OBJ_OP_WRITE; |
| 3387 | break; |
| 3388 | case REQ_OP_READ: |
| 3389 | op_type = OBJ_OP_READ; |
| 3390 | break; |
| 3391 | default: |
| 3392 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3393 | result = -EIO; |
| 3394 | goto err; |
| 3395 | } |
| 3396 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3397 | /* Ignore/skip any zero-length requests */ |
| 3398 | |
| 3399 | if (!length) { |
| 3400 | dout("%s: zero-length request\n", __func__); |
| 3401 | result = 0; |
| 3402 | goto err_rq; |
| 3403 | } |
| 3404 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3405 | rbd_assert(op_type == OBJ_OP_READ || |
| 3406 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3407 | |
| 3408 | /* |
| 3409 | * Quit early if the mapped snapshot no longer exists. It's |
| 3410 | * still possible the snapshot will have disappeared by the |
| 3411 | * time our request arrives at the osd, but there's no sense in |
| 3412 | * sending it if we already know. |
| 3413 | */ |
| 3414 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3415 | dout("request for non-existent snapshot"); |
| 3416 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3417 | result = -ENXIO; |
| 3418 | goto err_rq; |
| 3419 | } |
| 3420 | |
| 3421 | if (offset && length > U64_MAX - offset + 1) { |
| 3422 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3423 | length); |
| 3424 | result = -EINVAL; |
| 3425 | goto err_rq; /* Shouldn't happen */ |
| 3426 | } |
| 3427 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3428 | blk_mq_start_request(rq); |
| 3429 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3430 | down_read(&rbd_dev->header_rwsem); |
| 3431 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3432 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3433 | snapc = rbd_dev->header.snapc; |
| 3434 | ceph_get_snap_context(snapc); |
| 3435 | } |
| 3436 | up_read(&rbd_dev->header_rwsem); |
| 3437 | |
| 3438 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3439 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3440 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3441 | result = -EIO; |
| 3442 | goto err_rq; |
| 3443 | } |
| 3444 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3445 | must_be_locked = |
| 3446 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3447 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3448 | if (must_be_locked) { |
| 3449 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3450 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3451 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3452 | if (rbd_dev->opts->exclusive) { |
| 3453 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3454 | result = -EROFS; |
| 3455 | goto err_unlock; |
| 3456 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3457 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3458 | } |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3459 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3460 | result = -EBLACKLISTED; |
| 3461 | goto err_unlock; |
| 3462 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3463 | } |
| 3464 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3465 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3466 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3467 | if (!img_request) { |
| 3468 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3469 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3470 | } |
| 3471 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3472 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3473 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3474 | if (op_type == OBJ_OP_DISCARD) |
| 3475 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 3476 | NULL); |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 3477 | else { |
| 3478 | struct ceph_bio_iter bio_it = { .bio = rq->bio, |
| 3479 | .iter = rq->bio->bi_iter }; |
| 3480 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3481 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 3482 | &bio_it); |
| 3483 | } |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3484 | if (result) |
| 3485 | goto err_img_request; |
| 3486 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3487 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3488 | if (must_be_locked) |
| 3489 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3490 | return; |
| 3491 | |
| 3492 | err_img_request: |
| 3493 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3494 | err_unlock: |
| 3495 | if (must_be_locked) |
| 3496 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3497 | err_rq: |
| 3498 | if (result) |
| 3499 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3500 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3501 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3502 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3503 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3504 | } |
| 3505 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3506 | 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] | 3507 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3508 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3509 | struct request *rq = bd->rq; |
| 3510 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3511 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3512 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3513 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3514 | } |
| 3515 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3516 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3517 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3518 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3519 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3520 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3521 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3524 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3525 | struct ceph_object_id *oid, |
| 3526 | struct ceph_object_locator *oloc, |
| 3527 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3528 | |
| 3529 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3530 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3531 | struct ceph_osd_request *req; |
| 3532 | struct page **pages; |
| 3533 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3534 | int ret; |
| 3535 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3536 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3537 | if (!req) |
| 3538 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3539 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3540 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3541 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3542 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3543 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3544 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3545 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3546 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3547 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3548 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3549 | if (IS_ERR(pages)) { |
| 3550 | ret = PTR_ERR(pages); |
| 3551 | goto out_req; |
| 3552 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3553 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3554 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3555 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3556 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3557 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3558 | ceph_osdc_start_request(osdc, req, false); |
| 3559 | ret = ceph_osdc_wait_request(osdc, req); |
| 3560 | if (ret >= 0) |
| 3561 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3562 | |
| 3563 | out_req: |
| 3564 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3565 | return ret; |
| 3566 | } |
| 3567 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3568 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3569 | * Read the complete header for the given rbd device. On successful |
| 3570 | * return, the rbd_dev->header field will contain up-to-date |
| 3571 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3572 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3573 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3574 | { |
| 3575 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 3576 | u32 snap_count = 0; |
| 3577 | u64 names_size = 0; |
| 3578 | u32 want_count; |
| 3579 | int ret; |
| 3580 | |
| 3581 | /* |
| 3582 | * The complete header will include an array of its 64-bit |
| 3583 | * snapshot ids, followed by the names of those snapshots as |
| 3584 | * a contiguous block of NUL-terminated strings. Note that |
| 3585 | * the number of snapshots could change by the time we read |
| 3586 | * it in, in which case we re-read it. |
| 3587 | */ |
| 3588 | do { |
| 3589 | size_t size; |
| 3590 | |
| 3591 | kfree(ondisk); |
| 3592 | |
| 3593 | size = sizeof (*ondisk); |
| 3594 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 3595 | size += names_size; |
| 3596 | ondisk = kmalloc(size, GFP_KERNEL); |
| 3597 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3598 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3599 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3600 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 3601 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3602 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3603 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 3604 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3605 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3606 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 3607 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3608 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3609 | } |
| 3610 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 3611 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3612 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3613 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 3617 | want_count = snap_count; |
| 3618 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 3619 | } while (snap_count != want_count); |
| 3620 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3621 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 3622 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3623 | kfree(ondisk); |
| 3624 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3625 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3628 | /* |
| 3629 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 3630 | * has disappeared from the (just updated) snapshot context. |
| 3631 | */ |
| 3632 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 3633 | { |
| 3634 | u64 snap_id; |
| 3635 | |
| 3636 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 3637 | return; |
| 3638 | |
| 3639 | snap_id = rbd_dev->spec->snap_id; |
| 3640 | if (snap_id == CEPH_NOSNAP) |
| 3641 | return; |
| 3642 | |
| 3643 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 3644 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 3645 | } |
| 3646 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3647 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 3648 | { |
| 3649 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3650 | |
| 3651 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3652 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 3653 | * try to update its size. If REMOVING is set, updating size |
| 3654 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3655 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3656 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 3657 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3658 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 3659 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 3660 | set_capacity(rbd_dev->disk, size); |
| 3661 | revalidate_disk(rbd_dev->disk); |
| 3662 | } |
| 3663 | } |
| 3664 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3665 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3666 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3667 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3668 | int ret; |
| 3669 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3670 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 3671 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 3672 | |
| 3673 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3674 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3675 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3676 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3677 | /* |
| 3678 | * If there is a parent, see if it has disappeared due to the |
| 3679 | * mapped image getting flattened. |
| 3680 | */ |
| 3681 | if (rbd_dev->parent) { |
| 3682 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 3683 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3684 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3685 | } |
| 3686 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3687 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3688 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3689 | } else { |
| 3690 | /* validate mapped snapshot's EXISTS flag */ |
| 3691 | rbd_exists_validate(rbd_dev); |
| 3692 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3693 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3694 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3695 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3696 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3697 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3698 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3699 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3700 | } |
| 3701 | |
Christoph Hellwig | d6296d3 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 3702 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 3703 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3704 | { |
| 3705 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 3706 | |
| 3707 | INIT_WORK(work, rbd_queue_workfn); |
| 3708 | return 0; |
| 3709 | } |
| 3710 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 3711 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3712 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3713 | .init_request = rbd_init_request, |
| 3714 | }; |
| 3715 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3716 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 3717 | { |
| 3718 | struct gendisk *disk; |
| 3719 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3720 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3721 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3722 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3723 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3724 | disk = alloc_disk(single_major ? |
| 3725 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 3726 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3727 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3728 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3729 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3730 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3731 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3732 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3733 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3734 | if (single_major) |
| 3735 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3736 | disk->fops = &rbd_bd_ops; |
| 3737 | disk->private_data = rbd_dev; |
| 3738 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3739 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 3740 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3741 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3742 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3743 | 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] | 3744 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 3745 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 3746 | |
| 3747 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 3748 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3749 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3750 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3751 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 3752 | if (IS_ERR(q)) { |
| 3753 | err = PTR_ERR(q); |
| 3754 | goto out_tag_set; |
| 3755 | } |
| 3756 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 3757 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 3758 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3759 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3760 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3761 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 3762 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 3763 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 3764 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 3765 | blk_queue_max_segment_size(q, UINT_MAX); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3766 | blk_queue_io_min(q, segment_size); |
| 3767 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3768 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3769 | /* enable the discard support */ |
| 3770 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 3771 | q->limits.discard_granularity = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 3772 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3773 | blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3774 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3775 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 3776 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3777 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3778 | /* |
| 3779 | * disk_release() expects a queue ref from add_disk() and will |
| 3780 | * put it. Hold an extra ref until add_disk() is called. |
| 3781 | */ |
| 3782 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3783 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3784 | q->queuedata = rbd_dev; |
| 3785 | |
| 3786 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3787 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3788 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3789 | out_tag_set: |
| 3790 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3791 | out_disk: |
| 3792 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3793 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3796 | /* |
| 3797 | sysfs |
| 3798 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3799 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3800 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 3801 | { |
| 3802 | return container_of(dev, struct rbd_device, dev); |
| 3803 | } |
| 3804 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3805 | static ssize_t rbd_size_show(struct device *dev, |
| 3806 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3807 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3808 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3809 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3810 | return sprintf(buf, "%llu\n", |
| 3811 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3814 | /* |
| 3815 | * Note this shows the features for whatever's mapped, which is not |
| 3816 | * necessarily the base image. |
| 3817 | */ |
| 3818 | static ssize_t rbd_features_show(struct device *dev, |
| 3819 | struct device_attribute *attr, char *buf) |
| 3820 | { |
| 3821 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3822 | |
| 3823 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3824 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3825 | } |
| 3826 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3827 | static ssize_t rbd_major_show(struct device *dev, |
| 3828 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3829 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3830 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3831 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3832 | if (rbd_dev->major) |
| 3833 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 3834 | |
| 3835 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3836 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3837 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3838 | static ssize_t rbd_minor_show(struct device *dev, |
| 3839 | struct device_attribute *attr, char *buf) |
| 3840 | { |
| 3841 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3842 | |
| 3843 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3844 | } |
| 3845 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 3846 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 3847 | struct device_attribute *attr, char *buf) |
| 3848 | { |
| 3849 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3850 | struct ceph_entity_addr *client_addr = |
| 3851 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 3852 | |
| 3853 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 3854 | le32_to_cpu(client_addr->nonce)); |
| 3855 | } |
| 3856 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3857 | static ssize_t rbd_client_id_show(struct device *dev, |
| 3858 | struct device_attribute *attr, char *buf) |
| 3859 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3860 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3861 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 3862 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 3863 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3864 | } |
| 3865 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 3866 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 3867 | struct device_attribute *attr, char *buf) |
| 3868 | { |
| 3869 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3870 | |
| 3871 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 3872 | } |
| 3873 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 3874 | static ssize_t rbd_config_info_show(struct device *dev, |
| 3875 | struct device_attribute *attr, char *buf) |
| 3876 | { |
| 3877 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3878 | |
| 3879 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | static ssize_t rbd_pool_show(struct device *dev, |
| 3883 | struct device_attribute *attr, char *buf) |
| 3884 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3885 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3886 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3887 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3890 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 3891 | struct device_attribute *attr, char *buf) |
| 3892 | { |
| 3893 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3894 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3895 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3896 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3897 | } |
| 3898 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3899 | static ssize_t rbd_name_show(struct device *dev, |
| 3900 | struct device_attribute *attr, char *buf) |
| 3901 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3902 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3903 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 3904 | if (rbd_dev->spec->image_name) |
| 3905 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 3906 | |
| 3907 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3910 | static ssize_t rbd_image_id_show(struct device *dev, |
| 3911 | struct device_attribute *attr, char *buf) |
| 3912 | { |
| 3913 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3914 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3915 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3916 | } |
| 3917 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3918 | /* |
| 3919 | * Shows the name of the currently-mapped snapshot (or |
| 3920 | * RBD_SNAP_HEAD_NAME for the base image). |
| 3921 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3922 | static ssize_t rbd_snap_show(struct device *dev, |
| 3923 | struct device_attribute *attr, |
| 3924 | char *buf) |
| 3925 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3926 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3927 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 3928 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3929 | } |
| 3930 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 3931 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 3932 | struct device_attribute *attr, char *buf) |
| 3933 | { |
| 3934 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3935 | |
| 3936 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 3937 | } |
| 3938 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3939 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3940 | * For a v2 image, shows the chain of parent images, separated by empty |
| 3941 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 3942 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3943 | */ |
| 3944 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3945 | struct device_attribute *attr, |
| 3946 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3947 | { |
| 3948 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3949 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3950 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3951 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3952 | return sprintf(buf, "(no parent image)\n"); |
| 3953 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3954 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 3955 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3956 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3957 | count += sprintf(&buf[count], "%s" |
| 3958 | "pool_id %llu\npool_name %s\n" |
| 3959 | "image_id %s\nimage_name %s\n" |
| 3960 | "snap_id %llu\nsnap_name %s\n" |
| 3961 | "overlap %llu\n", |
| 3962 | !count ? "" : "\n", /* first? */ |
| 3963 | spec->pool_id, spec->pool_name, |
| 3964 | spec->image_id, spec->image_name ?: "(unknown)", |
| 3965 | spec->snap_id, spec->snap_name, |
| 3966 | rbd_dev->parent_overlap); |
| 3967 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3968 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 3969 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 3970 | } |
| 3971 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3972 | static ssize_t rbd_image_refresh(struct device *dev, |
| 3973 | struct device_attribute *attr, |
| 3974 | const char *buf, |
| 3975 | size_t size) |
| 3976 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3977 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3978 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3979 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3980 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3981 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3982 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3983 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3984 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3985 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3986 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3987 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3988 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3989 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3990 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 3991 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3992 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 3993 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 3994 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3995 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 3996 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3997 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 3998 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3999 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4000 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4001 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4002 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4003 | |
| 4004 | static struct attribute *rbd_attrs[] = { |
| 4005 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4006 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4007 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4008 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4009 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4010 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4011 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4012 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4013 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4014 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4015 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4016 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4017 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4018 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4019 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4020 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4021 | NULL |
| 4022 | }; |
| 4023 | |
| 4024 | static struct attribute_group rbd_attr_group = { |
| 4025 | .attrs = rbd_attrs, |
| 4026 | }; |
| 4027 | |
| 4028 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4029 | &rbd_attr_group, |
| 4030 | NULL |
| 4031 | }; |
| 4032 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4033 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4034 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4035 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4036 | .name = "rbd", |
| 4037 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4038 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4039 | }; |
| 4040 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4041 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4042 | { |
| 4043 | kref_get(&spec->kref); |
| 4044 | |
| 4045 | return spec; |
| 4046 | } |
| 4047 | |
| 4048 | static void rbd_spec_free(struct kref *kref); |
| 4049 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4050 | { |
| 4051 | if (spec) |
| 4052 | kref_put(&spec->kref, rbd_spec_free); |
| 4053 | } |
| 4054 | |
| 4055 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4056 | { |
| 4057 | struct rbd_spec *spec; |
| 4058 | |
| 4059 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4060 | if (!spec) |
| 4061 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4062 | |
| 4063 | spec->pool_id = CEPH_NOPOOL; |
| 4064 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4065 | kref_init(&spec->kref); |
| 4066 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4067 | return spec; |
| 4068 | } |
| 4069 | |
| 4070 | static void rbd_spec_free(struct kref *kref) |
| 4071 | { |
| 4072 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4073 | |
| 4074 | kfree(spec->pool_name); |
| 4075 | kfree(spec->image_id); |
| 4076 | kfree(spec->image_name); |
| 4077 | kfree(spec->snap_name); |
| 4078 | kfree(spec); |
| 4079 | } |
| 4080 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4081 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4082 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4083 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4084 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4085 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4086 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4087 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4088 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4089 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4090 | rbd_put_client(rbd_dev->rbd_client); |
| 4091 | rbd_spec_put(rbd_dev->spec); |
| 4092 | kfree(rbd_dev->opts); |
| 4093 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | static void rbd_dev_release(struct device *dev) |
| 4097 | { |
| 4098 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4099 | bool need_put = !!rbd_dev->opts; |
| 4100 | |
| 4101 | if (need_put) { |
| 4102 | destroy_workqueue(rbd_dev->task_wq); |
| 4103 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4104 | } |
| 4105 | |
| 4106 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4107 | |
| 4108 | /* |
| 4109 | * This is racy, but way better than putting module outside of |
| 4110 | * the release callback. The race window is pretty small, so |
| 4111 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4112 | */ |
| 4113 | if (need_put) |
| 4114 | module_put(THIS_MODULE); |
| 4115 | } |
| 4116 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4117 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4118 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4119 | { |
| 4120 | struct rbd_device *rbd_dev; |
| 4121 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4122 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4123 | if (!rbd_dev) |
| 4124 | return NULL; |
| 4125 | |
| 4126 | spin_lock_init(&rbd_dev->lock); |
| 4127 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4128 | init_rwsem(&rbd_dev->header_rwsem); |
| 4129 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4130 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4131 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4132 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4133 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4134 | mutex_init(&rbd_dev->watch_mutex); |
| 4135 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4136 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4137 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4138 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4139 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4140 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4141 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4142 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4143 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4144 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4145 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4146 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4147 | rbd_dev->dev.type = &rbd_device_type; |
| 4148 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4149 | device_initialize(&rbd_dev->dev); |
| 4150 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4151 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4152 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4153 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4154 | return rbd_dev; |
| 4155 | } |
| 4156 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4157 | /* |
| 4158 | * Create a mapping rbd_dev. |
| 4159 | */ |
| 4160 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4161 | struct rbd_spec *spec, |
| 4162 | struct rbd_options *opts) |
| 4163 | { |
| 4164 | struct rbd_device *rbd_dev; |
| 4165 | |
| 4166 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4167 | if (!rbd_dev) |
| 4168 | return NULL; |
| 4169 | |
| 4170 | rbd_dev->opts = opts; |
| 4171 | |
| 4172 | /* get an id and fill in device name */ |
| 4173 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4174 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4175 | GFP_KERNEL); |
| 4176 | if (rbd_dev->dev_id < 0) |
| 4177 | goto fail_rbd_dev; |
| 4178 | |
| 4179 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4180 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4181 | rbd_dev->name); |
| 4182 | if (!rbd_dev->task_wq) |
| 4183 | goto fail_dev_id; |
| 4184 | |
| 4185 | /* we have a ref from do_rbd_add() */ |
| 4186 | __module_get(THIS_MODULE); |
| 4187 | |
| 4188 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4189 | return rbd_dev; |
| 4190 | |
| 4191 | fail_dev_id: |
| 4192 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4193 | fail_rbd_dev: |
| 4194 | rbd_dev_free(rbd_dev); |
| 4195 | return NULL; |
| 4196 | } |
| 4197 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4198 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4199 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4200 | if (rbd_dev) |
| 4201 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4202 | } |
| 4203 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4204 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4205 | * Get the size and object order for an image snapshot, or if |
| 4206 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4207 | * image. |
| 4208 | */ |
| 4209 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4210 | u8 *order, u64 *snap_size) |
| 4211 | { |
| 4212 | __le64 snapid = cpu_to_le64(snap_id); |
| 4213 | int ret; |
| 4214 | struct { |
| 4215 | u8 order; |
| 4216 | __le64 size; |
| 4217 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4218 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4219 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4220 | &rbd_dev->header_oloc, "get_size", |
| 4221 | &snapid, sizeof(snapid), |
| 4222 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4223 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4224 | if (ret < 0) |
| 4225 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4226 | if (ret < sizeof (size_buf)) |
| 4227 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4228 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4229 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4230 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4231 | dout(" order %u", (unsigned int)*order); |
| 4232 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4233 | *snap_size = le64_to_cpu(size_buf.size); |
| 4234 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4235 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4236 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4237 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4238 | |
| 4239 | return 0; |
| 4240 | } |
| 4241 | |
| 4242 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4243 | { |
| 4244 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4245 | &rbd_dev->header.obj_order, |
| 4246 | &rbd_dev->header.image_size); |
| 4247 | } |
| 4248 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4249 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4250 | { |
| 4251 | void *reply_buf; |
| 4252 | int ret; |
| 4253 | void *p; |
| 4254 | |
| 4255 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4256 | if (!reply_buf) |
| 4257 | return -ENOMEM; |
| 4258 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4259 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4260 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4261 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4262 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4263 | if (ret < 0) |
| 4264 | goto out; |
| 4265 | |
| 4266 | p = reply_buf; |
| 4267 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4268 | p + ret, NULL, GFP_NOIO); |
| 4269 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4270 | |
| 4271 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4272 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4273 | rbd_dev->header.object_prefix = NULL; |
| 4274 | } else { |
| 4275 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4276 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4277 | out: |
| 4278 | kfree(reply_buf); |
| 4279 | |
| 4280 | return ret; |
| 4281 | } |
| 4282 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4283 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4284 | u64 *snap_features) |
| 4285 | { |
| 4286 | __le64 snapid = cpu_to_le64(snap_id); |
| 4287 | struct { |
| 4288 | __le64 features; |
| 4289 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4290 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4291 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4292 | int ret; |
| 4293 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4294 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4295 | &rbd_dev->header_oloc, "get_features", |
| 4296 | &snapid, sizeof(snapid), |
| 4297 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4298 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4299 | if (ret < 0) |
| 4300 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4301 | if (ret < sizeof (features_buf)) |
| 4302 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4303 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4304 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4305 | if (unsup) { |
| 4306 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4307 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4308 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4309 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4310 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4311 | *snap_features = le64_to_cpu(features_buf.features); |
| 4312 | |
| 4313 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4314 | (unsigned long long)snap_id, |
| 4315 | (unsigned long long)*snap_features, |
| 4316 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4317 | |
| 4318 | return 0; |
| 4319 | } |
| 4320 | |
| 4321 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4322 | { |
| 4323 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4324 | &rbd_dev->header.features); |
| 4325 | } |
| 4326 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4327 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4328 | { |
| 4329 | struct rbd_spec *parent_spec; |
| 4330 | size_t size; |
| 4331 | void *reply_buf = NULL; |
| 4332 | __le64 snapid; |
| 4333 | void *p; |
| 4334 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4335 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4336 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4337 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4338 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4339 | int ret; |
| 4340 | |
| 4341 | parent_spec = rbd_spec_alloc(); |
| 4342 | if (!parent_spec) |
| 4343 | return -ENOMEM; |
| 4344 | |
| 4345 | size = sizeof (__le64) + /* pool_id */ |
| 4346 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4347 | sizeof (__le64) + /* snap_id */ |
| 4348 | sizeof (__le64); /* overlap */ |
| 4349 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4350 | if (!reply_buf) { |
| 4351 | ret = -ENOMEM; |
| 4352 | goto out_err; |
| 4353 | } |
| 4354 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4355 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4356 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4357 | &rbd_dev->header_oloc, "get_parent", |
| 4358 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4359 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4360 | if (ret < 0) |
| 4361 | goto out_err; |
| 4362 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4363 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4364 | end = reply_buf + ret; |
| 4365 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4366 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4367 | if (pool_id == CEPH_NOPOOL) { |
| 4368 | /* |
| 4369 | * Either the parent never existed, or we have |
| 4370 | * record of it but the image got flattened so it no |
| 4371 | * longer has a parent. When the parent of a |
| 4372 | * layered image disappears we immediately set the |
| 4373 | * overlap to 0. The effect of this is that all new |
| 4374 | * requests will be treated as if the image had no |
| 4375 | * parent. |
| 4376 | */ |
| 4377 | if (rbd_dev->parent_overlap) { |
| 4378 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4379 | rbd_dev_parent_put(rbd_dev); |
| 4380 | pr_info("%s: clone image has been flattened\n", |
| 4381 | rbd_dev->disk->disk_name); |
| 4382 | } |
| 4383 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4384 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4385 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4386 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4387 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4388 | |
| 4389 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4390 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4391 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4392 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4393 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4394 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4395 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4396 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4397 | if (IS_ERR(image_id)) { |
| 4398 | ret = PTR_ERR(image_id); |
| 4399 | goto out_err; |
| 4400 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4401 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4402 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 4403 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4404 | /* |
| 4405 | * The parent won't change (except when the clone is |
| 4406 | * flattened, already handled that). So we only need to |
| 4407 | * record the parent spec we have not already done so. |
| 4408 | */ |
| 4409 | if (!rbd_dev->parent_spec) { |
| 4410 | parent_spec->pool_id = pool_id; |
| 4411 | parent_spec->image_id = image_id; |
| 4412 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4413 | rbd_dev->parent_spec = parent_spec; |
| 4414 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 4415 | } else { |
| 4416 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4420 | * We always update the parent overlap. If it's zero we issue |
| 4421 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4422 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4423 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4424 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4425 | /* refresh, careful to warn just once */ |
| 4426 | if (rbd_dev->parent_overlap) |
| 4427 | rbd_warn(rbd_dev, |
| 4428 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4429 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4430 | /* initial probe */ |
| 4431 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4432 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4433 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4434 | rbd_dev->parent_overlap = overlap; |
| 4435 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4436 | out: |
| 4437 | ret = 0; |
| 4438 | out_err: |
| 4439 | kfree(reply_buf); |
| 4440 | rbd_spec_put(parent_spec); |
| 4441 | |
| 4442 | return ret; |
| 4443 | } |
| 4444 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4445 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 4446 | { |
| 4447 | struct { |
| 4448 | __le64 stripe_unit; |
| 4449 | __le64 stripe_count; |
| 4450 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 4451 | size_t size = sizeof (striping_info_buf); |
| 4452 | void *p; |
| 4453 | u64 obj_size; |
| 4454 | u64 stripe_unit; |
| 4455 | u64 stripe_count; |
| 4456 | int ret; |
| 4457 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4458 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4459 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 4460 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4461 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 4462 | if (ret < 0) |
| 4463 | return ret; |
| 4464 | if (ret < size) |
| 4465 | return -ERANGE; |
| 4466 | |
| 4467 | /* |
| 4468 | * We don't actually support the "fancy striping" feature |
| 4469 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 4470 | * defaults the behavior is the same as before. So find |
| 4471 | * out, and only fail if the image has non-default values. |
| 4472 | */ |
| 4473 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4474 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4475 | p = &striping_info_buf; |
| 4476 | stripe_unit = ceph_decode_64(&p); |
| 4477 | if (stripe_unit != obj_size) { |
| 4478 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 4479 | "(got %llu want %llu)", |
| 4480 | stripe_unit, obj_size); |
| 4481 | return -EINVAL; |
| 4482 | } |
| 4483 | stripe_count = ceph_decode_64(&p); |
| 4484 | if (stripe_count != 1) { |
| 4485 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 4486 | "(got %llu want 1)", stripe_count); |
| 4487 | return -EINVAL; |
| 4488 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4489 | rbd_dev->header.stripe_unit = stripe_unit; |
| 4490 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4491 | |
| 4492 | return 0; |
| 4493 | } |
| 4494 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4495 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 4496 | { |
| 4497 | __le64 data_pool_id; |
| 4498 | int ret; |
| 4499 | |
| 4500 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4501 | &rbd_dev->header_oloc, "get_data_pool", |
| 4502 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 4503 | if (ret < 0) |
| 4504 | return ret; |
| 4505 | if (ret < sizeof(data_pool_id)) |
| 4506 | return -EBADMSG; |
| 4507 | |
| 4508 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 4509 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 4510 | return 0; |
| 4511 | } |
| 4512 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4513 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 4514 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4515 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4516 | size_t image_id_size; |
| 4517 | char *image_id; |
| 4518 | void *p; |
| 4519 | void *end; |
| 4520 | size_t size; |
| 4521 | void *reply_buf = NULL; |
| 4522 | size_t len = 0; |
| 4523 | char *image_name = NULL; |
| 4524 | int ret; |
| 4525 | |
| 4526 | rbd_assert(!rbd_dev->spec->image_name); |
| 4527 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4528 | len = strlen(rbd_dev->spec->image_id); |
| 4529 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4530 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 4531 | if (!image_id) |
| 4532 | return NULL; |
| 4533 | |
| 4534 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4535 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4536 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4537 | |
| 4538 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 4539 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4540 | if (!reply_buf) |
| 4541 | goto out; |
| 4542 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4543 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 4544 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 4545 | "dir_get_name", image_id, image_id_size, |
| 4546 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4547 | if (ret < 0) |
| 4548 | goto out; |
| 4549 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4550 | end = reply_buf + ret; |
| 4551 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4552 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 4553 | if (IS_ERR(image_name)) |
| 4554 | image_name = NULL; |
| 4555 | else |
| 4556 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 4557 | out: |
| 4558 | kfree(reply_buf); |
| 4559 | kfree(image_id); |
| 4560 | |
| 4561 | return image_name; |
| 4562 | } |
| 4563 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4564 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4565 | { |
| 4566 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4567 | const char *snap_name; |
| 4568 | u32 which = 0; |
| 4569 | |
| 4570 | /* Skip over names until we find the one we are looking for */ |
| 4571 | |
| 4572 | snap_name = rbd_dev->header.snap_names; |
| 4573 | while (which < snapc->num_snaps) { |
| 4574 | if (!strcmp(name, snap_name)) |
| 4575 | return snapc->snaps[which]; |
| 4576 | snap_name += strlen(snap_name) + 1; |
| 4577 | which++; |
| 4578 | } |
| 4579 | return CEPH_NOSNAP; |
| 4580 | } |
| 4581 | |
| 4582 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4583 | { |
| 4584 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4585 | u32 which; |
| 4586 | bool found = false; |
| 4587 | u64 snap_id; |
| 4588 | |
| 4589 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 4590 | const char *snap_name; |
| 4591 | |
| 4592 | snap_id = snapc->snaps[which]; |
| 4593 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 4594 | if (IS_ERR(snap_name)) { |
| 4595 | /* ignore no-longer existing snapshots */ |
| 4596 | if (PTR_ERR(snap_name) == -ENOENT) |
| 4597 | continue; |
| 4598 | else |
| 4599 | break; |
| 4600 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4601 | found = !strcmp(name, snap_name); |
| 4602 | kfree(snap_name); |
| 4603 | } |
| 4604 | return found ? snap_id : CEPH_NOSNAP; |
| 4605 | } |
| 4606 | |
| 4607 | /* |
| 4608 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 4609 | * no snapshot by that name is found, or if an error occurs. |
| 4610 | */ |
| 4611 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4612 | { |
| 4613 | if (rbd_dev->image_format == 1) |
| 4614 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 4615 | |
| 4616 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 4617 | } |
| 4618 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4619 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4620 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4621 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4622 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 4623 | { |
| 4624 | struct rbd_spec *spec = rbd_dev->spec; |
| 4625 | |
| 4626 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 4627 | rbd_assert(spec->image_id && spec->image_name); |
| 4628 | rbd_assert(spec->snap_name); |
| 4629 | |
| 4630 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 4631 | u64 snap_id; |
| 4632 | |
| 4633 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 4634 | if (snap_id == CEPH_NOSNAP) |
| 4635 | return -ENOENT; |
| 4636 | |
| 4637 | spec->snap_id = snap_id; |
| 4638 | } else { |
| 4639 | spec->snap_id = CEPH_NOSNAP; |
| 4640 | } |
| 4641 | |
| 4642 | return 0; |
| 4643 | } |
| 4644 | |
| 4645 | /* |
| 4646 | * A parent image will have all ids but none of the names. |
| 4647 | * |
| 4648 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 4649 | * can't figure out the name for an image id. |
| 4650 | */ |
| 4651 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4652 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4653 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4654 | struct rbd_spec *spec = rbd_dev->spec; |
| 4655 | const char *pool_name; |
| 4656 | const char *image_name; |
| 4657 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4658 | int ret; |
| 4659 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4660 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 4661 | rbd_assert(spec->image_id); |
| 4662 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4663 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4664 | /* 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] | 4665 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4666 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 4667 | if (!pool_name) { |
| 4668 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4669 | return -EIO; |
| 4670 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4671 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 4672 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4673 | return -ENOMEM; |
| 4674 | |
| 4675 | /* Fetch the image name; tolerate failure here */ |
| 4676 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4677 | image_name = rbd_dev_image_name(rbd_dev); |
| 4678 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4679 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4680 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4681 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4682 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4683 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 4684 | if (IS_ERR(snap_name)) { |
| 4685 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4686 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | spec->pool_name = pool_name; |
| 4690 | spec->image_name = image_name; |
| 4691 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4692 | |
| 4693 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4694 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4695 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4696 | kfree(image_name); |
| 4697 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4698 | return ret; |
| 4699 | } |
| 4700 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4701 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4702 | { |
| 4703 | size_t size; |
| 4704 | int ret; |
| 4705 | void *reply_buf; |
| 4706 | void *p; |
| 4707 | void *end; |
| 4708 | u64 seq; |
| 4709 | u32 snap_count; |
| 4710 | struct ceph_snap_context *snapc; |
| 4711 | u32 i; |
| 4712 | |
| 4713 | /* |
| 4714 | * We'll need room for the seq value (maximum snapshot id), |
| 4715 | * snapshot count, and array of that many snapshot ids. |
| 4716 | * For now we have a fixed upper limit on the number we're |
| 4717 | * prepared to receive. |
| 4718 | */ |
| 4719 | size = sizeof (__le64) + sizeof (__le32) + |
| 4720 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 4721 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 4722 | if (!reply_buf) |
| 4723 | return -ENOMEM; |
| 4724 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4725 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4726 | &rbd_dev->header_oloc, "get_snapcontext", |
| 4727 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4728 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4729 | if (ret < 0) |
| 4730 | goto out; |
| 4731 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4732 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4733 | end = reply_buf + ret; |
| 4734 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4735 | ceph_decode_64_safe(&p, end, seq, out); |
| 4736 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 4737 | |
| 4738 | /* |
| 4739 | * Make sure the reported number of snapshot ids wouldn't go |
| 4740 | * beyond the end of our buffer. But before checking that, |
| 4741 | * make sure the computed size of the snapshot context we |
| 4742 | * allocate is representable in a size_t. |
| 4743 | */ |
| 4744 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 4745 | / sizeof (u64)) { |
| 4746 | ret = -EINVAL; |
| 4747 | goto out; |
| 4748 | } |
| 4749 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 4750 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4751 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4752 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 4753 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4754 | if (!snapc) { |
| 4755 | ret = -ENOMEM; |
| 4756 | goto out; |
| 4757 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4758 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4759 | for (i = 0; i < snap_count; i++) |
| 4760 | snapc->snaps[i] = ceph_decode_64(&p); |
| 4761 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 4762 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4763 | rbd_dev->header.snapc = snapc; |
| 4764 | |
| 4765 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4766 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4767 | out: |
| 4768 | kfree(reply_buf); |
| 4769 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4770 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4771 | } |
| 4772 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4773 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 4774 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4775 | { |
| 4776 | size_t size; |
| 4777 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4778 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4779 | int ret; |
| 4780 | void *p; |
| 4781 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4782 | char *snap_name; |
| 4783 | |
| 4784 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 4785 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4786 | if (!reply_buf) |
| 4787 | return ERR_PTR(-ENOMEM); |
| 4788 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4789 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4790 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4791 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 4792 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4793 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4794 | if (ret < 0) { |
| 4795 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4796 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4797 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4798 | |
| 4799 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4800 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4801 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4802 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4803 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4804 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4805 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4806 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4807 | out: |
| 4808 | kfree(reply_buf); |
| 4809 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4810 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4811 | } |
| 4812 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4813 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4814 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4815 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4816 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4817 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 4818 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 4819 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4820 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 4821 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4822 | if (first_time) { |
| 4823 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 4824 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4825 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4826 | } |
| 4827 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4828 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 4829 | if (ret && first_time) { |
| 4830 | kfree(rbd_dev->header.object_prefix); |
| 4831 | rbd_dev->header.object_prefix = NULL; |
| 4832 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4833 | |
| 4834 | return ret; |
| 4835 | } |
| 4836 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4837 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 4838 | { |
| 4839 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 4840 | |
| 4841 | if (rbd_dev->image_format == 1) |
| 4842 | return rbd_dev_v1_header_info(rbd_dev); |
| 4843 | |
| 4844 | return rbd_dev_v2_header_info(rbd_dev); |
| 4845 | } |
| 4846 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4847 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4848 | * Skips over white space at *buf, and updates *buf to point to the |
| 4849 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4850 | * the token (string of non-white space characters) found. Note |
| 4851 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4852 | */ |
| 4853 | static inline size_t next_token(const char **buf) |
| 4854 | { |
| 4855 | /* |
| 4856 | * These are the characters that produce nonzero for |
| 4857 | * isspace() in the "C" and "POSIX" locales. |
| 4858 | */ |
| 4859 | const char *spaces = " \f\n\r\t\v"; |
| 4860 | |
| 4861 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 4862 | |
| 4863 | return strcspn(*buf, spaces); /* Return token length */ |
| 4864 | } |
| 4865 | |
| 4866 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 4867 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 4868 | * enough to hold a copy of it, and copies the token into the new |
| 4869 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 4870 | * that a duplicate buffer is created even for a zero-length token. |
| 4871 | * |
| 4872 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 4873 | * pointer if memory for the duplicate was not available. If |
| 4874 | * the lenp argument is a non-null pointer, the length of the token |
| 4875 | * (not including the '\0') is returned in *lenp. |
| 4876 | * |
| 4877 | * If successful, the *buf pointer will be updated to point beyond |
| 4878 | * the end of the found token. |
| 4879 | * |
| 4880 | * Note: uses GFP_KERNEL for allocation. |
| 4881 | */ |
| 4882 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 4883 | { |
| 4884 | char *dup; |
| 4885 | size_t len; |
| 4886 | |
| 4887 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 4888 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 4889 | if (!dup) |
| 4890 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 4891 | *(dup + len) = '\0'; |
| 4892 | *buf += len; |
| 4893 | |
| 4894 | if (lenp) |
| 4895 | *lenp = len; |
| 4896 | |
| 4897 | return dup; |
| 4898 | } |
| 4899 | |
| 4900 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4901 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 4902 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 4903 | * and the data written is passed here via a NUL-terminated buffer. |
| 4904 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4905 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4906 | * The information extracted from these options is recorded in |
| 4907 | * the other parameters which return dynamically-allocated |
| 4908 | * structures: |
| 4909 | * ceph_opts |
| 4910 | * The address of a pointer that will refer to a ceph options |
| 4911 | * structure. Caller must release the returned pointer using |
| 4912 | * ceph_destroy_options() when it is no longer needed. |
| 4913 | * rbd_opts |
| 4914 | * Address of an rbd options pointer. Fully initialized by |
| 4915 | * this function; caller must release with kfree(). |
| 4916 | * spec |
| 4917 | * Address of an rbd image specification pointer. Fully |
| 4918 | * initialized by this function based on parsed options. |
| 4919 | * Caller must release with rbd_spec_put(). |
| 4920 | * |
| 4921 | * The options passed take this form: |
| 4922 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 4923 | * where: |
| 4924 | * <mon_addrs> |
| 4925 | * A comma-separated list of one or more monitor addresses. |
| 4926 | * A monitor address is an ip address, optionally followed |
| 4927 | * by a port number (separated by a colon). |
| 4928 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 4929 | * <options> |
| 4930 | * A comma-separated list of ceph and/or rbd options. |
| 4931 | * <pool_name> |
| 4932 | * The name of the rados pool containing the rbd image. |
| 4933 | * <image_name> |
| 4934 | * The name of the image in that pool to map. |
| 4935 | * <snap_id> |
| 4936 | * An optional snapshot id. If provided, the mapping will |
| 4937 | * present data from the image at the time that snapshot was |
| 4938 | * created. The image head is used if no snapshot id is |
| 4939 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4940 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4941 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4942 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4943 | struct rbd_options **opts, |
| 4944 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4945 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4946 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4947 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4948 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4949 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4950 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4951 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4952 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4953 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4954 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4955 | |
| 4956 | /* The first four tokens are required */ |
| 4957 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4958 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4959 | if (!len) { |
| 4960 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 4961 | return -EINVAL; |
| 4962 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4963 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4964 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4965 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4966 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4967 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4968 | options = dup_token(&buf, NULL); |
| 4969 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4970 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4971 | if (!*options) { |
| 4972 | rbd_warn(NULL, "no options provided"); |
| 4973 | goto out_err; |
| 4974 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4975 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4976 | spec = rbd_spec_alloc(); |
| 4977 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4978 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4979 | |
| 4980 | spec->pool_name = dup_token(&buf, NULL); |
| 4981 | if (!spec->pool_name) |
| 4982 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4983 | if (!*spec->pool_name) { |
| 4984 | rbd_warn(NULL, "no pool name provided"); |
| 4985 | goto out_err; |
| 4986 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4987 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4988 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4989 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4990 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4991 | if (!*spec->image_name) { |
| 4992 | rbd_warn(NULL, "no image name provided"); |
| 4993 | goto out_err; |
| 4994 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 4995 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4996 | /* |
| 4997 | * Snapshot name is optional; default is to use "-" |
| 4998 | * (indicating the head/no snapshot). |
| 4999 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5000 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5001 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5002 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5003 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5004 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5005 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5006 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5007 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5008 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5009 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5010 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5011 | *(snap_name + len) = '\0'; |
| 5012 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5013 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5014 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5015 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5016 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5017 | if (!rbd_opts) |
| 5018 | goto out_mem; |
| 5019 | |
| 5020 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5021 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5022 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5023 | rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5024 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5025 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5026 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5027 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5028 | if (IS_ERR(copts)) { |
| 5029 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5030 | goto out_err; |
| 5031 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5032 | kfree(options); |
| 5033 | |
| 5034 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5035 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5036 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5037 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5038 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5039 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5040 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5041 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5042 | kfree(rbd_opts); |
| 5043 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5044 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5045 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5046 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5047 | } |
| 5048 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5049 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5050 | * Return pool id (>= 0) or a negative error code. |
| 5051 | */ |
| 5052 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5053 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5054 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5055 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5056 | int tries = 0; |
| 5057 | int ret; |
| 5058 | |
| 5059 | again: |
| 5060 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5061 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5062 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5063 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5064 | if (ret < 0) |
| 5065 | return ret; |
| 5066 | |
| 5067 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5068 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5069 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5070 | newest_epoch, |
| 5071 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5072 | goto again; |
| 5073 | } else { |
| 5074 | /* the osdmap we have is new enough */ |
| 5075 | return -ENOENT; |
| 5076 | } |
| 5077 | } |
| 5078 | |
| 5079 | return ret; |
| 5080 | } |
| 5081 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5082 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5083 | { |
| 5084 | down_write(&rbd_dev->lock_rwsem); |
| 5085 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5086 | rbd_unlock(rbd_dev); |
| 5087 | up_write(&rbd_dev->lock_rwsem); |
| 5088 | } |
| 5089 | |
| 5090 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5091 | { |
| 5092 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5093 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5094 | return -EINVAL; |
| 5095 | } |
| 5096 | |
| 5097 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5098 | down_read(&rbd_dev->lock_rwsem); |
| 5099 | rbd_wait_state_locked(rbd_dev); |
| 5100 | up_read(&rbd_dev->lock_rwsem); |
| 5101 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 5102 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5103 | return -EROFS; |
| 5104 | } |
| 5105 | |
| 5106 | return 0; |
| 5107 | } |
| 5108 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5109 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5110 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5111 | * name given to it by the user. Internally, that identifier is |
| 5112 | * what's used to specify the names of objects related to the image. |
| 5113 | * |
| 5114 | * A special "rbd id" object is used to map an rbd image name to its |
| 5115 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5116 | * with the supplied name. |
| 5117 | * |
| 5118 | * This function will record the given rbd_dev's image_id field if |
| 5119 | * it can be determined, and in that case will return 0. If any |
| 5120 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5121 | * image_id field will be unchanged (and should be NULL). |
| 5122 | */ |
| 5123 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5124 | { |
| 5125 | int ret; |
| 5126 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5127 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5128 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5129 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5130 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5131 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5132 | * When probing a parent image, the image id is already |
| 5133 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5134 | * need to fetch the image id again in this case. We |
| 5135 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5136 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5137 | if (rbd_dev->spec->image_id) { |
| 5138 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5139 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5140 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5141 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5142 | |
| 5143 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5144 | * First, see if the format 2 image id file exists, and if |
| 5145 | * so, get the image's persistent id from it. |
| 5146 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5147 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5148 | rbd_dev->spec->image_name); |
| 5149 | if (ret) |
| 5150 | return ret; |
| 5151 | |
| 5152 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5153 | |
| 5154 | /* Response will be an encoded string, which includes a length */ |
| 5155 | |
| 5156 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5157 | response = kzalloc(size, GFP_NOIO); |
| 5158 | if (!response) { |
| 5159 | ret = -ENOMEM; |
| 5160 | goto out; |
| 5161 | } |
| 5162 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5163 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5164 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5165 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5166 | "get_id", NULL, 0, |
| 5167 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5168 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5169 | if (ret == -ENOENT) { |
| 5170 | image_id = kstrdup("", GFP_KERNEL); |
| 5171 | ret = image_id ? 0 : -ENOMEM; |
| 5172 | if (!ret) |
| 5173 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5174 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5175 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5176 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5177 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5178 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5179 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5180 | if (!ret) |
| 5181 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5182 | } |
| 5183 | |
| 5184 | if (!ret) { |
| 5185 | rbd_dev->spec->image_id = image_id; |
| 5186 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5187 | } |
| 5188 | out: |
| 5189 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5190 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5191 | return ret; |
| 5192 | } |
| 5193 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5194 | /* |
| 5195 | * Undo whatever state changes are made by v1 or v2 header info |
| 5196 | * call. |
| 5197 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5198 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5199 | { |
| 5200 | struct rbd_image_header *header; |
| 5201 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5202 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5203 | |
| 5204 | /* Free dynamic fields from the header, then zero it out */ |
| 5205 | |
| 5206 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5207 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5208 | kfree(header->snap_sizes); |
| 5209 | kfree(header->snap_names); |
| 5210 | kfree(header->object_prefix); |
| 5211 | memset(header, 0, sizeof (*header)); |
| 5212 | } |
| 5213 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5214 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5215 | { |
| 5216 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5217 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5218 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5219 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5220 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5221 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5222 | /* |
| 5223 | * Get the and check features for the image. Currently the |
| 5224 | * features are assumed to never change. |
| 5225 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5226 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5227 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5228 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5229 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5230 | /* If the image supports fancy striping, get its parameters */ |
| 5231 | |
| 5232 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5233 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5234 | if (ret < 0) |
| 5235 | goto out_err; |
| 5236 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5237 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5238 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5239 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5240 | if (ret) |
| 5241 | goto out_err; |
| 5242 | } |
| 5243 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5244 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5245 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5246 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5247 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5248 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5249 | kfree(rbd_dev->header.object_prefix); |
| 5250 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5251 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5252 | } |
| 5253 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5254 | /* |
| 5255 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5256 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5257 | * length of the already discovered part of the parent chain. |
| 5258 | */ |
| 5259 | 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] | 5260 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5261 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5262 | int ret; |
| 5263 | |
| 5264 | if (!rbd_dev->parent_spec) |
| 5265 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5266 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5267 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5268 | pr_info("parent chain is too long (%d)\n", depth); |
| 5269 | ret = -EINVAL; |
| 5270 | goto out_err; |
| 5271 | } |
| 5272 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5273 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5274 | if (!parent) { |
| 5275 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5276 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5277 | } |
| 5278 | |
| 5279 | /* |
| 5280 | * Images related by parent/child relationships always share |
| 5281 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5282 | */ |
| 5283 | __rbd_get_client(rbd_dev->rbd_client); |
| 5284 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5285 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5286 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5287 | if (ret < 0) |
| 5288 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5289 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5290 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5291 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5292 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5293 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5294 | out_err: |
| 5295 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5296 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5297 | return ret; |
| 5298 | } |
| 5299 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5300 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5301 | { |
| 5302 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5303 | rbd_dev_mapping_clear(rbd_dev); |
| 5304 | rbd_free_disk(rbd_dev); |
| 5305 | if (!single_major) |
| 5306 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5307 | } |
| 5308 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5309 | /* |
| 5310 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5311 | * upon return. |
| 5312 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5313 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5314 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5315 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5316 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5317 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5318 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5319 | if (!single_major) { |
| 5320 | ret = register_blkdev(0, rbd_dev->name); |
| 5321 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5322 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5323 | |
| 5324 | rbd_dev->major = ret; |
| 5325 | rbd_dev->minor = 0; |
| 5326 | } else { |
| 5327 | rbd_dev->major = rbd_major; |
| 5328 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5329 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5330 | |
| 5331 | /* Set up the blkdev mapping. */ |
| 5332 | |
| 5333 | ret = rbd_init_disk(rbd_dev); |
| 5334 | if (ret) |
| 5335 | goto err_out_blkdev; |
| 5336 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5337 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5338 | if (ret) |
| 5339 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5340 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5341 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5342 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5343 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5344 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5345 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5346 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5347 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5348 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5349 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5350 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5351 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5352 | err_out_mapping: |
| 5353 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5354 | err_out_disk: |
| 5355 | rbd_free_disk(rbd_dev); |
| 5356 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5357 | if (!single_major) |
| 5358 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5359 | err_out_unlock: |
| 5360 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5361 | return ret; |
| 5362 | } |
| 5363 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5364 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5365 | { |
| 5366 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5367 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5368 | |
| 5369 | /* Record the header object name for this rbd image. */ |
| 5370 | |
| 5371 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5372 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5373 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5374 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5375 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5376 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5377 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5378 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5379 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5380 | } |
| 5381 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5382 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5383 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5384 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5385 | if (rbd_dev->opts) |
| 5386 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5387 | rbd_dev->image_format = 0; |
| 5388 | kfree(rbd_dev->spec->image_id); |
| 5389 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5390 | } |
| 5391 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5392 | /* |
| 5393 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5394 | * device. If this image is the one being mapped (i.e., not a |
| 5395 | * parent), initiate a watch on its header object before using that |
| 5396 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5397 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5398 | 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] | 5399 | { |
| 5400 | int ret; |
| 5401 | |
| 5402 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5403 | * Get the id from the image id object. Unless there's an |
| 5404 | * error, rbd_dev->spec->image_id will be filled in with |
| 5405 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5406 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5407 | */ |
| 5408 | ret = rbd_dev_image_id(rbd_dev); |
| 5409 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5410 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5411 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5412 | ret = rbd_dev_header_name(rbd_dev); |
| 5413 | if (ret) |
| 5414 | goto err_out_format; |
| 5415 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5416 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5417 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5418 | if (ret) { |
| 5419 | if (ret == -ENOENT) |
| 5420 | pr_info("image %s/%s does not exist\n", |
| 5421 | rbd_dev->spec->pool_name, |
| 5422 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5423 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5424 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5425 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5426 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5427 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5428 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5429 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5430 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5431 | /* |
| 5432 | * If this image is the one being mapped, we have pool name and |
| 5433 | * id, image name and id, and snap name - need to fill snap id. |
| 5434 | * Otherwise this is a parent image, identified by pool, image |
| 5435 | * and snap ids - need to fill in names for those ids. |
| 5436 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5437 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5438 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5439 | else |
| 5440 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5441 | if (ret) { |
| 5442 | if (ret == -ENOENT) |
| 5443 | pr_info("snap %s/%s@%s does not exist\n", |
| 5444 | rbd_dev->spec->pool_name, |
| 5445 | rbd_dev->spec->image_name, |
| 5446 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5447 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5448 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5449 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5450 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5451 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5452 | if (ret) |
| 5453 | goto err_out_probe; |
| 5454 | |
| 5455 | /* |
| 5456 | * Need to warn users if this image is the one being |
| 5457 | * mapped and has a parent. |
| 5458 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5459 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5460 | rbd_warn(rbd_dev, |
| 5461 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 5462 | } |
| 5463 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5464 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5465 | if (ret) |
| 5466 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5467 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5468 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5469 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5470 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5471 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5472 | err_out_probe: |
| 5473 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5474 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5475 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5476 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5477 | err_out_format: |
| 5478 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5479 | kfree(rbd_dev->spec->image_id); |
| 5480 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5481 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5482 | } |
| 5483 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5484 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 5485 | const char *buf, |
| 5486 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5487 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5488 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5489 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5490 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5491 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5492 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5493 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5494 | |
| 5495 | if (!try_module_get(THIS_MODULE)) |
| 5496 | return -ENODEV; |
| 5497 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5498 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5499 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5500 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5501 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5502 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5503 | rbdc = rbd_get_client(ceph_opts); |
| 5504 | if (IS_ERR(rbdc)) { |
| 5505 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5506 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5507 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5508 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5509 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5510 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5511 | if (rc < 0) { |
| 5512 | if (rc == -ENOENT) |
| 5513 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5514 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5515 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5516 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5517 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5518 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5519 | if (!rbd_dev) { |
| 5520 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5521 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5522 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5523 | rbdc = NULL; /* rbd_dev now owns this */ |
| 5524 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5525 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5526 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5527 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 5528 | if (!rbd_dev->config_info) { |
| 5529 | rc = -ENOMEM; |
| 5530 | goto err_out_rbd_dev; |
| 5531 | } |
| 5532 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5533 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5534 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5535 | if (rc < 0) { |
| 5536 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5537 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5538 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 5539 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5540 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5541 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5542 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5543 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5544 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5545 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5546 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5547 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5548 | if (rbd_dev->opts->exclusive) { |
| 5549 | rc = rbd_add_acquire_lock(rbd_dev); |
| 5550 | if (rc) |
| 5551 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5552 | } |
| 5553 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5554 | /* Everything's ready. Announce the disk to the world. */ |
| 5555 | |
| 5556 | rc = device_add(&rbd_dev->dev); |
| 5557 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5558 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5559 | |
| 5560 | add_disk(rbd_dev->disk); |
| 5561 | /* see rbd_init_disk() */ |
| 5562 | blk_put_queue(rbd_dev->disk->queue); |
| 5563 | |
| 5564 | spin_lock(&rbd_dev_list_lock); |
| 5565 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5566 | spin_unlock(&rbd_dev_list_lock); |
| 5567 | |
| 5568 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5569 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5570 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5571 | rc = count; |
| 5572 | out: |
| 5573 | module_put(THIS_MODULE); |
| 5574 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5575 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5576 | err_out_image_lock: |
| 5577 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5578 | err_out_device_setup: |
| 5579 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5580 | err_out_image_probe: |
| 5581 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5582 | err_out_rbd_dev: |
| 5583 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5584 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5585 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5586 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5587 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5588 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5589 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5590 | } |
| 5591 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5592 | static ssize_t rbd_add(struct bus_type *bus, |
| 5593 | const char *buf, |
| 5594 | size_t count) |
| 5595 | { |
| 5596 | if (single_major) |
| 5597 | return -EINVAL; |
| 5598 | |
| 5599 | return do_rbd_add(bus, buf, count); |
| 5600 | } |
| 5601 | |
| 5602 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 5603 | const char *buf, |
| 5604 | size_t count) |
| 5605 | { |
| 5606 | return do_rbd_add(bus, buf, count); |
| 5607 | } |
| 5608 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5609 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 5610 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5611 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5612 | struct rbd_device *first = rbd_dev; |
| 5613 | struct rbd_device *second = first->parent; |
| 5614 | struct rbd_device *third; |
| 5615 | |
| 5616 | /* |
| 5617 | * Follow to the parent with no grandparent and |
| 5618 | * remove it. |
| 5619 | */ |
| 5620 | while (second && (third = second->parent)) { |
| 5621 | first = second; |
| 5622 | second = third; |
| 5623 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5624 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5625 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5626 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5627 | first->parent = NULL; |
| 5628 | first->parent_overlap = 0; |
| 5629 | |
| 5630 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5631 | rbd_spec_put(first->parent_spec); |
| 5632 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5633 | } |
| 5634 | } |
| 5635 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5636 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 5637 | const char *buf, |
| 5638 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5639 | { |
| 5640 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5641 | struct list_head *tmp; |
| 5642 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5643 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5644 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5645 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5646 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5647 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5648 | dev_id = -1; |
| 5649 | opt_buf[0] = '\0'; |
| 5650 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 5651 | if (dev_id < 0) { |
| 5652 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5653 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5654 | } |
| 5655 | if (opt_buf[0] != '\0') { |
| 5656 | if (!strcmp(opt_buf, "force")) { |
| 5657 | force = true; |
| 5658 | } else { |
| 5659 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 5660 | return -EINVAL; |
| 5661 | } |
| 5662 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5663 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5664 | ret = -ENOENT; |
| 5665 | spin_lock(&rbd_dev_list_lock); |
| 5666 | list_for_each(tmp, &rbd_dev_list) { |
| 5667 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 5668 | if (rbd_dev->dev_id == dev_id) { |
| 5669 | ret = 0; |
| 5670 | break; |
| 5671 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5672 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5673 | if (!ret) { |
| 5674 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5675 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5676 | ret = -EBUSY; |
| 5677 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5678 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 5679 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5680 | spin_unlock_irq(&rbd_dev->lock); |
| 5681 | } |
| 5682 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5683 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5684 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5685 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5686 | if (force) { |
| 5687 | /* |
| 5688 | * Prevent new IO from being queued and wait for existing |
| 5689 | * IO to complete/fail. |
| 5690 | */ |
| 5691 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 5692 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 5693 | } |
| 5694 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5695 | del_gendisk(rbd_dev->disk); |
| 5696 | spin_lock(&rbd_dev_list_lock); |
| 5697 | list_del_init(&rbd_dev->node); |
| 5698 | spin_unlock(&rbd_dev_list_lock); |
| 5699 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 5700 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5701 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5702 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5703 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5704 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5705 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5706 | } |
| 5707 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5708 | static ssize_t rbd_remove(struct bus_type *bus, |
| 5709 | const char *buf, |
| 5710 | size_t count) |
| 5711 | { |
| 5712 | if (single_major) |
| 5713 | return -EINVAL; |
| 5714 | |
| 5715 | return do_rbd_remove(bus, buf, count); |
| 5716 | } |
| 5717 | |
| 5718 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 5719 | const char *buf, |
| 5720 | size_t count) |
| 5721 | { |
| 5722 | return do_rbd_remove(bus, buf, count); |
| 5723 | } |
| 5724 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5725 | /* |
| 5726 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5727 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5728 | */ |
| 5729 | static int rbd_sysfs_init(void) |
| 5730 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5731 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5732 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5733 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 5734 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5735 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5736 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5737 | ret = bus_register(&rbd_bus_type); |
| 5738 | if (ret < 0) |
| 5739 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5740 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5741 | return ret; |
| 5742 | } |
| 5743 | |
| 5744 | static void rbd_sysfs_cleanup(void) |
| 5745 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5746 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5747 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5748 | } |
| 5749 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5750 | static int rbd_slab_init(void) |
| 5751 | { |
| 5752 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5753 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5754 | if (!rbd_img_request_cache) |
| 5755 | return -ENOMEM; |
| 5756 | |
| 5757 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5758 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 5759 | if (!rbd_obj_request_cache) |
| 5760 | goto out_err; |
| 5761 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5762 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5763 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5764 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5765 | kmem_cache_destroy(rbd_img_request_cache); |
| 5766 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5767 | return -ENOMEM; |
| 5768 | } |
| 5769 | |
| 5770 | static void rbd_slab_exit(void) |
| 5771 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5772 | rbd_assert(rbd_obj_request_cache); |
| 5773 | kmem_cache_destroy(rbd_obj_request_cache); |
| 5774 | rbd_obj_request_cache = NULL; |
| 5775 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5776 | rbd_assert(rbd_img_request_cache); |
| 5777 | kmem_cache_destroy(rbd_img_request_cache); |
| 5778 | rbd_img_request_cache = NULL; |
| 5779 | } |
| 5780 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 5781 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5782 | { |
| 5783 | int rc; |
| 5784 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 5785 | if (!libceph_compatible(NULL)) { |
| 5786 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 5787 | return -EINVAL; |
| 5788 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5789 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5790 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5791 | if (rc) |
| 5792 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5793 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5794 | /* |
| 5795 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 5796 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5797 | */ |
| 5798 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 5799 | if (!rbd_wq) { |
| 5800 | rc = -ENOMEM; |
| 5801 | goto err_out_slab; |
| 5802 | } |
| 5803 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5804 | if (single_major) { |
| 5805 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 5806 | if (rbd_major < 0) { |
| 5807 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5808 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5809 | } |
| 5810 | } |
| 5811 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5812 | rc = rbd_sysfs_init(); |
| 5813 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5814 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5815 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5816 | if (single_major) |
| 5817 | pr_info("loaded (major %d)\n", rbd_major); |
| 5818 | else |
| 5819 | pr_info("loaded\n"); |
| 5820 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5821 | return 0; |
| 5822 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5823 | err_out_blkdev: |
| 5824 | if (single_major) |
| 5825 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5826 | err_out_wq: |
| 5827 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5828 | err_out_slab: |
| 5829 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5830 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5831 | } |
| 5832 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 5833 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5834 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 5835 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5836 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5837 | if (single_major) |
| 5838 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5839 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5840 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | module_init(rbd_init); |
| 5844 | module_exit(rbd_exit); |
| 5845 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 5846 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5847 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 5848 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5849 | /* following authorship retained from original osdblk.c */ |
| 5850 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 5851 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 5852 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5853 | MODULE_LICENSE("GPL"); |