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> |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 35 | #include <linux/ceph/striper.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 36 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 37 | #include <linux/parser.h> |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 38 | #include <linux/bsearch.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 39 | |
| 40 | #include <linux/kernel.h> |
| 41 | #include <linux/device.h> |
| 42 | #include <linux/module.h> |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 43 | #include <linux/blk-mq.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 44 | #include <linux/fs.h> |
| 45 | #include <linux/blkdev.h> |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 46 | #include <linux/slab.h> |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 47 | #include <linux/idr.h> |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 48 | #include <linux/workqueue.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 49 | |
| 50 | #include "rbd_types.h" |
| 51 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 52 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
| 53 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 54 | /* |
| 55 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 56 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 57 | * universally 512 bytes. These symbols are just slightly more |
| 58 | * meaningful than the bare numbers they represent. |
| 59 | */ |
| 60 | #define SECTOR_SHIFT 9 |
| 61 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 62 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 63 | /* |
| 64 | * Increment the given counter and return its updated value. |
| 65 | * If the counter is already 0 it will not be incremented. |
| 66 | * If the counter is already at its maximum value returns |
| 67 | * -EINVAL without updating it. |
| 68 | */ |
| 69 | static int atomic_inc_return_safe(atomic_t *v) |
| 70 | { |
| 71 | unsigned int counter; |
| 72 | |
| 73 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); |
| 74 | if (counter <= (unsigned int)INT_MAX) |
| 75 | return (int)counter; |
| 76 | |
| 77 | atomic_dec(v); |
| 78 | |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 83 | static int atomic_dec_return_safe(atomic_t *v) |
| 84 | { |
| 85 | int counter; |
| 86 | |
| 87 | counter = atomic_dec_return(v); |
| 88 | if (counter >= 0) |
| 89 | return counter; |
| 90 | |
| 91 | atomic_inc(v); |
| 92 | |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 96 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 97 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 98 | #define RBD_MINORS_PER_MAJOR 256 |
| 99 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 100 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 101 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 102 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 103 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 104 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 105 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 106 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 107 | #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] | 108 | |
| 109 | #define RBD_SNAP_HEAD_NAME "-" |
| 110 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 111 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 112 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 113 | /* This allows a single page to hold an image name sent by OSD */ |
| 114 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 115 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 116 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 117 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 118 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 119 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 120 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 121 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 122 | /* Feature bits */ |
| 123 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 124 | #define RBD_FEATURE_LAYERING (1ULL<<0) |
| 125 | #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) |
| 126 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) |
| 127 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 128 | #define RBD_FEATURE_OPERATIONS (1ULL<<8) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 129 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 130 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 131 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 132 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 133 | RBD_FEATURE_DATA_POOL | \ |
| 134 | RBD_FEATURE_OPERATIONS) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 135 | |
| 136 | /* Features supported by this (client software) implementation. */ |
| 137 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 138 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 139 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 140 | /* |
| 141 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 142 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 143 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 144 | #define DEV_NAME_LEN 32 |
| 145 | |
| 146 | /* |
| 147 | * block device image metadata (in-memory version) |
| 148 | */ |
| 149 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 150 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 151 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 152 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 153 | u64 stripe_unit; |
| 154 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 155 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 156 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 157 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 158 | /* The remaining fields need to be updated occasionally */ |
| 159 | u64 image_size; |
| 160 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 161 | char *snap_names; /* format 1 only */ |
| 162 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 165 | /* |
| 166 | * An rbd image specification. |
| 167 | * |
| 168 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 169 | * identify an image. Each rbd_dev structure includes a pointer to |
| 170 | * an rbd_spec structure that encapsulates this identity. |
| 171 | * |
| 172 | * Each of the id's in an rbd_spec has an associated name. For a |
| 173 | * user-mapped image, the names are supplied and the id's associated |
| 174 | * with them are looked up. For a layered image, a parent image is |
| 175 | * defined by the tuple, and the names are looked up. |
| 176 | * |
| 177 | * An rbd_dev structure contains a parent_spec pointer which is |
| 178 | * non-null if the image it represents is a child in a layered |
| 179 | * image. This pointer will refer to the rbd_spec structure used |
| 180 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 181 | * is shared between the parent and child). |
| 182 | * |
| 183 | * Since these structures are populated once, during the discovery |
| 184 | * phase of image construction, they are effectively immutable so |
| 185 | * we make no effort to synchronize access to them. |
| 186 | * |
| 187 | * Note that code herein does not assume the image name is known (it |
| 188 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 189 | */ |
| 190 | struct rbd_spec { |
| 191 | u64 pool_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 192 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 193 | |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 194 | const char *image_id; |
| 195 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 196 | |
| 197 | u64 snap_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 198 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 199 | |
| 200 | struct kref kref; |
| 201 | }; |
| 202 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 203 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 204 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 205 | */ |
| 206 | struct rbd_client { |
| 207 | struct ceph_client *client; |
| 208 | struct kref kref; |
| 209 | struct list_head node; |
| 210 | }; |
| 211 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 212 | struct rbd_img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 213 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 214 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 215 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 216 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 217 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 218 | OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */ |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 219 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 220 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 221 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 222 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 223 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 224 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 225 | }; |
| 226 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 227 | /* |
| 228 | * Writes go through the following state machine to deal with |
| 229 | * layering: |
| 230 | * |
| 231 | * need copyup |
| 232 | * RBD_OBJ_WRITE_GUARD ---------------> RBD_OBJ_WRITE_COPYUP |
| 233 | * | ^ | |
| 234 | * v \------------------------------/ |
| 235 | * done |
| 236 | * ^ |
| 237 | * | |
| 238 | * RBD_OBJ_WRITE_FLAT |
| 239 | * |
| 240 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
| 241 | * there is a parent or not. |
| 242 | */ |
| 243 | enum rbd_obj_write_state { |
| 244 | RBD_OBJ_WRITE_FLAT = 1, |
| 245 | RBD_OBJ_WRITE_GUARD, |
| 246 | RBD_OBJ_WRITE_COPYUP, |
| 247 | }; |
| 248 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 249 | struct rbd_obj_request { |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 250 | struct ceph_object_extent ex; |
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; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 257 | struct ceph_file_extent *img_extents; |
| 258 | u32 num_img_extents; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 259 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 260 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 261 | struct ceph_bio_iter bio_pos; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 262 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 263 | struct ceph_bvec_iter bvec_pos; |
| 264 | u32 bvec_count; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 265 | u32 bvec_idx; |
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 | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 288 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 289 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 290 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 291 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 292 | }; |
| 293 | union { |
| 294 | struct request *rq; /* block request */ |
| 295 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | }; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 297 | spinlock_t completion_lock; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 298 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 299 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 300 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 301 | struct list_head object_extents; /* obj_req.ex structs */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | u32 obj_request_count; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 303 | u32 pending_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 304 | |
| 305 | struct kref kref; |
| 306 | }; |
| 307 | |
| 308 | #define for_each_obj_request(ireq, oreq) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 309 | list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 310 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 311 | list_for_each_entry_safe(oreq, n, &(ireq)->object_extents, ex.oe_item) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 312 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 313 | enum rbd_watch_state { |
| 314 | RBD_WATCH_STATE_UNREGISTERED, |
| 315 | RBD_WATCH_STATE_REGISTERED, |
| 316 | RBD_WATCH_STATE_ERROR, |
| 317 | }; |
| 318 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 319 | enum rbd_lock_state { |
| 320 | RBD_LOCK_STATE_UNLOCKED, |
| 321 | RBD_LOCK_STATE_LOCKED, |
| 322 | RBD_LOCK_STATE_RELEASING, |
| 323 | }; |
| 324 | |
| 325 | /* WatchNotify::ClientId */ |
| 326 | struct rbd_client_id { |
| 327 | u64 gid; |
| 328 | u64 handle; |
| 329 | }; |
| 330 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 331 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 332 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 333 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 334 | }; |
| 335 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 336 | /* |
| 337 | * a single device |
| 338 | */ |
| 339 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 340 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 341 | |
| 342 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 343 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 344 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 345 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 346 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 347 | struct rbd_client *rbd_client; |
| 348 | |
| 349 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 350 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 351 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 352 | |
| 353 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 354 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 355 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 356 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 357 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 358 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 359 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 360 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 361 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 362 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 363 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 364 | struct mutex watch_mutex; |
| 365 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 366 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 367 | u64 watch_cookie; |
| 368 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 369 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 370 | struct rw_semaphore lock_rwsem; |
| 371 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 372 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 373 | struct rbd_client_id owner_cid; |
| 374 | struct work_struct acquired_lock_work; |
| 375 | struct work_struct released_lock_work; |
| 376 | struct delayed_work lock_dwork; |
| 377 | struct work_struct unlock_work; |
| 378 | wait_queue_head_t lock_waitq; |
| 379 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 380 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 381 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 382 | struct rbd_spec *parent_spec; |
| 383 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 384 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 385 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 386 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 387 | /* Block layer tags. */ |
| 388 | struct blk_mq_tag_set tag_set; |
| 389 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 390 | /* protects updating the header */ |
| 391 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 392 | |
| 393 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 394 | |
| 395 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 396 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 397 | /* sysfs related */ |
| 398 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 399 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 400 | }; |
| 401 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 402 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 403 | * Flag bits for rbd_dev->flags: |
| 404 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 405 | * by rbd_dev->lock |
| 406 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 407 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 408 | enum rbd_dev_flags { |
| 409 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 410 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 411 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 412 | }; |
| 413 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 414 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 415 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 416 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 417 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 418 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 419 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 420 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 421 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 422 | /* Slab caches for frequently-allocated structures */ |
| 423 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 424 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 425 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 426 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 427 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 428 | static DEFINE_IDA(rbd_dev_id_ida); |
| 429 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 430 | static struct workqueue_struct *rbd_wq; |
| 431 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 432 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 433 | * single-major requires >= 0.75 version of userspace rbd utility. |
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 | static bool single_major = true; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 436 | module_param(single_major, bool, S_IRUGO); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 437 | 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] | 438 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 439 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 440 | size_t count); |
| 441 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 442 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 443 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 444 | size_t count); |
| 445 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 446 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 447 | 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] | 448 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 449 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 450 | static int rbd_dev_id_to_minor(int dev_id) |
| 451 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 452 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static int minor_to_rbd_dev_id(int minor) |
| 456 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 457 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 460 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 461 | { |
| 462 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 463 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 464 | } |
| 465 | |
| 466 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 467 | { |
| 468 | bool is_lock_owner; |
| 469 | |
| 470 | down_read(&rbd_dev->lock_rwsem); |
| 471 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 472 | up_read(&rbd_dev->lock_rwsem); |
| 473 | return is_lock_owner; |
| 474 | } |
| 475 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 476 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 477 | { |
| 478 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 479 | } |
| 480 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 481 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 482 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 483 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 484 | 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] | 485 | 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] | 486 | |
| 487 | static struct attribute *rbd_bus_attrs[] = { |
| 488 | &bus_attr_add.attr, |
| 489 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 490 | &bus_attr_add_single_major.attr, |
| 491 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 492 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 493 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 494 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 495 | |
| 496 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 497 | struct attribute *attr, int index) |
| 498 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 499 | if (!single_major && |
| 500 | (attr == &bus_attr_add_single_major.attr || |
| 501 | attr == &bus_attr_remove_single_major.attr)) |
| 502 | return 0; |
| 503 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 504 | return attr->mode; |
| 505 | } |
| 506 | |
| 507 | static const struct attribute_group rbd_bus_group = { |
| 508 | .attrs = rbd_bus_attrs, |
| 509 | .is_visible = rbd_bus_is_visible, |
| 510 | }; |
| 511 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 512 | |
| 513 | static struct bus_type rbd_bus_type = { |
| 514 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 515 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 516 | }; |
| 517 | |
| 518 | static void rbd_root_dev_release(struct device *dev) |
| 519 | { |
| 520 | } |
| 521 | |
| 522 | static struct device rbd_root_dev = { |
| 523 | .init_name = "rbd", |
| 524 | .release = rbd_root_dev_release, |
| 525 | }; |
| 526 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 527 | static __printf(2, 3) |
| 528 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 529 | { |
| 530 | struct va_format vaf; |
| 531 | va_list args; |
| 532 | |
| 533 | va_start(args, fmt); |
| 534 | vaf.fmt = fmt; |
| 535 | vaf.va = &args; |
| 536 | |
| 537 | if (!rbd_dev) |
| 538 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 539 | else if (rbd_dev->disk) |
| 540 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 541 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 542 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 543 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 544 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 545 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 546 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 547 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 548 | else /* punt */ |
| 549 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 550 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 551 | va_end(args); |
| 552 | } |
| 553 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 554 | #ifdef RBD_DEBUG |
| 555 | #define rbd_assert(expr) \ |
| 556 | if (unlikely(!(expr))) { \ |
| 557 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 558 | "at line %d:\n\n" \ |
| 559 | "\trbd_assert(%s);\n\n", \ |
| 560 | __func__, __LINE__, #expr); \ |
| 561 | BUG(); \ |
| 562 | } |
| 563 | #else /* !RBD_DEBUG */ |
| 564 | # define rbd_assert(expr) ((void) 0) |
| 565 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 566 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 567 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 568 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 569 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 570 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 571 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 572 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 573 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 574 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 575 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 576 | u8 *order, u64 *snap_size); |
| 577 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 578 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 579 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 580 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 581 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 582 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 583 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 584 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 585 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 586 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 587 | removing = true; |
| 588 | else |
| 589 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 590 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 591 | if (removing) |
| 592 | return -ENOENT; |
| 593 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 594 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 595 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 596 | return 0; |
| 597 | } |
| 598 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 599 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 600 | { |
| 601 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 602 | unsigned long open_count_before; |
| 603 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 604 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 605 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 606 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 607 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 608 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 609 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 612 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 613 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 614 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 615 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 616 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 617 | return -EFAULT; |
| 618 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 619 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 620 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 621 | return -EROFS; |
| 622 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 623 | /* Let blkdev_roset() handle it */ |
| 624 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 628 | unsigned int cmd, unsigned long arg) |
| 629 | { |
| 630 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 631 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 632 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 633 | switch (cmd) { |
| 634 | case BLKROSET: |
| 635 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 636 | break; |
| 637 | default: |
| 638 | ret = -ENOTTY; |
| 639 | } |
| 640 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 641 | return ret; |
| 642 | } |
| 643 | |
| 644 | #ifdef CONFIG_COMPAT |
| 645 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 646 | unsigned int cmd, unsigned long arg) |
| 647 | { |
| 648 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 649 | } |
| 650 | #endif /* CONFIG_COMPAT */ |
| 651 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 652 | static const struct block_device_operations rbd_bd_ops = { |
| 653 | .owner = THIS_MODULE, |
| 654 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 655 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 656 | .ioctl = rbd_ioctl, |
| 657 | #ifdef CONFIG_COMPAT |
| 658 | .compat_ioctl = rbd_compat_ioctl, |
| 659 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 660 | }; |
| 661 | |
| 662 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 663 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 664 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 665 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 666 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 667 | { |
| 668 | struct rbd_client *rbdc; |
| 669 | int ret = -ENOMEM; |
| 670 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 671 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 672 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 673 | if (!rbdc) |
| 674 | goto out_opt; |
| 675 | |
| 676 | kref_init(&rbdc->kref); |
| 677 | INIT_LIST_HEAD(&rbdc->node); |
| 678 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 679 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 680 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 681 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 682 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 683 | |
| 684 | ret = ceph_open_session(rbdc->client); |
| 685 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 686 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 687 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 688 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 689 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 690 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 691 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 692 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 693 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 694 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 695 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 696 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 697 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 698 | kfree(rbdc); |
| 699 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 700 | if (ceph_opts) |
| 701 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 702 | dout("%s: error %d\n", __func__, ret); |
| 703 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 704 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 707 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 708 | { |
| 709 | kref_get(&rbdc->kref); |
| 710 | |
| 711 | return rbdc; |
| 712 | } |
| 713 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 715 | * Find a ceph client with specific addr and configuration. If |
| 716 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 717 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 718 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | { |
| 720 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 721 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 722 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 723 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 724 | return NULL; |
| 725 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 726 | spin_lock(&rbd_client_list_lock); |
| 727 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 728 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 729 | __rbd_get_client(client_node); |
| 730 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 731 | found = true; |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | spin_unlock(&rbd_client_list_lock); |
| 736 | |
| 737 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 741 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 742 | */ |
| 743 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 744 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 745 | Opt_last_int, |
| 746 | /* int args above */ |
| 747 | Opt_last_string, |
| 748 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 749 | Opt_read_only, |
| 750 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 751 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 752 | Opt_exclusive, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 753 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 754 | }; |
| 755 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 756 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 757 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 758 | /* int args above */ |
| 759 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 760 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 761 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 762 | {Opt_read_write, "read_write"}, |
| 763 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 764 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 765 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 766 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 769 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 770 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 771 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 772 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 773 | bool exclusive; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 774 | }; |
| 775 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 776 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 777 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 778 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 779 | #define RBD_EXCLUSIVE_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 780 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 781 | static int parse_rbd_opts_token(char *c, void *private) |
| 782 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 783 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 784 | substring_t argstr[MAX_OPT_ARGS]; |
| 785 | int token, intval, ret; |
| 786 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 787 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 788 | if (token < Opt_last_int) { |
| 789 | ret = match_int(&argstr[0], &intval); |
| 790 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 791 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 792 | return ret; |
| 793 | } |
| 794 | dout("got int token %d val %d\n", token, intval); |
| 795 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 796 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 797 | } else { |
| 798 | dout("got token %d\n", token); |
| 799 | } |
| 800 | |
| 801 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 802 | case Opt_queue_depth: |
| 803 | if (intval < 1) { |
| 804 | pr_err("queue_depth out of range\n"); |
| 805 | return -EINVAL; |
| 806 | } |
| 807 | rbd_opts->queue_depth = intval; |
| 808 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 809 | case Opt_read_only: |
| 810 | rbd_opts->read_only = true; |
| 811 | break; |
| 812 | case Opt_read_write: |
| 813 | rbd_opts->read_only = false; |
| 814 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 815 | case Opt_lock_on_read: |
| 816 | rbd_opts->lock_on_read = true; |
| 817 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 818 | case Opt_exclusive: |
| 819 | rbd_opts->exclusive = true; |
| 820 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 821 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 822 | /* libceph prints "bad option" msg */ |
| 823 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 824 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 825 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | return 0; |
| 827 | } |
| 828 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 829 | static char* obj_op_name(enum obj_operation_type op_type) |
| 830 | { |
| 831 | switch (op_type) { |
| 832 | case OBJ_OP_READ: |
| 833 | return "read"; |
| 834 | case OBJ_OP_WRITE: |
| 835 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 836 | case OBJ_OP_DISCARD: |
| 837 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 838 | default: |
| 839 | return "???"; |
| 840 | } |
| 841 | } |
| 842 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 843 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 844 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 845 | * not exist create it. Either way, ceph_opts is consumed by this |
| 846 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 847 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 848 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 849 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 850 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 851 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 852 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 853 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 854 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 855 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 856 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 857 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 858 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 859 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 860 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /* |
| 864 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 865 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 866 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 867 | */ |
| 868 | static void rbd_client_release(struct kref *kref) |
| 869 | { |
| 870 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 871 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 872 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 873 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 874 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 875 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 876 | |
| 877 | ceph_destroy_client(rbdc->client); |
| 878 | kfree(rbdc); |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 883 | * it. |
| 884 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 885 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 886 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 887 | if (rbdc) |
| 888 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 891 | static bool rbd_image_format_valid(u32 image_format) |
| 892 | { |
| 893 | return image_format == 1 || image_format == 2; |
| 894 | } |
| 895 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 896 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 897 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 898 | size_t size; |
| 899 | u32 snap_count; |
| 900 | |
| 901 | /* The header has to start with the magic rbd header text */ |
| 902 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 903 | return false; |
| 904 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 905 | /* The bio layer requires at least sector-sized I/O */ |
| 906 | |
| 907 | if (ondisk->options.order < SECTOR_SHIFT) |
| 908 | return false; |
| 909 | |
| 910 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 911 | |
| 912 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 913 | return false; |
| 914 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 915 | /* |
| 916 | * The size of a snapshot header has to fit in a size_t, and |
| 917 | * that limits the number of snapshots. |
| 918 | */ |
| 919 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 920 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 921 | if (snap_count > size / sizeof (__le64)) |
| 922 | return false; |
| 923 | |
| 924 | /* |
| 925 | * Not only that, but the size of the entire the snapshot |
| 926 | * header must also be representable in a size_t. |
| 927 | */ |
| 928 | size -= snap_count * sizeof (__le64); |
| 929 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 930 | return false; |
| 931 | |
| 932 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 933 | } |
| 934 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 935 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 936 | * returns the size of an object in the image |
| 937 | */ |
| 938 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 939 | { |
| 940 | return 1U << header->obj_order; |
| 941 | } |
| 942 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 943 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 944 | { |
| 945 | if (rbd_dev->header.stripe_unit == 0 || |
| 946 | rbd_dev->header.stripe_count == 0) { |
| 947 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 948 | rbd_dev->header.stripe_count = 1; |
| 949 | } |
| 950 | |
| 951 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 952 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 953 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 954 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 955 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 956 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 957 | } |
| 958 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 959 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 960 | * Fill an rbd image header with information from the given format 1 |
| 961 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 962 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 963 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 964 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 965 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 966 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 967 | bool first_time = header->object_prefix == NULL; |
| 968 | struct ceph_snap_context *snapc; |
| 969 | char *object_prefix = NULL; |
| 970 | char *snap_names = NULL; |
| 971 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 972 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 973 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 974 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 975 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 976 | /* Allocate this now to avoid having to handle failure below */ |
| 977 | |
| 978 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 979 | object_prefix = kstrndup(ondisk->object_prefix, |
| 980 | sizeof(ondisk->object_prefix), |
| 981 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 982 | if (!object_prefix) |
| 983 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 987 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 988 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 989 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 990 | if (!snapc) |
| 991 | goto out_err; |
| 992 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 993 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 994 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 995 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 996 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 997 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 998 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 999 | if (snap_names_len > (u64)SIZE_MAX) |
| 1000 | goto out_2big; |
| 1001 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1002 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1003 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1004 | |
| 1005 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1006 | snap_sizes = kmalloc_array(snap_count, |
| 1007 | sizeof(*header->snap_sizes), |
| 1008 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | if (!snap_sizes) |
| 1010 | goto out_err; |
| 1011 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1012 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1013 | * Copy the names, and fill in each snapshot's id |
| 1014 | * and size. |
| 1015 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1016 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1017 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1018 | * snap_names_len bytes beyond the end of the |
| 1019 | * snapshot id array, this memcpy() is safe. |
| 1020 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1021 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1022 | snaps = ondisk->snaps; |
| 1023 | for (i = 0; i < snap_count; i++) { |
| 1024 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1025 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1026 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1027 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1028 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1030 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1031 | if (first_time) { |
| 1032 | header->object_prefix = object_prefix; |
| 1033 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1034 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1035 | } else { |
| 1036 | ceph_put_snap_context(header->snapc); |
| 1037 | kfree(header->snap_names); |
| 1038 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1042 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1043 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1044 | header->snapc = snapc; |
| 1045 | header->snap_names = snap_names; |
| 1046 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1047 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1048 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1049 | out_2big: |
| 1050 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1051 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1052 | kfree(snap_sizes); |
| 1053 | kfree(snap_names); |
| 1054 | ceph_put_snap_context(snapc); |
| 1055 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1056 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1057 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1060 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1061 | { |
| 1062 | const char *snap_name; |
| 1063 | |
| 1064 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1065 | |
| 1066 | /* Skip over names until we find the one we are looking for */ |
| 1067 | |
| 1068 | snap_name = rbd_dev->header.snap_names; |
| 1069 | while (which--) |
| 1070 | snap_name += strlen(snap_name) + 1; |
| 1071 | |
| 1072 | return kstrdup(snap_name, GFP_KERNEL); |
| 1073 | } |
| 1074 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1075 | /* |
| 1076 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1077 | * Note that result is for snapshots in *descending* order. |
| 1078 | */ |
| 1079 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1080 | { |
| 1081 | u64 snap_id1 = *(u64 *)s1; |
| 1082 | u64 snap_id2 = *(u64 *)s2; |
| 1083 | |
| 1084 | if (snap_id1 < snap_id2) |
| 1085 | return 1; |
| 1086 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Search a snapshot context to see if the given snapshot id is |
| 1091 | * present. |
| 1092 | * |
| 1093 | * Returns the position of the snapshot id in the array if it's found, |
| 1094 | * or BAD_SNAP_INDEX otherwise. |
| 1095 | * |
| 1096 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1097 | * reverse order, highest snapshot id first. |
| 1098 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1099 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1100 | { |
| 1101 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1102 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1103 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1104 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1105 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1106 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1107 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1108 | } |
| 1109 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1110 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1111 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1112 | { |
| 1113 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1114 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1115 | |
| 1116 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1117 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1118 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1119 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1120 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1121 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1122 | } |
| 1123 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1124 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1125 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1126 | if (snap_id == CEPH_NOSNAP) |
| 1127 | return RBD_SNAP_HEAD_NAME; |
| 1128 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1129 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1130 | if (rbd_dev->image_format == 1) |
| 1131 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1132 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1133 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1136 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1137 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1138 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1139 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1140 | if (snap_id == CEPH_NOSNAP) { |
| 1141 | *snap_size = rbd_dev->header.image_size; |
| 1142 | } else if (rbd_dev->image_format == 1) { |
| 1143 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1144 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1145 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1146 | if (which == BAD_SNAP_INDEX) |
| 1147 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1148 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1149 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1150 | } else { |
| 1151 | u64 size = 0; |
| 1152 | int ret; |
| 1153 | |
| 1154 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1155 | if (ret) |
| 1156 | return ret; |
| 1157 | |
| 1158 | *snap_size = size; |
| 1159 | } |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1164 | u64 *snap_features) |
| 1165 | { |
| 1166 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1167 | if (snap_id == CEPH_NOSNAP) { |
| 1168 | *snap_features = rbd_dev->header.features; |
| 1169 | } else if (rbd_dev->image_format == 1) { |
| 1170 | *snap_features = 0; /* No features for format 1 */ |
| 1171 | } else { |
| 1172 | u64 features = 0; |
| 1173 | int ret; |
| 1174 | |
| 1175 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1176 | if (ret) |
| 1177 | return ret; |
| 1178 | |
| 1179 | *snap_features = features; |
| 1180 | } |
| 1181 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1184 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1185 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1186 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1187 | u64 size = 0; |
| 1188 | u64 features = 0; |
| 1189 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1190 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1191 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1192 | if (ret) |
| 1193 | return ret; |
| 1194 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1195 | if (ret) |
| 1196 | return ret; |
| 1197 | |
| 1198 | rbd_dev->mapping.size = size; |
| 1199 | rbd_dev->mapping.features = features; |
| 1200 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1201 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1204 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1205 | { |
| 1206 | rbd_dev->mapping.size = 0; |
| 1207 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1208 | } |
| 1209 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1210 | static void zero_bvec(struct bio_vec *bv) |
| 1211 | { |
| 1212 | void *buf; |
| 1213 | unsigned long flags; |
| 1214 | |
| 1215 | buf = bvec_kmap_irq(bv, &flags); |
| 1216 | memset(buf, 0, bv->bv_len); |
| 1217 | flush_dcache_page(bv->bv_page); |
| 1218 | bvec_kunmap_irq(buf, &flags); |
| 1219 | } |
| 1220 | |
| 1221 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
| 1222 | { |
| 1223 | struct ceph_bio_iter it = *bio_pos; |
| 1224 | |
| 1225 | ceph_bio_iter_advance(&it, off); |
| 1226 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1227 | zero_bvec(&bv); |
| 1228 | })); |
| 1229 | } |
| 1230 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1231 | 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] | 1232 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1233 | struct ceph_bvec_iter it = *bvec_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1234 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1235 | ceph_bvec_iter_advance(&it, off); |
| 1236 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1237 | zero_bvec(&bv); |
| 1238 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1242 | * Zero a range in @obj_req data buffer defined by a bio (list) or |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1243 | * (private) bio_vec array. |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1244 | * |
| 1245 | * @off is relative to the start of the data buffer. |
| 1246 | */ |
| 1247 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1248 | u32 bytes) |
| 1249 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1250 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1251 | case OBJ_REQUEST_BIO: |
| 1252 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1253 | break; |
| 1254 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1255 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1256 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1257 | break; |
| 1258 | default: |
| 1259 | rbd_assert(0); |
| 1260 | } |
| 1261 | } |
| 1262 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1263 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1264 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1265 | { |
| 1266 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1267 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1268 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1269 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1270 | } |
| 1271 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1272 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1273 | { |
| 1274 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1275 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1276 | kref_get(&img_request->kref); |
| 1277 | } |
| 1278 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1279 | static void rbd_img_request_destroy(struct kref *kref); |
| 1280 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1281 | { |
| 1282 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1283 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1284 | kref_read(&img_request->kref)); |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1285 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1289 | struct rbd_obj_request *obj_request) |
| 1290 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1291 | rbd_assert(obj_request->img_request == NULL); |
| 1292 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1293 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1294 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1295 | img_request->obj_request_count++; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1296 | img_request->pending_count++; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1297 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1301 | struct rbd_obj_request *obj_request) |
| 1302 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1303 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1304 | list_del(&obj_request->ex.oe_item); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1305 | rbd_assert(img_request->obj_request_count > 0); |
| 1306 | img_request->obj_request_count--; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1307 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1308 | rbd_obj_request_put(obj_request); |
| 1309 | } |
| 1310 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1311 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1312 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1313 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1314 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1315 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1316 | obj_request, obj_request->ex.oe_objno, obj_request->ex.oe_off, |
| 1317 | obj_request->ex.oe_len, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1318 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1319 | } |
| 1320 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1321 | /* |
| 1322 | * The default/initial value for all image request flags is 0. Each |
| 1323 | * is conditionally set to 1 at image request initialization time |
| 1324 | * and currently never change thereafter. |
| 1325 | */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1326 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1327 | { |
| 1328 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1329 | smp_mb(); |
| 1330 | } |
| 1331 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1332 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1333 | { |
| 1334 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1335 | smp_mb(); |
| 1336 | } |
| 1337 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1338 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1339 | { |
| 1340 | smp_mb(); |
| 1341 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1342 | } |
| 1343 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1344 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
| 1345 | { |
| 1346 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1347 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1348 | return !obj_req->ex.oe_off && |
| 1349 | obj_req->ex.oe_len == rbd_dev->layout.object_size; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
| 1353 | { |
| 1354 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1355 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1356 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1357 | rbd_dev->layout.object_size; |
| 1358 | } |
| 1359 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1360 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
| 1361 | { |
| 1362 | return ceph_file_extents_bytes(obj_req->img_extents, |
| 1363 | obj_req->num_img_extents); |
| 1364 | } |
| 1365 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1366 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1367 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1368 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1369 | case OBJ_OP_READ: |
| 1370 | return false; |
| 1371 | case OBJ_OP_WRITE: |
| 1372 | case OBJ_OP_DISCARD: |
| 1373 | return true; |
| 1374 | default: |
| 1375 | rbd_assert(0); |
| 1376 | } |
| 1377 | } |
| 1378 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1379 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
| 1380 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1381 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1382 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1383 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1384 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1385 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1386 | osd_req->r_result, obj_req); |
| 1387 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1388 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1389 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1390 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1391 | obj_req->xferred = osd_req->r_result; |
| 1392 | else |
| 1393 | /* |
| 1394 | * Writes aren't allowed to return a data payload. In some |
| 1395 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1396 | * a stat response makes it through, but we don't care. |
| 1397 | */ |
| 1398 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1399 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1400 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1401 | } |
| 1402 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1403 | 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] | 1404 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1405 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1406 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1407 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1408 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1412 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1413 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1414 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1415 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Deepa Dinamani | 1134e09 | 2017-05-08 15:59:19 -0700 | [diff] [blame] | 1416 | ktime_get_real_ts(&osd_req->r_mtime); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1417 | osd_req->r_data_offset = obj_request->ex.oe_off; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1418 | } |
| 1419 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1420 | static struct ceph_osd_request * |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1421 | 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] | 1422 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1423 | struct rbd_img_request *img_req = obj_req->img_request; |
| 1424 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1425 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1426 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1427 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1428 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1429 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1430 | req = ceph_osdc_alloc_request(osdc, |
| 1431 | (rbd_img_is_write(img_req) ? img_req->snapc : NULL), |
| 1432 | num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1433 | if (!req) |
| 1434 | return NULL; |
| 1435 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1436 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1437 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1438 | |
| 1439 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1440 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1441 | rbd_dev->header.object_prefix, obj_req->ex.oe_objno)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1442 | goto err_req; |
| 1443 | |
| 1444 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1445 | goto err_req; |
| 1446 | |
| 1447 | return req; |
| 1448 | |
| 1449 | err_req: |
| 1450 | ceph_osdc_put_request(req); |
| 1451 | return NULL; |
| 1452 | } |
| 1453 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1454 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1455 | { |
| 1456 | ceph_osdc_put_request(osd_req); |
| 1457 | } |
| 1458 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1459 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1460 | { |
| 1461 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1462 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1463 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1464 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1465 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1466 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1467 | ceph_object_extent_init(&obj_request->ex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1468 | kref_init(&obj_request->kref); |
| 1469 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1470 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1471 | return obj_request; |
| 1472 | } |
| 1473 | |
| 1474 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1475 | { |
| 1476 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1477 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1478 | |
| 1479 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1480 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1481 | dout("%s: obj %p\n", __func__, obj_request); |
| 1482 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1483 | if (obj_request->osd_req) |
| 1484 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1485 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1486 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1487 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1488 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1489 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1490 | break; /* Nothing to do */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1491 | case OBJ_REQUEST_OWN_BVECS: |
| 1492 | kfree(obj_request->bvec_pos.bvecs); |
| 1493 | break; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1494 | default: |
| 1495 | rbd_assert(0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1496 | } |
| 1497 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1498 | kfree(obj_request->img_extents); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1499 | if (obj_request->copyup_bvecs) { |
| 1500 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1501 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1502 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1503 | } |
| 1504 | kfree(obj_request->copyup_bvecs); |
| 1505 | } |
Ilya Dryomov | f9dcbc4 | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1506 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1507 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1508 | } |
| 1509 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1510 | /* It's OK to call this for a device with no parent */ |
| 1511 | |
| 1512 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1513 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1514 | { |
| 1515 | rbd_dev_remove_parent(rbd_dev); |
| 1516 | rbd_spec_put(rbd_dev->parent_spec); |
| 1517 | rbd_dev->parent_spec = NULL; |
| 1518 | rbd_dev->parent_overlap = 0; |
| 1519 | } |
| 1520 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1521 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1522 | * Parent image reference counting is used to determine when an |
| 1523 | * image's parent fields can be safely torn down--after there are no |
| 1524 | * more in-flight requests to the parent image. When the last |
| 1525 | * reference is dropped, cleaning them up is safe. |
| 1526 | */ |
| 1527 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1528 | { |
| 1529 | int counter; |
| 1530 | |
| 1531 | if (!rbd_dev->parent_spec) |
| 1532 | return; |
| 1533 | |
| 1534 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1535 | if (counter > 0) |
| 1536 | return; |
| 1537 | |
| 1538 | /* Last reference; clean up parent data structures */ |
| 1539 | |
| 1540 | if (!counter) |
| 1541 | rbd_dev_unparent(rbd_dev); |
| 1542 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1543 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * If an image has a non-zero parent overlap, get a reference to its |
| 1548 | * parent. |
| 1549 | * |
| 1550 | * Returns true if the rbd device has a parent with a non-zero |
| 1551 | * overlap and a reference for it was successfully taken, or |
| 1552 | * false otherwise. |
| 1553 | */ |
| 1554 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1555 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1556 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1557 | |
| 1558 | if (!rbd_dev->parent_spec) |
| 1559 | return false; |
| 1560 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1561 | down_read(&rbd_dev->header_rwsem); |
| 1562 | if (rbd_dev->parent_overlap) |
| 1563 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1564 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1565 | |
| 1566 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1567 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1568 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1569 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1570 | } |
| 1571 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1572 | /* |
| 1573 | * Caller is responsible for filling in the list of object requests |
| 1574 | * that comprises the image request, and the Linux request pointer |
| 1575 | * (if there is one). |
| 1576 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1577 | static struct rbd_img_request *rbd_img_request_create( |
| 1578 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1579 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1580 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1581 | { |
| 1582 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1583 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1584 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1585 | if (!img_request) |
| 1586 | return NULL; |
| 1587 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1588 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1589 | img_request->op_type = op_type; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1590 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1591 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1592 | else |
| 1593 | img_request->snapc = snapc; |
| 1594 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1595 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1596 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1597 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1598 | spin_lock_init(&img_request->completion_lock); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1599 | INIT_LIST_HEAD(&img_request->object_extents); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1600 | kref_init(&img_request->kref); |
| 1601 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1602 | dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, |
| 1603 | obj_op_name(op_type), img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1604 | return img_request; |
| 1605 | } |
| 1606 | |
| 1607 | static void rbd_img_request_destroy(struct kref *kref) |
| 1608 | { |
| 1609 | struct rbd_img_request *img_request; |
| 1610 | struct rbd_obj_request *obj_request; |
| 1611 | struct rbd_obj_request *next_obj_request; |
| 1612 | |
| 1613 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1614 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1615 | dout("%s: img %p\n", __func__, img_request); |
| 1616 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1617 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1618 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1619 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1620 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1621 | if (img_request_layered_test(img_request)) { |
| 1622 | img_request_layered_clear(img_request); |
| 1623 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1624 | } |
| 1625 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1626 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1627 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1628 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1629 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1630 | } |
| 1631 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1632 | static void prune_extents(struct ceph_file_extent *img_extents, |
| 1633 | u32 *num_img_extents, u64 overlap) |
| 1634 | { |
| 1635 | u32 cnt = *num_img_extents; |
| 1636 | |
| 1637 | /* drop extents completely beyond the overlap */ |
| 1638 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) |
| 1639 | cnt--; |
| 1640 | |
| 1641 | if (cnt) { |
| 1642 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; |
| 1643 | |
| 1644 | /* trim final overlapping extent */ |
| 1645 | if (ex->fe_off + ex->fe_len > overlap) |
| 1646 | ex->fe_len = overlap - ex->fe_off; |
| 1647 | } |
| 1648 | |
| 1649 | *num_img_extents = cnt; |
| 1650 | } |
| 1651 | |
| 1652 | /* |
| 1653 | * Determine the byte range(s) covered by either just the object extent |
| 1654 | * or the entire object in the parent image. |
| 1655 | */ |
| 1656 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, |
| 1657 | bool entire) |
| 1658 | { |
| 1659 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1660 | int ret; |
| 1661 | |
| 1662 | if (!rbd_dev->parent_overlap) |
| 1663 | return 0; |
| 1664 | |
| 1665 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
| 1666 | entire ? 0 : obj_req->ex.oe_off, |
| 1667 | entire ? rbd_dev->layout.object_size : |
| 1668 | obj_req->ex.oe_len, |
| 1669 | &obj_req->img_extents, |
| 1670 | &obj_req->num_img_extents); |
| 1671 | if (ret) |
| 1672 | return ret; |
| 1673 | |
| 1674 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 1675 | rbd_dev->parent_overlap); |
| 1676 | return 0; |
| 1677 | } |
| 1678 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1679 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1680 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1681 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1682 | case OBJ_REQUEST_BIO: |
| 1683 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1684 | &obj_req->bio_pos, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1685 | obj_req->ex.oe_len); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1686 | break; |
| 1687 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1688 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1689 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1690 | obj_req->ex.oe_len); |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1691 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1692 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1693 | &obj_req->bvec_pos); |
| 1694 | break; |
| 1695 | default: |
| 1696 | rbd_assert(0); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1701 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1702 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1703 | if (!obj_req->osd_req) |
| 1704 | return -ENOMEM; |
| 1705 | |
| 1706 | osd_req_op_extent_init(obj_req->osd_req, 0, CEPH_OSD_OP_READ, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1707 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1708 | rbd_osd_req_setup_data(obj_req, 0); |
| 1709 | |
| 1710 | rbd_osd_req_format_read(obj_req); |
| 1711 | return 0; |
| 1712 | } |
| 1713 | |
| 1714 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1715 | unsigned int which) |
| 1716 | { |
| 1717 | struct page **pages; |
| 1718 | |
| 1719 | /* |
| 1720 | * The response data for a STAT call consists of: |
| 1721 | * le64 length; |
| 1722 | * struct { |
| 1723 | * le32 tv_sec; |
| 1724 | * le32 tv_nsec; |
| 1725 | * } mtime; |
| 1726 | */ |
| 1727 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1728 | if (IS_ERR(pages)) |
| 1729 | return PTR_ERR(pages); |
| 1730 | |
| 1731 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1732 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1733 | 8 + sizeof(struct ceph_timespec), |
| 1734 | 0, false, true); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
| 1738 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 1739 | unsigned int which) |
| 1740 | { |
| 1741 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1742 | u16 opcode; |
| 1743 | |
| 1744 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 1745 | rbd_dev->layout.object_size, |
| 1746 | rbd_dev->layout.object_size); |
| 1747 | |
| 1748 | if (rbd_obj_is_entire(obj_req)) |
| 1749 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1750 | else |
| 1751 | opcode = CEPH_OSD_OP_WRITE; |
| 1752 | |
| 1753 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1754 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1755 | rbd_osd_req_setup_data(obj_req, which++); |
| 1756 | |
| 1757 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1758 | rbd_osd_req_format_write(obj_req); |
| 1759 | } |
| 1760 | |
| 1761 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
| 1762 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1763 | unsigned int num_osd_ops, which = 0; |
| 1764 | int ret; |
| 1765 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1766 | /* reverse map the entire object onto the parent */ |
| 1767 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1768 | if (ret) |
| 1769 | return ret; |
| 1770 | |
| 1771 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1772 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1773 | num_osd_ops = 3; /* stat + setallochint + write/writefull */ |
| 1774 | } else { |
| 1775 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1776 | num_osd_ops = 2; /* setallochint + write/writefull */ |
| 1777 | } |
| 1778 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1779 | 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] | 1780 | if (!obj_req->osd_req) |
| 1781 | return -ENOMEM; |
| 1782 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1783 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1784 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1785 | if (ret) |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
| 1789 | __rbd_obj_setup_write(obj_req, which); |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | static void __rbd_obj_setup_discard(struct rbd_obj_request *obj_req, |
| 1794 | unsigned int which) |
| 1795 | { |
| 1796 | u16 opcode; |
| 1797 | |
| 1798 | if (rbd_obj_is_entire(obj_req)) { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1799 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1800 | osd_req_op_init(obj_req->osd_req, which++, |
| 1801 | CEPH_OSD_OP_CREATE, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1802 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1803 | } else { |
| 1804 | osd_req_op_init(obj_req->osd_req, which++, |
| 1805 | CEPH_OSD_OP_DELETE, 0); |
| 1806 | opcode = 0; |
| 1807 | } |
| 1808 | } else if (rbd_obj_is_tail(obj_req)) { |
| 1809 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1810 | } else { |
| 1811 | opcode = CEPH_OSD_OP_ZERO; |
| 1812 | } |
| 1813 | |
| 1814 | if (opcode) |
| 1815 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1816 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1817 | 0, 0); |
| 1818 | |
| 1819 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1820 | rbd_osd_req_format_write(obj_req); |
| 1821 | } |
| 1822 | |
| 1823 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 1824 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1825 | unsigned int num_osd_ops, which = 0; |
| 1826 | int ret; |
| 1827 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1828 | /* reverse map the entire object onto the parent */ |
| 1829 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1830 | if (ret) |
| 1831 | return ret; |
| 1832 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1833 | if (rbd_obj_is_entire(obj_req)) { |
| 1834 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1835 | if (obj_req->num_img_extents) |
| 1836 | num_osd_ops = 2; /* create + truncate */ |
| 1837 | else |
| 1838 | num_osd_ops = 1; /* delete */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1839 | } else { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1840 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1841 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1842 | num_osd_ops = 2; /* stat + truncate/zero */ |
| 1843 | } else { |
| 1844 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1845 | num_osd_ops = 1; /* truncate/zero */ |
| 1846 | } |
| 1847 | } |
| 1848 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1849 | 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] | 1850 | if (!obj_req->osd_req) |
| 1851 | return -ENOMEM; |
| 1852 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1853 | if (!rbd_obj_is_entire(obj_req) && obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1854 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1855 | if (ret) |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
| 1859 | __rbd_obj_setup_discard(obj_req, which); |
| 1860 | return 0; |
| 1861 | } |
| 1862 | |
| 1863 | /* |
| 1864 | * For each object request in @img_req, allocate an OSD request, add |
| 1865 | * individual OSD ops and prepare them for submission. The number of |
| 1866 | * OSD ops depends on op_type and the overlap point (if any). |
| 1867 | */ |
| 1868 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 1869 | { |
| 1870 | struct rbd_obj_request *obj_req; |
| 1871 | int ret; |
| 1872 | |
| 1873 | for_each_obj_request(img_req, obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1874 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1875 | case OBJ_OP_READ: |
| 1876 | ret = rbd_obj_setup_read(obj_req); |
| 1877 | break; |
| 1878 | case OBJ_OP_WRITE: |
| 1879 | ret = rbd_obj_setup_write(obj_req); |
| 1880 | break; |
| 1881 | case OBJ_OP_DISCARD: |
| 1882 | ret = rbd_obj_setup_discard(obj_req); |
| 1883 | break; |
| 1884 | default: |
| 1885 | rbd_assert(0); |
| 1886 | } |
| 1887 | if (ret) |
| 1888 | return ret; |
| 1889 | } |
| 1890 | |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1894 | union rbd_img_fill_iter { |
| 1895 | struct ceph_bio_iter bio_iter; |
| 1896 | struct ceph_bvec_iter bvec_iter; |
| 1897 | }; |
| 1898 | |
| 1899 | struct rbd_img_fill_ctx { |
| 1900 | enum obj_request_type pos_type; |
| 1901 | union rbd_img_fill_iter *pos; |
| 1902 | union rbd_img_fill_iter iter; |
| 1903 | ceph_object_extent_fn_t set_pos_fn; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1904 | ceph_object_extent_fn_t count_fn; |
| 1905 | ceph_object_extent_fn_t copy_fn; |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1906 | }; |
| 1907 | |
| 1908 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
| 1909 | { |
| 1910 | struct rbd_img_request *img_req = arg; |
| 1911 | struct rbd_obj_request *obj_req; |
| 1912 | |
| 1913 | obj_req = rbd_obj_request_create(); |
| 1914 | if (!obj_req) |
| 1915 | return NULL; |
| 1916 | |
| 1917 | rbd_img_obj_request_add(img_req, obj_req); |
| 1918 | return &obj_req->ex; |
| 1919 | } |
| 1920 | |
| 1921 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1922 | * While su != os && sc == 1 is technically not fancy (it's the same |
| 1923 | * layout as su == os && sc == 1), we can't use the nocopy path for it |
| 1924 | * because ->set_pos_fn() should be called only once per object. |
| 1925 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so |
| 1926 | * treat su != os && sc == 1 as fancy. |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1927 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1928 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) |
| 1929 | { |
| 1930 | return l->stripe_unit != l->object_size; |
| 1931 | } |
| 1932 | |
| 1933 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
| 1934 | struct ceph_file_extent *img_extents, |
| 1935 | u32 num_img_extents, |
| 1936 | struct rbd_img_fill_ctx *fctx) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1937 | { |
| 1938 | u32 i; |
| 1939 | int ret; |
| 1940 | |
| 1941 | img_req->data_type = fctx->pos_type; |
| 1942 | |
| 1943 | /* |
| 1944 | * Create object requests and set each object request's starting |
| 1945 | * position in the provided bio (list) or bio_vec array. |
| 1946 | */ |
| 1947 | fctx->iter = *fctx->pos; |
| 1948 | for (i = 0; i < num_img_extents; i++) { |
| 1949 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, |
| 1950 | img_extents[i].fe_off, |
| 1951 | img_extents[i].fe_len, |
| 1952 | &img_req->object_extents, |
| 1953 | alloc_object_extent, img_req, |
| 1954 | fctx->set_pos_fn, &fctx->iter); |
| 1955 | if (ret) |
| 1956 | return ret; |
| 1957 | } |
| 1958 | |
| 1959 | return __rbd_img_fill_request(img_req); |
| 1960 | } |
| 1961 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 1962 | /* |
| 1963 | * Map a list of image extents to a list of object extents, create the |
| 1964 | * corresponding object requests (normally each to a different object, |
| 1965 | * but not always) and add them to @img_req. For each object request, |
| 1966 | * set up its data descriptor to point to the corresponding chunk(s) of |
| 1967 | * @fctx->pos data buffer. |
| 1968 | * |
| 1969 | * Because ceph_file_to_extents() will merge adjacent object extents |
| 1970 | * together, each object request's data descriptor may point to multiple |
| 1971 | * different chunks of @fctx->pos data buffer. |
| 1972 | * |
| 1973 | * @fctx->pos data buffer is assumed to be large enough. |
| 1974 | */ |
| 1975 | static int rbd_img_fill_request(struct rbd_img_request *img_req, |
| 1976 | struct ceph_file_extent *img_extents, |
| 1977 | u32 num_img_extents, |
| 1978 | struct rbd_img_fill_ctx *fctx) |
| 1979 | { |
| 1980 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 1981 | struct rbd_obj_request *obj_req; |
| 1982 | u32 i; |
| 1983 | int ret; |
| 1984 | |
| 1985 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
| 1986 | !rbd_layout_is_fancy(&rbd_dev->layout)) |
| 1987 | return rbd_img_fill_request_nocopy(img_req, img_extents, |
| 1988 | num_img_extents, fctx); |
| 1989 | |
| 1990 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
| 1991 | |
| 1992 | /* |
| 1993 | * Create object requests and determine ->bvec_count for each object |
| 1994 | * request. Note that ->bvec_count sum over all object requests may |
| 1995 | * be greater than the number of bio_vecs in the provided bio (list) |
| 1996 | * or bio_vec array because when mapped, those bio_vecs can straddle |
| 1997 | * stripe unit boundaries. |
| 1998 | */ |
| 1999 | fctx->iter = *fctx->pos; |
| 2000 | for (i = 0; i < num_img_extents; i++) { |
| 2001 | ret = ceph_file_to_extents(&rbd_dev->layout, |
| 2002 | img_extents[i].fe_off, |
| 2003 | img_extents[i].fe_len, |
| 2004 | &img_req->object_extents, |
| 2005 | alloc_object_extent, img_req, |
| 2006 | fctx->count_fn, &fctx->iter); |
| 2007 | if (ret) |
| 2008 | return ret; |
| 2009 | } |
| 2010 | |
| 2011 | for_each_obj_request(img_req, obj_req) { |
| 2012 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, |
| 2013 | sizeof(*obj_req->bvec_pos.bvecs), |
| 2014 | GFP_NOIO); |
| 2015 | if (!obj_req->bvec_pos.bvecs) |
| 2016 | return -ENOMEM; |
| 2017 | } |
| 2018 | |
| 2019 | /* |
| 2020 | * Fill in each object request's private bio_vec array, splitting and |
| 2021 | * rearranging the provided bio_vecs in stripe unit chunks as needed. |
| 2022 | */ |
| 2023 | fctx->iter = *fctx->pos; |
| 2024 | for (i = 0; i < num_img_extents; i++) { |
| 2025 | ret = ceph_iterate_extents(&rbd_dev->layout, |
| 2026 | img_extents[i].fe_off, |
| 2027 | img_extents[i].fe_len, |
| 2028 | &img_req->object_extents, |
| 2029 | fctx->copy_fn, &fctx->iter); |
| 2030 | if (ret) |
| 2031 | return ret; |
| 2032 | } |
| 2033 | |
| 2034 | return __rbd_img_fill_request(img_req); |
| 2035 | } |
| 2036 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2037 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, |
| 2038 | u64 off, u64 len) |
| 2039 | { |
| 2040 | struct ceph_file_extent ex = { off, len }; |
| 2041 | union rbd_img_fill_iter dummy; |
| 2042 | struct rbd_img_fill_ctx fctx = { |
| 2043 | .pos_type = OBJ_REQUEST_NODATA, |
| 2044 | .pos = &dummy, |
| 2045 | }; |
| 2046 | |
| 2047 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); |
| 2048 | } |
| 2049 | |
| 2050 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2051 | { |
| 2052 | struct rbd_obj_request *obj_req = |
| 2053 | container_of(ex, struct rbd_obj_request, ex); |
| 2054 | struct ceph_bio_iter *it = arg; |
| 2055 | |
| 2056 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2057 | obj_req->bio_pos = *it; |
| 2058 | ceph_bio_iter_advance(it, bytes); |
| 2059 | } |
| 2060 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 2061 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2062 | { |
| 2063 | struct rbd_obj_request *obj_req = |
| 2064 | container_of(ex, struct rbd_obj_request, ex); |
| 2065 | struct ceph_bio_iter *it = arg; |
| 2066 | |
| 2067 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2068 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2069 | obj_req->bvec_count++; |
| 2070 | })); |
| 2071 | |
| 2072 | } |
| 2073 | |
| 2074 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2075 | { |
| 2076 | struct rbd_obj_request *obj_req = |
| 2077 | container_of(ex, struct rbd_obj_request, ex); |
| 2078 | struct ceph_bio_iter *it = arg; |
| 2079 | |
| 2080 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2081 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2082 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2083 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2084 | })); |
| 2085 | } |
| 2086 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2087 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2088 | struct ceph_file_extent *img_extents, |
| 2089 | u32 num_img_extents, |
| 2090 | struct ceph_bio_iter *bio_pos) |
| 2091 | { |
| 2092 | struct rbd_img_fill_ctx fctx = { |
| 2093 | .pos_type = OBJ_REQUEST_BIO, |
| 2094 | .pos = (union rbd_img_fill_iter *)bio_pos, |
| 2095 | .set_pos_fn = set_bio_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 2096 | .count_fn = count_bio_bvecs, |
| 2097 | .copy_fn = copy_bio_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2098 | }; |
| 2099 | |
| 2100 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2101 | &fctx); |
| 2102 | } |
| 2103 | |
| 2104 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2105 | u64 off, u64 len, struct bio *bio) |
| 2106 | { |
| 2107 | struct ceph_file_extent ex = { off, len }; |
| 2108 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; |
| 2109 | |
| 2110 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
| 2111 | } |
| 2112 | |
| 2113 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2114 | { |
| 2115 | struct rbd_obj_request *obj_req = |
| 2116 | container_of(ex, struct rbd_obj_request, ex); |
| 2117 | struct ceph_bvec_iter *it = arg; |
| 2118 | |
| 2119 | obj_req->bvec_pos = *it; |
| 2120 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); |
| 2121 | ceph_bvec_iter_advance(it, bytes); |
| 2122 | } |
| 2123 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 2124 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2125 | { |
| 2126 | struct rbd_obj_request *obj_req = |
| 2127 | container_of(ex, struct rbd_obj_request, ex); |
| 2128 | struct ceph_bvec_iter *it = arg; |
| 2129 | |
| 2130 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2131 | obj_req->bvec_count++; |
| 2132 | })); |
| 2133 | } |
| 2134 | |
| 2135 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2136 | { |
| 2137 | struct rbd_obj_request *obj_req = |
| 2138 | container_of(ex, struct rbd_obj_request, ex); |
| 2139 | struct ceph_bvec_iter *it = arg; |
| 2140 | |
| 2141 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2142 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2143 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2144 | })); |
| 2145 | } |
| 2146 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2147 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2148 | struct ceph_file_extent *img_extents, |
| 2149 | u32 num_img_extents, |
| 2150 | struct ceph_bvec_iter *bvec_pos) |
| 2151 | { |
| 2152 | struct rbd_img_fill_ctx fctx = { |
| 2153 | .pos_type = OBJ_REQUEST_BVECS, |
| 2154 | .pos = (union rbd_img_fill_iter *)bvec_pos, |
| 2155 | .set_pos_fn = set_bvec_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 2156 | .count_fn = count_bvecs, |
| 2157 | .copy_fn = copy_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2158 | }; |
| 2159 | |
| 2160 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2161 | &fctx); |
| 2162 | } |
| 2163 | |
| 2164 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2165 | struct ceph_file_extent *img_extents, |
| 2166 | u32 num_img_extents, |
| 2167 | struct bio_vec *bvecs) |
| 2168 | { |
| 2169 | struct ceph_bvec_iter it = { |
| 2170 | .bvecs = bvecs, |
| 2171 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, |
| 2172 | num_img_extents) }, |
| 2173 | }; |
| 2174 | |
| 2175 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
| 2176 | &it); |
| 2177 | } |
| 2178 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2179 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2180 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2181 | struct rbd_obj_request *obj_request; |
| 2182 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2183 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2184 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2185 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2186 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2187 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2188 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2189 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2190 | } |
| 2191 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2192 | static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2193 | { |
| 2194 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2195 | struct rbd_img_request *child_img_req; |
| 2196 | int ret; |
| 2197 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2198 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
| 2199 | OBJ_OP_READ, NULL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2200 | if (!child_img_req) |
| 2201 | return -ENOMEM; |
| 2202 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2203 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
| 2204 | child_img_req->obj_request = obj_req; |
| 2205 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2206 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2207 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2208 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2209 | ret = __rbd_img_fill_from_bio(child_img_req, |
| 2210 | obj_req->img_extents, |
| 2211 | obj_req->num_img_extents, |
| 2212 | &obj_req->bio_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2213 | break; |
| 2214 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame^] | 2215 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2216 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
| 2217 | obj_req->img_extents, |
| 2218 | obj_req->num_img_extents, |
| 2219 | &obj_req->bvec_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2220 | break; |
| 2221 | default: |
| 2222 | rbd_assert(0); |
| 2223 | } |
| 2224 | } else { |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2225 | ret = rbd_img_fill_from_bvecs(child_img_req, |
| 2226 | obj_req->img_extents, |
| 2227 | obj_req->num_img_extents, |
| 2228 | obj_req->copyup_bvecs); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2229 | } |
| 2230 | if (ret) { |
| 2231 | rbd_img_request_put(child_img_req); |
| 2232 | return ret; |
| 2233 | } |
| 2234 | |
| 2235 | rbd_img_request_submit(child_img_req); |
| 2236 | return 0; |
| 2237 | } |
| 2238 | |
| 2239 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2240 | { |
| 2241 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2242 | int ret; |
| 2243 | |
| 2244 | if (obj_req->result == -ENOENT && |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2245 | rbd_dev->parent_overlap && !obj_req->tried_parent) { |
| 2246 | /* reverse map this object extent onto the parent */ |
| 2247 | ret = rbd_obj_calc_img_extents(obj_req, false); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2248 | if (ret) { |
| 2249 | obj_req->result = ret; |
| 2250 | return true; |
| 2251 | } |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2252 | |
| 2253 | if (obj_req->num_img_extents) { |
| 2254 | obj_req->tried_parent = true; |
| 2255 | ret = rbd_obj_read_from_parent(obj_req); |
| 2256 | if (ret) { |
| 2257 | obj_req->result = ret; |
| 2258 | return true; |
| 2259 | } |
| 2260 | return false; |
| 2261 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2266 | * length of the request. A short read also implies zero-fill |
| 2267 | * to the end of the request. In both cases we update xferred |
| 2268 | * count to indicate the whole request was satisfied. |
| 2269 | */ |
| 2270 | if (obj_req->result == -ENOENT || |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2271 | (!obj_req->result && obj_req->xferred < obj_req->ex.oe_len)) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2272 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2273 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2274 | obj_req->ex.oe_len - obj_req->xferred); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2275 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2276 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | return true; |
| 2280 | } |
| 2281 | |
| 2282 | /* |
| 2283 | * copyup_bvecs pages are never highmem pages |
| 2284 | */ |
| 2285 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2286 | { |
| 2287 | struct ceph_bvec_iter it = { |
| 2288 | .bvecs = bvecs, |
| 2289 | .iter = { .bi_size = bytes }, |
| 2290 | }; |
| 2291 | |
| 2292 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2293 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2294 | bv.bv_len)) |
| 2295 | return false; |
| 2296 | })); |
| 2297 | return true; |
| 2298 | } |
| 2299 | |
| 2300 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2301 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2302 | unsigned int num_osd_ops = obj_req->osd_req->r_num_ops; |
| 2303 | |
| 2304 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2305 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
| 2306 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2307 | |
| 2308 | /* |
| 2309 | * Create a copyup request with the same number of OSD ops as |
| 2310 | * the original request. The original request was stat + op(s), |
| 2311 | * the new copyup request will be copyup + the same op(s). |
| 2312 | */ |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2313 | 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] | 2314 | if (!obj_req->osd_req) |
| 2315 | return -ENOMEM; |
| 2316 | |
| 2317 | /* |
| 2318 | * Only send non-zero copyup data to save some I/O and network |
| 2319 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2320 | * existing. |
| 2321 | */ |
| 2322 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2323 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2324 | bytes = 0; |
| 2325 | } |
| 2326 | |
| 2327 | osd_req_op_cls_init(obj_req->osd_req, 0, CEPH_OSD_OP_CALL, "rbd", |
| 2328 | "copyup"); |
| 2329 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
| 2330 | obj_req->copyup_bvecs, bytes); |
| 2331 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2332 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2333 | case OBJ_OP_WRITE: |
| 2334 | __rbd_obj_setup_write(obj_req, 1); |
| 2335 | break; |
| 2336 | case OBJ_OP_DISCARD: |
| 2337 | rbd_assert(!rbd_obj_is_entire(obj_req)); |
| 2338 | __rbd_obj_setup_discard(obj_req, 1); |
| 2339 | break; |
| 2340 | default: |
| 2341 | rbd_assert(0); |
| 2342 | } |
| 2343 | |
| 2344 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2345 | return 0; |
| 2346 | } |
| 2347 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2348 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2349 | { |
| 2350 | u32 i; |
| 2351 | |
| 2352 | rbd_assert(!obj_req->copyup_bvecs); |
| 2353 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2354 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2355 | sizeof(*obj_req->copyup_bvecs), |
| 2356 | GFP_NOIO); |
| 2357 | if (!obj_req->copyup_bvecs) |
| 2358 | return -ENOMEM; |
| 2359 | |
| 2360 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2361 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2362 | |
| 2363 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2364 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2365 | return -ENOMEM; |
| 2366 | |
| 2367 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2368 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2369 | obj_overlap -= len; |
| 2370 | } |
| 2371 | |
| 2372 | rbd_assert(!obj_overlap); |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2376 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2377 | { |
| 2378 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2379 | int ret; |
| 2380 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2381 | rbd_assert(obj_req->num_img_extents); |
| 2382 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 2383 | rbd_dev->parent_overlap); |
| 2384 | if (!obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2385 | /* |
| 2386 | * The overlap has become 0 (most likely because the |
| 2387 | * image has been flattened). Use rbd_obj_issue_copyup() |
| 2388 | * to re-submit the original write request -- the copyup |
| 2389 | * operation itself will be a no-op, since someone must |
| 2390 | * have populated the child object while we weren't |
| 2391 | * looking. Move to WRITE_FLAT state as we'll be done |
| 2392 | * with the operation once the null copyup completes. |
| 2393 | */ |
| 2394 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2395 | return rbd_obj_issue_copyup(obj_req, 0); |
| 2396 | } |
| 2397 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2398 | ret = setup_copyup_bvecs(obj_req, rbd_obj_img_extents_bytes(obj_req)); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2399 | if (ret) |
| 2400 | return ret; |
| 2401 | |
| 2402 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2403 | return rbd_obj_read_from_parent(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2407 | { |
| 2408 | int ret; |
| 2409 | |
| 2410 | again: |
| 2411 | switch (obj_req->write_state) { |
| 2412 | case RBD_OBJ_WRITE_GUARD: |
| 2413 | rbd_assert(!obj_req->xferred); |
| 2414 | if (obj_req->result == -ENOENT) { |
| 2415 | /* |
| 2416 | * The target object doesn't exist. Read the data for |
| 2417 | * the entire target object up to the overlap point (if |
| 2418 | * any) from the parent, so we can use it for a copyup. |
| 2419 | */ |
| 2420 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2421 | if (ret) { |
| 2422 | obj_req->result = ret; |
| 2423 | return true; |
| 2424 | } |
| 2425 | return false; |
| 2426 | } |
| 2427 | /* fall through */ |
| 2428 | case RBD_OBJ_WRITE_FLAT: |
| 2429 | if (!obj_req->result) |
| 2430 | /* |
| 2431 | * There is no such thing as a successful short |
| 2432 | * write -- indicate the whole request was satisfied. |
| 2433 | */ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2434 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2435 | return true; |
| 2436 | case RBD_OBJ_WRITE_COPYUP: |
| 2437 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2438 | if (obj_req->result) |
| 2439 | goto again; |
| 2440 | |
| 2441 | rbd_assert(obj_req->xferred); |
| 2442 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2443 | if (ret) { |
| 2444 | obj_req->result = ret; |
| 2445 | return true; |
| 2446 | } |
| 2447 | return false; |
| 2448 | default: |
| 2449 | rbd_assert(0); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | /* |
| 2454 | * Returns true if @obj_req is completed, or false otherwise. |
| 2455 | */ |
| 2456 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2457 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2458 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2459 | case OBJ_OP_READ: |
| 2460 | return rbd_obj_handle_read(obj_req); |
| 2461 | case OBJ_OP_WRITE: |
| 2462 | return rbd_obj_handle_write(obj_req); |
| 2463 | case OBJ_OP_DISCARD: |
| 2464 | if (rbd_obj_handle_write(obj_req)) { |
| 2465 | /* |
| 2466 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2467 | * a non-existent object is not a problem. |
| 2468 | */ |
| 2469 | if (obj_req->result == -ENOENT) { |
| 2470 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2471 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2472 | } |
| 2473 | return true; |
| 2474 | } |
| 2475 | return false; |
| 2476 | default: |
| 2477 | rbd_assert(0); |
| 2478 | } |
| 2479 | } |
| 2480 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2481 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2482 | { |
| 2483 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2484 | |
| 2485 | rbd_assert((!obj_req->result && |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2486 | obj_req->xferred == obj_req->ex.oe_len) || |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2487 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2488 | if (!obj_req->result) { |
| 2489 | img_req->xferred += obj_req->xferred; |
| 2490 | return; |
| 2491 | } |
| 2492 | |
| 2493 | rbd_warn(img_req->rbd_dev, |
| 2494 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2495 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, |
| 2496 | obj_req->ex.oe_off, obj_req->ex.oe_len, obj_req->result, |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2497 | obj_req->xferred); |
| 2498 | if (!img_req->result) { |
| 2499 | img_req->result = obj_req->result; |
| 2500 | img_req->xferred = 0; |
| 2501 | } |
| 2502 | } |
| 2503 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2504 | static void rbd_img_end_child_request(struct rbd_img_request *img_req) |
| 2505 | { |
| 2506 | struct rbd_obj_request *obj_req = img_req->obj_request; |
| 2507 | |
| 2508 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2509 | rbd_assert((!img_req->result && |
| 2510 | img_req->xferred == rbd_obj_img_extents_bytes(obj_req)) || |
| 2511 | (img_req->result < 0 && !img_req->xferred)); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2512 | |
| 2513 | obj_req->result = img_req->result; |
| 2514 | obj_req->xferred = img_req->xferred; |
| 2515 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2516 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2517 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2518 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2519 | { |
| 2520 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2521 | rbd_assert((!img_req->result && |
| 2522 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2523 | (img_req->result < 0 && !img_req->xferred)); |
| 2524 | |
| 2525 | blk_mq_end_request(img_req->rq, |
| 2526 | errno_to_blk_status(img_req->result)); |
| 2527 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2531 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2532 | struct rbd_img_request *img_req; |
| 2533 | |
| 2534 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2535 | if (!__rbd_obj_handle_request(obj_req)) |
| 2536 | return; |
| 2537 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2538 | img_req = obj_req->img_request; |
| 2539 | spin_lock(&img_req->completion_lock); |
| 2540 | rbd_obj_end_request(obj_req); |
| 2541 | rbd_assert(img_req->pending_count); |
| 2542 | if (--img_req->pending_count) { |
| 2543 | spin_unlock(&img_req->completion_lock); |
| 2544 | return; |
| 2545 | } |
| 2546 | |
| 2547 | spin_unlock(&img_req->completion_lock); |
| 2548 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2549 | obj_req = img_req->obj_request; |
| 2550 | rbd_img_end_child_request(img_req); |
| 2551 | goto again; |
| 2552 | } |
| 2553 | rbd_img_end_request(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2554 | } |
| 2555 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2556 | static const struct rbd_client_id rbd_empty_cid; |
| 2557 | |
| 2558 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2559 | const struct rbd_client_id *rhs) |
| 2560 | { |
| 2561 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2562 | } |
| 2563 | |
| 2564 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2565 | { |
| 2566 | struct rbd_client_id cid; |
| 2567 | |
| 2568 | mutex_lock(&rbd_dev->watch_mutex); |
| 2569 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2570 | cid.handle = rbd_dev->watch_cookie; |
| 2571 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2572 | return cid; |
| 2573 | } |
| 2574 | |
| 2575 | /* |
| 2576 | * lock_rwsem must be held for write |
| 2577 | */ |
| 2578 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2579 | const struct rbd_client_id *cid) |
| 2580 | { |
| 2581 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2582 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2583 | cid->gid, cid->handle); |
| 2584 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2585 | } |
| 2586 | |
| 2587 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2588 | { |
| 2589 | mutex_lock(&rbd_dev->watch_mutex); |
| 2590 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2591 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2592 | } |
| 2593 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2594 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2595 | { |
| 2596 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2597 | |
| 2598 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2599 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2600 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2601 | } |
| 2602 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2603 | /* |
| 2604 | * lock_rwsem must be held for write |
| 2605 | */ |
| 2606 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2607 | { |
| 2608 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2609 | char cookie[32]; |
| 2610 | int ret; |
| 2611 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2612 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2613 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2614 | |
| 2615 | format_lock_cookie(rbd_dev, cookie); |
| 2616 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2617 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2618 | RBD_LOCK_TAG, "", 0); |
| 2619 | if (ret) |
| 2620 | return ret; |
| 2621 | |
| 2622 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2623 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2624 | return 0; |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * lock_rwsem must be held for write |
| 2629 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2630 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2631 | { |
| 2632 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2633 | int ret; |
| 2634 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2635 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2636 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2637 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2638 | 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] | 2639 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2640 | if (ret && ret != -ENOENT) |
| 2641 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2642 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2643 | /* treat errors as the image is unlocked */ |
| 2644 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2645 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2646 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2647 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2651 | enum rbd_notify_op notify_op, |
| 2652 | struct page ***preply_pages, |
| 2653 | size_t *preply_len) |
| 2654 | { |
| 2655 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2656 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2657 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 2658 | char buf[buf_size]; |
| 2659 | void *p = buf; |
| 2660 | |
| 2661 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2662 | |
| 2663 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2664 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2665 | ceph_encode_32(&p, notify_op); |
| 2666 | ceph_encode_64(&p, cid.gid); |
| 2667 | ceph_encode_64(&p, cid.handle); |
| 2668 | |
| 2669 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2670 | &rbd_dev->header_oloc, buf, buf_size, |
| 2671 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2672 | } |
| 2673 | |
| 2674 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2675 | enum rbd_notify_op notify_op) |
| 2676 | { |
| 2677 | struct page **reply_pages; |
| 2678 | size_t reply_len; |
| 2679 | |
| 2680 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2681 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2682 | } |
| 2683 | |
| 2684 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2685 | { |
| 2686 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2687 | acquired_lock_work); |
| 2688 | |
| 2689 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2690 | } |
| 2691 | |
| 2692 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2693 | { |
| 2694 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2695 | released_lock_work); |
| 2696 | |
| 2697 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2698 | } |
| 2699 | |
| 2700 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2701 | { |
| 2702 | struct page **reply_pages; |
| 2703 | size_t reply_len; |
| 2704 | bool lock_owner_responded = false; |
| 2705 | int ret; |
| 2706 | |
| 2707 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2708 | |
| 2709 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2710 | &reply_pages, &reply_len); |
| 2711 | if (ret && ret != -ETIMEDOUT) { |
| 2712 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2713 | goto out; |
| 2714 | } |
| 2715 | |
| 2716 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2717 | void *p = page_address(reply_pages[0]); |
| 2718 | void *const end = p + reply_len; |
| 2719 | u32 n; |
| 2720 | |
| 2721 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2722 | while (n--) { |
| 2723 | u8 struct_v; |
| 2724 | u32 len; |
| 2725 | |
| 2726 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2727 | p += 8 + 8; /* skip gid and cookie */ |
| 2728 | |
| 2729 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2730 | if (!len) |
| 2731 | continue; |
| 2732 | |
| 2733 | if (lock_owner_responded) { |
| 2734 | rbd_warn(rbd_dev, |
| 2735 | "duplicate lock owners detected"); |
| 2736 | ret = -EIO; |
| 2737 | goto out; |
| 2738 | } |
| 2739 | |
| 2740 | lock_owner_responded = true; |
| 2741 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2742 | &struct_v, &len); |
| 2743 | if (ret) { |
| 2744 | rbd_warn(rbd_dev, |
| 2745 | "failed to decode ResponseMessage: %d", |
| 2746 | ret); |
| 2747 | goto e_inval; |
| 2748 | } |
| 2749 | |
| 2750 | ret = ceph_decode_32(&p); |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | if (!lock_owner_responded) { |
| 2755 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 2756 | ret = -ETIMEDOUT; |
| 2757 | } |
| 2758 | |
| 2759 | out: |
| 2760 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2761 | return ret; |
| 2762 | |
| 2763 | e_inval: |
| 2764 | ret = -EINVAL; |
| 2765 | goto out; |
| 2766 | } |
| 2767 | |
| 2768 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 2769 | { |
| 2770 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 2771 | |
| 2772 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 2773 | if (wake_all) |
| 2774 | wake_up_all(&rbd_dev->lock_waitq); |
| 2775 | else |
| 2776 | wake_up(&rbd_dev->lock_waitq); |
| 2777 | } |
| 2778 | |
| 2779 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 2780 | struct ceph_locker **lockers, u32 *num_lockers) |
| 2781 | { |
| 2782 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2783 | u8 lock_type; |
| 2784 | char *lock_tag; |
| 2785 | int ret; |
| 2786 | |
| 2787 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2788 | |
| 2789 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 2790 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2791 | &lock_type, &lock_tag, lockers, num_lockers); |
| 2792 | if (ret) |
| 2793 | return ret; |
| 2794 | |
| 2795 | if (*num_lockers == 0) { |
| 2796 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 2797 | goto out; |
| 2798 | } |
| 2799 | |
| 2800 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 2801 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 2802 | lock_tag); |
| 2803 | ret = -EBUSY; |
| 2804 | goto out; |
| 2805 | } |
| 2806 | |
| 2807 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 2808 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 2809 | ret = -EBUSY; |
| 2810 | goto out; |
| 2811 | } |
| 2812 | |
| 2813 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 2814 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 2815 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 2816 | (*lockers)[0].id.cookie); |
| 2817 | ret = -EBUSY; |
| 2818 | goto out; |
| 2819 | } |
| 2820 | |
| 2821 | out: |
| 2822 | kfree(lock_tag); |
| 2823 | return ret; |
| 2824 | } |
| 2825 | |
| 2826 | static int find_watcher(struct rbd_device *rbd_dev, |
| 2827 | const struct ceph_locker *locker) |
| 2828 | { |
| 2829 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2830 | struct ceph_watch_item *watchers; |
| 2831 | u32 num_watchers; |
| 2832 | u64 cookie; |
| 2833 | int i; |
| 2834 | int ret; |
| 2835 | |
| 2836 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 2837 | &rbd_dev->header_oloc, &watchers, |
| 2838 | &num_watchers); |
| 2839 | if (ret) |
| 2840 | return ret; |
| 2841 | |
| 2842 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 2843 | for (i = 0; i < num_watchers; i++) { |
| 2844 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 2845 | sizeof(locker->info.addr)) && |
| 2846 | watchers[i].cookie == cookie) { |
| 2847 | struct rbd_client_id cid = { |
| 2848 | .gid = le64_to_cpu(watchers[i].name.num), |
| 2849 | .handle = cookie, |
| 2850 | }; |
| 2851 | |
| 2852 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 2853 | rbd_dev, cid.gid, cid.handle); |
| 2854 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2855 | ret = 1; |
| 2856 | goto out; |
| 2857 | } |
| 2858 | } |
| 2859 | |
| 2860 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 2861 | ret = 0; |
| 2862 | out: |
| 2863 | kfree(watchers); |
| 2864 | return ret; |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * lock_rwsem must be held for write |
| 2869 | */ |
| 2870 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 2871 | { |
| 2872 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 2873 | struct ceph_locker *lockers; |
| 2874 | u32 num_lockers; |
| 2875 | int ret; |
| 2876 | |
| 2877 | for (;;) { |
| 2878 | ret = rbd_lock(rbd_dev); |
| 2879 | if (ret != -EBUSY) |
| 2880 | return ret; |
| 2881 | |
| 2882 | /* determine if the current lock holder is still alive */ |
| 2883 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 2884 | if (ret) |
| 2885 | return ret; |
| 2886 | |
| 2887 | if (num_lockers == 0) |
| 2888 | goto again; |
| 2889 | |
| 2890 | ret = find_watcher(rbd_dev, lockers); |
| 2891 | if (ret) { |
| 2892 | if (ret > 0) |
| 2893 | ret = 0; /* have to request lock */ |
| 2894 | goto out; |
| 2895 | } |
| 2896 | |
| 2897 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 2898 | ENTITY_NAME(lockers[0].id.name)); |
| 2899 | |
| 2900 | ret = ceph_monc_blacklist_add(&client->monc, |
| 2901 | &lockers[0].info.addr); |
| 2902 | if (ret) { |
| 2903 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 2904 | ENTITY_NAME(lockers[0].id.name), ret); |
| 2905 | goto out; |
| 2906 | } |
| 2907 | |
| 2908 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 2909 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2910 | lockers[0].id.cookie, |
| 2911 | &lockers[0].id.name); |
| 2912 | if (ret && ret != -ENOENT) |
| 2913 | goto out; |
| 2914 | |
| 2915 | again: |
| 2916 | ceph_free_lockers(lockers, num_lockers); |
| 2917 | } |
| 2918 | |
| 2919 | out: |
| 2920 | ceph_free_lockers(lockers, num_lockers); |
| 2921 | return ret; |
| 2922 | } |
| 2923 | |
| 2924 | /* |
| 2925 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 2926 | */ |
| 2927 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 2928 | int *pret) |
| 2929 | { |
| 2930 | enum rbd_lock_state lock_state; |
| 2931 | |
| 2932 | down_read(&rbd_dev->lock_rwsem); |
| 2933 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 2934 | rbd_dev->lock_state); |
| 2935 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 2936 | lock_state = rbd_dev->lock_state; |
| 2937 | up_read(&rbd_dev->lock_rwsem); |
| 2938 | return lock_state; |
| 2939 | } |
| 2940 | |
| 2941 | up_read(&rbd_dev->lock_rwsem); |
| 2942 | down_write(&rbd_dev->lock_rwsem); |
| 2943 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 2944 | rbd_dev->lock_state); |
| 2945 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 2946 | *pret = rbd_try_lock(rbd_dev); |
| 2947 | if (*pret) |
| 2948 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 2949 | } |
| 2950 | |
| 2951 | lock_state = rbd_dev->lock_state; |
| 2952 | up_write(&rbd_dev->lock_rwsem); |
| 2953 | return lock_state; |
| 2954 | } |
| 2955 | |
| 2956 | static void rbd_acquire_lock(struct work_struct *work) |
| 2957 | { |
| 2958 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 2959 | struct rbd_device, lock_dwork); |
| 2960 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 2961 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2962 | |
| 2963 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2964 | again: |
| 2965 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 2966 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 2967 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 2968 | wake_requests(rbd_dev, true); |
| 2969 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 2970 | rbd_dev, lock_state, ret); |
| 2971 | return; |
| 2972 | } |
| 2973 | |
| 2974 | ret = rbd_request_lock(rbd_dev); |
| 2975 | if (ret == -ETIMEDOUT) { |
| 2976 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 2977 | } else if (ret == -EROFS) { |
| 2978 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 2979 | /* |
| 2980 | * If this is rbd_add_acquire_lock(), we want to fail |
| 2981 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 2982 | * want to block. |
| 2983 | */ |
| 2984 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 2985 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 2986 | /* wake "rbd map --exclusive" process */ |
| 2987 | wake_requests(rbd_dev, false); |
| 2988 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2989 | } else if (ret < 0) { |
| 2990 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 2991 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 2992 | RBD_RETRY_DELAY); |
| 2993 | } else { |
| 2994 | /* |
| 2995 | * lock owner acked, but resend if we don't see them |
| 2996 | * release the lock |
| 2997 | */ |
| 2998 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 2999 | rbd_dev); |
| 3000 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3001 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3002 | } |
| 3003 | } |
| 3004 | |
| 3005 | /* |
| 3006 | * lock_rwsem must be held for write |
| 3007 | */ |
| 3008 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3009 | { |
| 3010 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3011 | rbd_dev->lock_state); |
| 3012 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3013 | return false; |
| 3014 | |
| 3015 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3016 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3017 | /* |
| 3018 | * Ensure that all in-flight IO is flushed. |
| 3019 | * |
| 3020 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3021 | * may be shared with other devices. |
| 3022 | */ |
| 3023 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3024 | up_read(&rbd_dev->lock_rwsem); |
| 3025 | |
| 3026 | down_write(&rbd_dev->lock_rwsem); |
| 3027 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3028 | rbd_dev->lock_state); |
| 3029 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3030 | return false; |
| 3031 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3032 | rbd_unlock(rbd_dev); |
| 3033 | /* |
| 3034 | * Give others a chance to grab the lock - we would re-acquire |
| 3035 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3036 | * otherwise. We need to ack our own notifications, so this |
| 3037 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3038 | * after wake_requests() in rbd_handle_released_lock(). |
| 3039 | */ |
| 3040 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3041 | return true; |
| 3042 | } |
| 3043 | |
| 3044 | static void rbd_release_lock_work(struct work_struct *work) |
| 3045 | { |
| 3046 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3047 | unlock_work); |
| 3048 | |
| 3049 | down_write(&rbd_dev->lock_rwsem); |
| 3050 | rbd_release_lock(rbd_dev); |
| 3051 | up_write(&rbd_dev->lock_rwsem); |
| 3052 | } |
| 3053 | |
| 3054 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3055 | void **p) |
| 3056 | { |
| 3057 | struct rbd_client_id cid = { 0 }; |
| 3058 | |
| 3059 | if (struct_v >= 2) { |
| 3060 | cid.gid = ceph_decode_64(p); |
| 3061 | cid.handle = ceph_decode_64(p); |
| 3062 | } |
| 3063 | |
| 3064 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3065 | cid.handle); |
| 3066 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3067 | down_write(&rbd_dev->lock_rwsem); |
| 3068 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3069 | /* |
| 3070 | * we already know that the remote client is |
| 3071 | * the owner |
| 3072 | */ |
| 3073 | up_write(&rbd_dev->lock_rwsem); |
| 3074 | return; |
| 3075 | } |
| 3076 | |
| 3077 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3078 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3079 | } else { |
| 3080 | down_read(&rbd_dev->lock_rwsem); |
| 3081 | } |
| 3082 | |
| 3083 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3084 | wake_requests(rbd_dev, false); |
| 3085 | up_read(&rbd_dev->lock_rwsem); |
| 3086 | } |
| 3087 | |
| 3088 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3089 | void **p) |
| 3090 | { |
| 3091 | struct rbd_client_id cid = { 0 }; |
| 3092 | |
| 3093 | if (struct_v >= 2) { |
| 3094 | cid.gid = ceph_decode_64(p); |
| 3095 | cid.handle = ceph_decode_64(p); |
| 3096 | } |
| 3097 | |
| 3098 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3099 | cid.handle); |
| 3100 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3101 | down_write(&rbd_dev->lock_rwsem); |
| 3102 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3103 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3104 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3105 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3106 | up_write(&rbd_dev->lock_rwsem); |
| 3107 | return; |
| 3108 | } |
| 3109 | |
| 3110 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3111 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3112 | } else { |
| 3113 | down_read(&rbd_dev->lock_rwsem); |
| 3114 | } |
| 3115 | |
| 3116 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3117 | wake_requests(rbd_dev, false); |
| 3118 | up_read(&rbd_dev->lock_rwsem); |
| 3119 | } |
| 3120 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3121 | /* |
| 3122 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3123 | * ResponseMessage is needed. |
| 3124 | */ |
| 3125 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3126 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3127 | { |
| 3128 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3129 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3130 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3131 | |
| 3132 | if (struct_v >= 2) { |
| 3133 | cid.gid = ceph_decode_64(p); |
| 3134 | cid.handle = ceph_decode_64(p); |
| 3135 | } |
| 3136 | |
| 3137 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3138 | cid.handle); |
| 3139 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3140 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3141 | |
| 3142 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3143 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3144 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3145 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3146 | goto out_unlock; |
| 3147 | |
| 3148 | /* |
| 3149 | * encode ResponseMessage(0) so the peer can detect |
| 3150 | * a missing owner |
| 3151 | */ |
| 3152 | result = 0; |
| 3153 | |
| 3154 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3155 | if (!rbd_dev->opts->exclusive) { |
| 3156 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3157 | __func__, rbd_dev); |
| 3158 | queue_work(rbd_dev->task_wq, |
| 3159 | &rbd_dev->unlock_work); |
| 3160 | } else { |
| 3161 | /* refuse to release the lock */ |
| 3162 | result = -EROFS; |
| 3163 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3164 | } |
| 3165 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3166 | |
| 3167 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3168 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3169 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3173 | u64 notify_id, u64 cookie, s32 *result) |
| 3174 | { |
| 3175 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3176 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3177 | char buf[buf_size]; |
| 3178 | int ret; |
| 3179 | |
| 3180 | if (result) { |
| 3181 | void *p = buf; |
| 3182 | |
| 3183 | /* encode ResponseMessage */ |
| 3184 | ceph_start_encoding(&p, 1, 1, |
| 3185 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3186 | ceph_encode_32(&p, *result); |
| 3187 | } else { |
| 3188 | buf_size = 0; |
| 3189 | } |
| 3190 | |
| 3191 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3192 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3193 | buf, buf_size); |
| 3194 | if (ret) |
| 3195 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3196 | } |
| 3197 | |
| 3198 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3199 | u64 cookie) |
| 3200 | { |
| 3201 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3202 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3203 | } |
| 3204 | |
| 3205 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3206 | u64 notify_id, u64 cookie, s32 result) |
| 3207 | { |
| 3208 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3209 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3210 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3211 | |
| 3212 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3213 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3214 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3215 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3216 | void *p = data; |
| 3217 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3218 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3219 | u32 len; |
| 3220 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3221 | int ret; |
| 3222 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3223 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3224 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3225 | if (data_len) { |
| 3226 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3227 | &struct_v, &len); |
| 3228 | if (ret) { |
| 3229 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3230 | ret); |
| 3231 | return; |
| 3232 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3233 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3234 | notify_op = ceph_decode_32(&p); |
| 3235 | } else { |
| 3236 | /* legacy notification for header updates */ |
| 3237 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3238 | len = 0; |
| 3239 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3240 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3241 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3242 | switch (notify_op) { |
| 3243 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3244 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3245 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3246 | break; |
| 3247 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3248 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3249 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3250 | break; |
| 3251 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3252 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3253 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3254 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3255 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3256 | else |
| 3257 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3258 | break; |
| 3259 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3260 | ret = rbd_dev_refresh(rbd_dev); |
| 3261 | if (ret) |
| 3262 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3263 | |
| 3264 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3265 | break; |
| 3266 | default: |
| 3267 | if (rbd_is_lock_owner(rbd_dev)) |
| 3268 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3269 | cookie, -EOPNOTSUPP); |
| 3270 | else |
| 3271 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3272 | break; |
| 3273 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3274 | } |
| 3275 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3276 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3277 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3278 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3279 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3280 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3281 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3282 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3283 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3284 | down_write(&rbd_dev->lock_rwsem); |
| 3285 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3286 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3287 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3288 | mutex_lock(&rbd_dev->watch_mutex); |
| 3289 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3290 | __rbd_unregister_watch(rbd_dev); |
| 3291 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3292 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3293 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3294 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3295 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3299 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3300 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3301 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3302 | { |
| 3303 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3304 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3305 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3306 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3307 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3308 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3309 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3310 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3311 | rbd_watch_errcb, rbd_dev); |
| 3312 | if (IS_ERR(handle)) |
| 3313 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3314 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3315 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3316 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3317 | } |
| 3318 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3319 | /* |
| 3320 | * watch_mutex must be locked |
| 3321 | */ |
| 3322 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3323 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3324 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3325 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3326 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3327 | rbd_assert(rbd_dev->watch_handle); |
| 3328 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3329 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3330 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3331 | if (ret) |
| 3332 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3333 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3334 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3335 | } |
| 3336 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3337 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3338 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3339 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3340 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3341 | mutex_lock(&rbd_dev->watch_mutex); |
| 3342 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3343 | ret = __rbd_register_watch(rbd_dev); |
| 3344 | if (ret) |
| 3345 | goto out; |
| 3346 | |
| 3347 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3348 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3349 | |
| 3350 | out: |
| 3351 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3352 | return ret; |
| 3353 | } |
| 3354 | |
| 3355 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3356 | { |
| 3357 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3358 | |
| 3359 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3360 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3361 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3362 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3363 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3367 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3368 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3369 | cancel_tasks_sync(rbd_dev); |
| 3370 | |
| 3371 | mutex_lock(&rbd_dev->watch_mutex); |
| 3372 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3373 | __rbd_unregister_watch(rbd_dev); |
| 3374 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3375 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3376 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3377 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3378 | } |
| 3379 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3380 | /* |
| 3381 | * lock_rwsem must be held for write |
| 3382 | */ |
| 3383 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3384 | { |
| 3385 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3386 | char cookie[32]; |
| 3387 | int ret; |
| 3388 | |
| 3389 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3390 | |
| 3391 | format_lock_cookie(rbd_dev, cookie); |
| 3392 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3393 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3394 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3395 | RBD_LOCK_TAG, cookie); |
| 3396 | if (ret) { |
| 3397 | if (ret != -EOPNOTSUPP) |
| 3398 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3399 | ret); |
| 3400 | |
| 3401 | /* |
| 3402 | * Lock cookie cannot be updated on older OSDs, so do |
| 3403 | * a manual release and queue an acquire. |
| 3404 | */ |
| 3405 | if (rbd_release_lock(rbd_dev)) |
| 3406 | queue_delayed_work(rbd_dev->task_wq, |
| 3407 | &rbd_dev->lock_dwork, 0); |
| 3408 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3409 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3413 | static void rbd_reregister_watch(struct work_struct *work) |
| 3414 | { |
| 3415 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3416 | struct rbd_device, watch_dwork); |
| 3417 | int ret; |
| 3418 | |
| 3419 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3420 | |
| 3421 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3422 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3423 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3424 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3425 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3426 | |
| 3427 | ret = __rbd_register_watch(rbd_dev); |
| 3428 | if (ret) { |
| 3429 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3430 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3431 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3432 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3433 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3434 | queue_delayed_work(rbd_dev->task_wq, |
| 3435 | &rbd_dev->watch_dwork, |
| 3436 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3437 | } |
| 3438 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3439 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3443 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3444 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3445 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3446 | down_write(&rbd_dev->lock_rwsem); |
| 3447 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3448 | rbd_reacquire_lock(rbd_dev); |
| 3449 | up_write(&rbd_dev->lock_rwsem); |
| 3450 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3451 | ret = rbd_dev_refresh(rbd_dev); |
| 3452 | if (ret) |
| 3453 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3454 | } |
| 3455 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3456 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3457 | * Synchronous osd object method call. Returns the number of bytes |
| 3458 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3459 | */ |
| 3460 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3461 | struct ceph_object_id *oid, |
| 3462 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3463 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3464 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3465 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3466 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3467 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3468 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3469 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3470 | struct page *req_page = NULL; |
| 3471 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3472 | int ret; |
| 3473 | |
| 3474 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3475 | * Method calls are ultimately read operations. The result |
| 3476 | * should placed into the inbound buffer provided. They |
| 3477 | * also supply outbound data--parameters for the object |
| 3478 | * method. Currently if this is present it will be a |
| 3479 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3480 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3481 | if (outbound) { |
| 3482 | if (outbound_size > PAGE_SIZE) |
| 3483 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3484 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3485 | req_page = alloc_page(GFP_KERNEL); |
| 3486 | if (!req_page) |
| 3487 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3488 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3489 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3490 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3491 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3492 | reply_page = alloc_page(GFP_KERNEL); |
| 3493 | if (!reply_page) { |
| 3494 | if (req_page) |
| 3495 | __free_page(req_page); |
| 3496 | return -ENOMEM; |
| 3497 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3498 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3499 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3500 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3501 | reply_page, &inbound_size); |
| 3502 | if (!ret) { |
| 3503 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3504 | ret = inbound_size; |
| 3505 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3506 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3507 | if (req_page) |
| 3508 | __free_page(req_page); |
| 3509 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3510 | return ret; |
| 3511 | } |
| 3512 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3513 | /* |
| 3514 | * lock_rwsem must be held for read |
| 3515 | */ |
| 3516 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 3517 | { |
| 3518 | DEFINE_WAIT(wait); |
| 3519 | |
| 3520 | do { |
| 3521 | /* |
| 3522 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3523 | * and cancel_delayed_work() in wake_requests(). |
| 3524 | */ |
| 3525 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3526 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3527 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3528 | TASK_UNINTERRUPTIBLE); |
| 3529 | up_read(&rbd_dev->lock_rwsem); |
| 3530 | schedule(); |
| 3531 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3532 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
| 3533 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)); |
| 3534 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3535 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 3536 | } |
| 3537 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3538 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3539 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3540 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3541 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3542 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3543 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3544 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3545 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3546 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3547 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3548 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3549 | int result; |
| 3550 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3551 | switch (req_op(rq)) { |
| 3552 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3553 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3554 | op_type = OBJ_OP_DISCARD; |
| 3555 | break; |
| 3556 | case REQ_OP_WRITE: |
| 3557 | op_type = OBJ_OP_WRITE; |
| 3558 | break; |
| 3559 | case REQ_OP_READ: |
| 3560 | op_type = OBJ_OP_READ; |
| 3561 | break; |
| 3562 | default: |
| 3563 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3564 | result = -EIO; |
| 3565 | goto err; |
| 3566 | } |
| 3567 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3568 | /* Ignore/skip any zero-length requests */ |
| 3569 | |
| 3570 | if (!length) { |
| 3571 | dout("%s: zero-length request\n", __func__); |
| 3572 | result = 0; |
| 3573 | goto err_rq; |
| 3574 | } |
| 3575 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3576 | rbd_assert(op_type == OBJ_OP_READ || |
| 3577 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3578 | |
| 3579 | /* |
| 3580 | * Quit early if the mapped snapshot no longer exists. It's |
| 3581 | * still possible the snapshot will have disappeared by the |
| 3582 | * time our request arrives at the osd, but there's no sense in |
| 3583 | * sending it if we already know. |
| 3584 | */ |
| 3585 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3586 | dout("request for non-existent snapshot"); |
| 3587 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3588 | result = -ENXIO; |
| 3589 | goto err_rq; |
| 3590 | } |
| 3591 | |
| 3592 | if (offset && length > U64_MAX - offset + 1) { |
| 3593 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3594 | length); |
| 3595 | result = -EINVAL; |
| 3596 | goto err_rq; /* Shouldn't happen */ |
| 3597 | } |
| 3598 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3599 | blk_mq_start_request(rq); |
| 3600 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3601 | down_read(&rbd_dev->header_rwsem); |
| 3602 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3603 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3604 | snapc = rbd_dev->header.snapc; |
| 3605 | ceph_get_snap_context(snapc); |
| 3606 | } |
| 3607 | up_read(&rbd_dev->header_rwsem); |
| 3608 | |
| 3609 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3610 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3611 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3612 | result = -EIO; |
| 3613 | goto err_rq; |
| 3614 | } |
| 3615 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3616 | must_be_locked = |
| 3617 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3618 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3619 | if (must_be_locked) { |
| 3620 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3621 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED && |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3622 | !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3623 | if (rbd_dev->opts->exclusive) { |
| 3624 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3625 | result = -EROFS; |
| 3626 | goto err_unlock; |
| 3627 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3628 | rbd_wait_state_locked(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3629 | } |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3630 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3631 | result = -EBLACKLISTED; |
| 3632 | goto err_unlock; |
| 3633 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3634 | } |
| 3635 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 3636 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3637 | if (!img_request) { |
| 3638 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3639 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3640 | } |
| 3641 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3642 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3643 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3644 | if (op_type == OBJ_OP_DISCARD) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3645 | result = rbd_img_fill_nodata(img_request, offset, length); |
| 3646 | else |
| 3647 | result = rbd_img_fill_from_bio(img_request, offset, length, |
| 3648 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3649 | if (result) |
| 3650 | goto err_img_request; |
| 3651 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3652 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3653 | if (must_be_locked) |
| 3654 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3655 | return; |
| 3656 | |
| 3657 | err_img_request: |
| 3658 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3659 | err_unlock: |
| 3660 | if (must_be_locked) |
| 3661 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3662 | err_rq: |
| 3663 | if (result) |
| 3664 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3665 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3666 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3667 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3668 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3669 | } |
| 3670 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3671 | 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] | 3672 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3673 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3674 | struct request *rq = bd->rq; |
| 3675 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3676 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3677 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3678 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3679 | } |
| 3680 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3681 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3682 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3683 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3684 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3685 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3686 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3689 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3690 | struct ceph_object_id *oid, |
| 3691 | struct ceph_object_locator *oloc, |
| 3692 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3693 | |
| 3694 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3695 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3696 | struct ceph_osd_request *req; |
| 3697 | struct page **pages; |
| 3698 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3699 | int ret; |
| 3700 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3701 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3702 | if (!req) |
| 3703 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3704 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3705 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3706 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3707 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3708 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3709 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3710 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3711 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3712 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3713 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3714 | if (IS_ERR(pages)) { |
| 3715 | ret = PTR_ERR(pages); |
| 3716 | goto out_req; |
| 3717 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3718 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3719 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3720 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3721 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3722 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3723 | ceph_osdc_start_request(osdc, req, false); |
| 3724 | ret = ceph_osdc_wait_request(osdc, req); |
| 3725 | if (ret >= 0) |
| 3726 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3727 | |
| 3728 | out_req: |
| 3729 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3730 | return ret; |
| 3731 | } |
| 3732 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3733 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3734 | * Read the complete header for the given rbd device. On successful |
| 3735 | * return, the rbd_dev->header field will contain up-to-date |
| 3736 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3737 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3738 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3739 | { |
| 3740 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 3741 | u32 snap_count = 0; |
| 3742 | u64 names_size = 0; |
| 3743 | u32 want_count; |
| 3744 | int ret; |
| 3745 | |
| 3746 | /* |
| 3747 | * The complete header will include an array of its 64-bit |
| 3748 | * snapshot ids, followed by the names of those snapshots as |
| 3749 | * a contiguous block of NUL-terminated strings. Note that |
| 3750 | * the number of snapshots could change by the time we read |
| 3751 | * it in, in which case we re-read it. |
| 3752 | */ |
| 3753 | do { |
| 3754 | size_t size; |
| 3755 | |
| 3756 | kfree(ondisk); |
| 3757 | |
| 3758 | size = sizeof (*ondisk); |
| 3759 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 3760 | size += names_size; |
| 3761 | ondisk = kmalloc(size, GFP_KERNEL); |
| 3762 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3763 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3764 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3765 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 3766 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3767 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3768 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 3769 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3770 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3771 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 3772 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3773 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3774 | } |
| 3775 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 3776 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3777 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3778 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 3782 | want_count = snap_count; |
| 3783 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 3784 | } while (snap_count != want_count); |
| 3785 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3786 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 3787 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3788 | kfree(ondisk); |
| 3789 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3790 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3793 | /* |
| 3794 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 3795 | * has disappeared from the (just updated) snapshot context. |
| 3796 | */ |
| 3797 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 3798 | { |
| 3799 | u64 snap_id; |
| 3800 | |
| 3801 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 3802 | return; |
| 3803 | |
| 3804 | snap_id = rbd_dev->spec->snap_id; |
| 3805 | if (snap_id == CEPH_NOSNAP) |
| 3806 | return; |
| 3807 | |
| 3808 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 3809 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 3810 | } |
| 3811 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3812 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 3813 | { |
| 3814 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3815 | |
| 3816 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3817 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 3818 | * try to update its size. If REMOVING is set, updating size |
| 3819 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3820 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3821 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 3822 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3823 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 3824 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 3825 | set_capacity(rbd_dev->disk, size); |
| 3826 | revalidate_disk(rbd_dev->disk); |
| 3827 | } |
| 3828 | } |
| 3829 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3830 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3831 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3832 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3833 | int ret; |
| 3834 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3835 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 3836 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 3837 | |
| 3838 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3839 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3840 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3841 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3842 | /* |
| 3843 | * If there is a parent, see if it has disappeared due to the |
| 3844 | * mapped image getting flattened. |
| 3845 | */ |
| 3846 | if (rbd_dev->parent) { |
| 3847 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 3848 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3849 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3850 | } |
| 3851 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3852 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3853 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3854 | } else { |
| 3855 | /* validate mapped snapshot's EXISTS flag */ |
| 3856 | rbd_exists_validate(rbd_dev); |
| 3857 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3858 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3859 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3860 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3861 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3862 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3863 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3864 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3865 | } |
| 3866 | |
Christoph Hellwig | d6296d3 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 3867 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 3868 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3869 | { |
| 3870 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 3871 | |
| 3872 | INIT_WORK(work, rbd_queue_workfn); |
| 3873 | return 0; |
| 3874 | } |
| 3875 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 3876 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3877 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3878 | .init_request = rbd_init_request, |
| 3879 | }; |
| 3880 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3881 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 3882 | { |
| 3883 | struct gendisk *disk; |
| 3884 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3885 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3886 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3887 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3888 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3889 | disk = alloc_disk(single_major ? |
| 3890 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 3891 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3892 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3893 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3894 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3895 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3896 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3897 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3898 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3899 | if (single_major) |
| 3900 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3901 | disk->fops = &rbd_bd_ops; |
| 3902 | disk->private_data = rbd_dev; |
| 3903 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3904 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 3905 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3906 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3907 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3908 | 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] | 3909 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 3910 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 3911 | |
| 3912 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 3913 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3914 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3915 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3916 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 3917 | if (IS_ERR(q)) { |
| 3918 | err = PTR_ERR(q); |
| 3919 | goto out_tag_set; |
| 3920 | } |
| 3921 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 3922 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 3923 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3924 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3925 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3926 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 3927 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 3928 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 3929 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 3930 | blk_queue_max_segment_size(q, UINT_MAX); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3931 | blk_queue_io_min(q, segment_size); |
| 3932 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3933 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3934 | /* enable the discard support */ |
| 3935 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 3936 | q->limits.discard_granularity = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 3937 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3938 | blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3939 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3940 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 3941 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 3942 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3943 | /* |
| 3944 | * disk_release() expects a queue ref from add_disk() and will |
| 3945 | * put it. Hold an extra ref until add_disk() is called. |
| 3946 | */ |
| 3947 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3948 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3949 | q->queuedata = rbd_dev; |
| 3950 | |
| 3951 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3952 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3953 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3954 | out_tag_set: |
| 3955 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3956 | out_disk: |
| 3957 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3958 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3959 | } |
| 3960 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3961 | /* |
| 3962 | sysfs |
| 3963 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3964 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3965 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 3966 | { |
| 3967 | return container_of(dev, struct rbd_device, dev); |
| 3968 | } |
| 3969 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3970 | static ssize_t rbd_size_show(struct device *dev, |
| 3971 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3972 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3973 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3974 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3975 | return sprintf(buf, "%llu\n", |
| 3976 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3977 | } |
| 3978 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3979 | /* |
| 3980 | * Note this shows the features for whatever's mapped, which is not |
| 3981 | * necessarily the base image. |
| 3982 | */ |
| 3983 | static ssize_t rbd_features_show(struct device *dev, |
| 3984 | struct device_attribute *attr, char *buf) |
| 3985 | { |
| 3986 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 3987 | |
| 3988 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3989 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 3990 | } |
| 3991 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3992 | static ssize_t rbd_major_show(struct device *dev, |
| 3993 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3994 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 3995 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3996 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 3997 | if (rbd_dev->major) |
| 3998 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 3999 | |
| 4000 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4001 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4002 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4003 | static ssize_t rbd_minor_show(struct device *dev, |
| 4004 | struct device_attribute *attr, char *buf) |
| 4005 | { |
| 4006 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4007 | |
| 4008 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4009 | } |
| 4010 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4011 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4012 | struct device_attribute *attr, char *buf) |
| 4013 | { |
| 4014 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4015 | struct ceph_entity_addr *client_addr = |
| 4016 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4017 | |
| 4018 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4019 | le32_to_cpu(client_addr->nonce)); |
| 4020 | } |
| 4021 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4022 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4023 | struct device_attribute *attr, char *buf) |
| 4024 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4025 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4026 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4027 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4028 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4029 | } |
| 4030 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4031 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4032 | struct device_attribute *attr, char *buf) |
| 4033 | { |
| 4034 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4035 | |
| 4036 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4037 | } |
| 4038 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4039 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4040 | struct device_attribute *attr, char *buf) |
| 4041 | { |
| 4042 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4043 | |
| 4044 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | static ssize_t rbd_pool_show(struct device *dev, |
| 4048 | struct device_attribute *attr, char *buf) |
| 4049 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4050 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4051 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4052 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4053 | } |
| 4054 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4055 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4056 | struct device_attribute *attr, char *buf) |
| 4057 | { |
| 4058 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4059 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4060 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4061 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4062 | } |
| 4063 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4064 | static ssize_t rbd_name_show(struct device *dev, |
| 4065 | struct device_attribute *attr, char *buf) |
| 4066 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4067 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4068 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4069 | if (rbd_dev->spec->image_name) |
| 4070 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4071 | |
| 4072 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4075 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4076 | struct device_attribute *attr, char *buf) |
| 4077 | { |
| 4078 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4079 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4080 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4081 | } |
| 4082 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4083 | /* |
| 4084 | * Shows the name of the currently-mapped snapshot (or |
| 4085 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4086 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4087 | static ssize_t rbd_snap_show(struct device *dev, |
| 4088 | struct device_attribute *attr, |
| 4089 | char *buf) |
| 4090 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4091 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4092 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4093 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4094 | } |
| 4095 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4096 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4097 | struct device_attribute *attr, char *buf) |
| 4098 | { |
| 4099 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4100 | |
| 4101 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4102 | } |
| 4103 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4104 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4105 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4106 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4107 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4108 | */ |
| 4109 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4110 | struct device_attribute *attr, |
| 4111 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4112 | { |
| 4113 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4114 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4115 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4116 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4117 | return sprintf(buf, "(no parent image)\n"); |
| 4118 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4119 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4120 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4121 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4122 | count += sprintf(&buf[count], "%s" |
| 4123 | "pool_id %llu\npool_name %s\n" |
| 4124 | "image_id %s\nimage_name %s\n" |
| 4125 | "snap_id %llu\nsnap_name %s\n" |
| 4126 | "overlap %llu\n", |
| 4127 | !count ? "" : "\n", /* first? */ |
| 4128 | spec->pool_id, spec->pool_name, |
| 4129 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4130 | spec->snap_id, spec->snap_name, |
| 4131 | rbd_dev->parent_overlap); |
| 4132 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4133 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4134 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4135 | } |
| 4136 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4137 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4138 | struct device_attribute *attr, |
| 4139 | const char *buf, |
| 4140 | size_t size) |
| 4141 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4142 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4143 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4144 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4145 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4146 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4147 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4148 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4149 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4150 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4151 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4152 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4153 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4154 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4155 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4156 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4157 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4158 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4159 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4160 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4161 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4162 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4163 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4164 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4165 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4166 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4167 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4168 | |
| 4169 | static struct attribute *rbd_attrs[] = { |
| 4170 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4171 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4172 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4173 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4174 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4175 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4176 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4177 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4178 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4179 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4180 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4181 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4182 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4183 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4184 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4185 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4186 | NULL |
| 4187 | }; |
| 4188 | |
| 4189 | static struct attribute_group rbd_attr_group = { |
| 4190 | .attrs = rbd_attrs, |
| 4191 | }; |
| 4192 | |
| 4193 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4194 | &rbd_attr_group, |
| 4195 | NULL |
| 4196 | }; |
| 4197 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4198 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4199 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4200 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4201 | .name = "rbd", |
| 4202 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4203 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4204 | }; |
| 4205 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4206 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4207 | { |
| 4208 | kref_get(&spec->kref); |
| 4209 | |
| 4210 | return spec; |
| 4211 | } |
| 4212 | |
| 4213 | static void rbd_spec_free(struct kref *kref); |
| 4214 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4215 | { |
| 4216 | if (spec) |
| 4217 | kref_put(&spec->kref, rbd_spec_free); |
| 4218 | } |
| 4219 | |
| 4220 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4221 | { |
| 4222 | struct rbd_spec *spec; |
| 4223 | |
| 4224 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4225 | if (!spec) |
| 4226 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4227 | |
| 4228 | spec->pool_id = CEPH_NOPOOL; |
| 4229 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4230 | kref_init(&spec->kref); |
| 4231 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4232 | return spec; |
| 4233 | } |
| 4234 | |
| 4235 | static void rbd_spec_free(struct kref *kref) |
| 4236 | { |
| 4237 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4238 | |
| 4239 | kfree(spec->pool_name); |
| 4240 | kfree(spec->image_id); |
| 4241 | kfree(spec->image_name); |
| 4242 | kfree(spec->snap_name); |
| 4243 | kfree(spec); |
| 4244 | } |
| 4245 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4246 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4247 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4248 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4249 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4250 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4251 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4252 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4253 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4254 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4255 | rbd_put_client(rbd_dev->rbd_client); |
| 4256 | rbd_spec_put(rbd_dev->spec); |
| 4257 | kfree(rbd_dev->opts); |
| 4258 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4259 | } |
| 4260 | |
| 4261 | static void rbd_dev_release(struct device *dev) |
| 4262 | { |
| 4263 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4264 | bool need_put = !!rbd_dev->opts; |
| 4265 | |
| 4266 | if (need_put) { |
| 4267 | destroy_workqueue(rbd_dev->task_wq); |
| 4268 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4269 | } |
| 4270 | |
| 4271 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4272 | |
| 4273 | /* |
| 4274 | * This is racy, but way better than putting module outside of |
| 4275 | * the release callback. The race window is pretty small, so |
| 4276 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4277 | */ |
| 4278 | if (need_put) |
| 4279 | module_put(THIS_MODULE); |
| 4280 | } |
| 4281 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4282 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4283 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4284 | { |
| 4285 | struct rbd_device *rbd_dev; |
| 4286 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4287 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4288 | if (!rbd_dev) |
| 4289 | return NULL; |
| 4290 | |
| 4291 | spin_lock_init(&rbd_dev->lock); |
| 4292 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4293 | init_rwsem(&rbd_dev->header_rwsem); |
| 4294 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4295 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4296 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4297 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4298 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4299 | mutex_init(&rbd_dev->watch_mutex); |
| 4300 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4301 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4302 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4303 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4304 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4305 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4306 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4307 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4308 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4309 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4310 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4311 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4312 | rbd_dev->dev.type = &rbd_device_type; |
| 4313 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4314 | device_initialize(&rbd_dev->dev); |
| 4315 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4316 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4317 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4318 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4319 | return rbd_dev; |
| 4320 | } |
| 4321 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4322 | /* |
| 4323 | * Create a mapping rbd_dev. |
| 4324 | */ |
| 4325 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4326 | struct rbd_spec *spec, |
| 4327 | struct rbd_options *opts) |
| 4328 | { |
| 4329 | struct rbd_device *rbd_dev; |
| 4330 | |
| 4331 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4332 | if (!rbd_dev) |
| 4333 | return NULL; |
| 4334 | |
| 4335 | rbd_dev->opts = opts; |
| 4336 | |
| 4337 | /* get an id and fill in device name */ |
| 4338 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4339 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4340 | GFP_KERNEL); |
| 4341 | if (rbd_dev->dev_id < 0) |
| 4342 | goto fail_rbd_dev; |
| 4343 | |
| 4344 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4345 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4346 | rbd_dev->name); |
| 4347 | if (!rbd_dev->task_wq) |
| 4348 | goto fail_dev_id; |
| 4349 | |
| 4350 | /* we have a ref from do_rbd_add() */ |
| 4351 | __module_get(THIS_MODULE); |
| 4352 | |
| 4353 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4354 | return rbd_dev; |
| 4355 | |
| 4356 | fail_dev_id: |
| 4357 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4358 | fail_rbd_dev: |
| 4359 | rbd_dev_free(rbd_dev); |
| 4360 | return NULL; |
| 4361 | } |
| 4362 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4363 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4364 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4365 | if (rbd_dev) |
| 4366 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4367 | } |
| 4368 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4369 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4370 | * Get the size and object order for an image snapshot, or if |
| 4371 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4372 | * image. |
| 4373 | */ |
| 4374 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4375 | u8 *order, u64 *snap_size) |
| 4376 | { |
| 4377 | __le64 snapid = cpu_to_le64(snap_id); |
| 4378 | int ret; |
| 4379 | struct { |
| 4380 | u8 order; |
| 4381 | __le64 size; |
| 4382 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4383 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4384 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4385 | &rbd_dev->header_oloc, "get_size", |
| 4386 | &snapid, sizeof(snapid), |
| 4387 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4388 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4389 | if (ret < 0) |
| 4390 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4391 | if (ret < sizeof (size_buf)) |
| 4392 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4393 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4394 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4395 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4396 | dout(" order %u", (unsigned int)*order); |
| 4397 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4398 | *snap_size = le64_to_cpu(size_buf.size); |
| 4399 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4400 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4401 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4402 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4403 | |
| 4404 | return 0; |
| 4405 | } |
| 4406 | |
| 4407 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4408 | { |
| 4409 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4410 | &rbd_dev->header.obj_order, |
| 4411 | &rbd_dev->header.image_size); |
| 4412 | } |
| 4413 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4414 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4415 | { |
| 4416 | void *reply_buf; |
| 4417 | int ret; |
| 4418 | void *p; |
| 4419 | |
| 4420 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4421 | if (!reply_buf) |
| 4422 | return -ENOMEM; |
| 4423 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4424 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4425 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4426 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4427 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4428 | if (ret < 0) |
| 4429 | goto out; |
| 4430 | |
| 4431 | p = reply_buf; |
| 4432 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4433 | p + ret, NULL, GFP_NOIO); |
| 4434 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4435 | |
| 4436 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4437 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4438 | rbd_dev->header.object_prefix = NULL; |
| 4439 | } else { |
| 4440 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4441 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4442 | out: |
| 4443 | kfree(reply_buf); |
| 4444 | |
| 4445 | return ret; |
| 4446 | } |
| 4447 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4448 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4449 | u64 *snap_features) |
| 4450 | { |
| 4451 | __le64 snapid = cpu_to_le64(snap_id); |
| 4452 | struct { |
| 4453 | __le64 features; |
| 4454 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4455 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4456 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4457 | int ret; |
| 4458 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4459 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4460 | &rbd_dev->header_oloc, "get_features", |
| 4461 | &snapid, sizeof(snapid), |
| 4462 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4463 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4464 | if (ret < 0) |
| 4465 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4466 | if (ret < sizeof (features_buf)) |
| 4467 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4468 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4469 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4470 | if (unsup) { |
| 4471 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4472 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4473 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4474 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4475 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4476 | *snap_features = le64_to_cpu(features_buf.features); |
| 4477 | |
| 4478 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4479 | (unsigned long long)snap_id, |
| 4480 | (unsigned long long)*snap_features, |
| 4481 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4482 | |
| 4483 | return 0; |
| 4484 | } |
| 4485 | |
| 4486 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4487 | { |
| 4488 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4489 | &rbd_dev->header.features); |
| 4490 | } |
| 4491 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4492 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4493 | { |
| 4494 | struct rbd_spec *parent_spec; |
| 4495 | size_t size; |
| 4496 | void *reply_buf = NULL; |
| 4497 | __le64 snapid; |
| 4498 | void *p; |
| 4499 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4500 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4501 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4502 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4503 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4504 | int ret; |
| 4505 | |
| 4506 | parent_spec = rbd_spec_alloc(); |
| 4507 | if (!parent_spec) |
| 4508 | return -ENOMEM; |
| 4509 | |
| 4510 | size = sizeof (__le64) + /* pool_id */ |
| 4511 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 4512 | sizeof (__le64) + /* snap_id */ |
| 4513 | sizeof (__le64); /* overlap */ |
| 4514 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4515 | if (!reply_buf) { |
| 4516 | ret = -ENOMEM; |
| 4517 | goto out_err; |
| 4518 | } |
| 4519 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4520 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4521 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4522 | &rbd_dev->header_oloc, "get_parent", |
| 4523 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4524 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4525 | if (ret < 0) |
| 4526 | goto out_err; |
| 4527 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4528 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4529 | end = reply_buf + ret; |
| 4530 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4531 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4532 | if (pool_id == CEPH_NOPOOL) { |
| 4533 | /* |
| 4534 | * Either the parent never existed, or we have |
| 4535 | * record of it but the image got flattened so it no |
| 4536 | * longer has a parent. When the parent of a |
| 4537 | * layered image disappears we immediately set the |
| 4538 | * overlap to 0. The effect of this is that all new |
| 4539 | * requests will be treated as if the image had no |
| 4540 | * parent. |
| 4541 | */ |
| 4542 | if (rbd_dev->parent_overlap) { |
| 4543 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4544 | rbd_dev_parent_put(rbd_dev); |
| 4545 | pr_info("%s: clone image has been flattened\n", |
| 4546 | rbd_dev->disk->disk_name); |
| 4547 | } |
| 4548 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4549 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4550 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4551 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4552 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4553 | |
| 4554 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4555 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4556 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4557 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4558 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4559 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4560 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4561 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4562 | if (IS_ERR(image_id)) { |
| 4563 | ret = PTR_ERR(image_id); |
| 4564 | goto out_err; |
| 4565 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4566 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4567 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 4568 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4569 | /* |
| 4570 | * The parent won't change (except when the clone is |
| 4571 | * flattened, already handled that). So we only need to |
| 4572 | * record the parent spec we have not already done so. |
| 4573 | */ |
| 4574 | if (!rbd_dev->parent_spec) { |
| 4575 | parent_spec->pool_id = pool_id; |
| 4576 | parent_spec->image_id = image_id; |
| 4577 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4578 | rbd_dev->parent_spec = parent_spec; |
| 4579 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 4580 | } else { |
| 4581 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4582 | } |
| 4583 | |
| 4584 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4585 | * We always update the parent overlap. If it's zero we issue |
| 4586 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4587 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4588 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4589 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4590 | /* refresh, careful to warn just once */ |
| 4591 | if (rbd_dev->parent_overlap) |
| 4592 | rbd_warn(rbd_dev, |
| 4593 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4594 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4595 | /* initial probe */ |
| 4596 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4597 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4598 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4599 | rbd_dev->parent_overlap = overlap; |
| 4600 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4601 | out: |
| 4602 | ret = 0; |
| 4603 | out_err: |
| 4604 | kfree(reply_buf); |
| 4605 | rbd_spec_put(parent_spec); |
| 4606 | |
| 4607 | return ret; |
| 4608 | } |
| 4609 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4610 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 4611 | { |
| 4612 | struct { |
| 4613 | __le64 stripe_unit; |
| 4614 | __le64 stripe_count; |
| 4615 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 4616 | size_t size = sizeof (striping_info_buf); |
| 4617 | void *p; |
| 4618 | u64 obj_size; |
| 4619 | u64 stripe_unit; |
| 4620 | u64 stripe_count; |
| 4621 | int ret; |
| 4622 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4623 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4624 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 4625 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4626 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 4627 | if (ret < 0) |
| 4628 | return ret; |
| 4629 | if (ret < size) |
| 4630 | return -ERANGE; |
| 4631 | |
| 4632 | /* |
| 4633 | * We don't actually support the "fancy striping" feature |
| 4634 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 4635 | * defaults the behavior is the same as before. So find |
| 4636 | * out, and only fail if the image has non-default values. |
| 4637 | */ |
| 4638 | ret = -EINVAL; |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4639 | obj_size = rbd_obj_bytes(&rbd_dev->header); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4640 | p = &striping_info_buf; |
| 4641 | stripe_unit = ceph_decode_64(&p); |
| 4642 | if (stripe_unit != obj_size) { |
| 4643 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 4644 | "(got %llu want %llu)", |
| 4645 | stripe_unit, obj_size); |
| 4646 | return -EINVAL; |
| 4647 | } |
| 4648 | stripe_count = ceph_decode_64(&p); |
| 4649 | if (stripe_count != 1) { |
| 4650 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 4651 | "(got %llu want 1)", stripe_count); |
| 4652 | return -EINVAL; |
| 4653 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4654 | rbd_dev->header.stripe_unit = stripe_unit; |
| 4655 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4656 | |
| 4657 | return 0; |
| 4658 | } |
| 4659 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4660 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 4661 | { |
| 4662 | __le64 data_pool_id; |
| 4663 | int ret; |
| 4664 | |
| 4665 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4666 | &rbd_dev->header_oloc, "get_data_pool", |
| 4667 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 4668 | if (ret < 0) |
| 4669 | return ret; |
| 4670 | if (ret < sizeof(data_pool_id)) |
| 4671 | return -EBADMSG; |
| 4672 | |
| 4673 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 4674 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 4675 | return 0; |
| 4676 | } |
| 4677 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4678 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 4679 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4680 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4681 | size_t image_id_size; |
| 4682 | char *image_id; |
| 4683 | void *p; |
| 4684 | void *end; |
| 4685 | size_t size; |
| 4686 | void *reply_buf = NULL; |
| 4687 | size_t len = 0; |
| 4688 | char *image_name = NULL; |
| 4689 | int ret; |
| 4690 | |
| 4691 | rbd_assert(!rbd_dev->spec->image_name); |
| 4692 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4693 | len = strlen(rbd_dev->spec->image_id); |
| 4694 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4695 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 4696 | if (!image_id) |
| 4697 | return NULL; |
| 4698 | |
| 4699 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4700 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4701 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4702 | |
| 4703 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 4704 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4705 | if (!reply_buf) |
| 4706 | goto out; |
| 4707 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4708 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 4709 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 4710 | "dir_get_name", image_id, image_id_size, |
| 4711 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4712 | if (ret < 0) |
| 4713 | goto out; |
| 4714 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4715 | end = reply_buf + ret; |
| 4716 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4717 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 4718 | if (IS_ERR(image_name)) |
| 4719 | image_name = NULL; |
| 4720 | else |
| 4721 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 4722 | out: |
| 4723 | kfree(reply_buf); |
| 4724 | kfree(image_id); |
| 4725 | |
| 4726 | return image_name; |
| 4727 | } |
| 4728 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4729 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4730 | { |
| 4731 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4732 | const char *snap_name; |
| 4733 | u32 which = 0; |
| 4734 | |
| 4735 | /* Skip over names until we find the one we are looking for */ |
| 4736 | |
| 4737 | snap_name = rbd_dev->header.snap_names; |
| 4738 | while (which < snapc->num_snaps) { |
| 4739 | if (!strcmp(name, snap_name)) |
| 4740 | return snapc->snaps[which]; |
| 4741 | snap_name += strlen(snap_name) + 1; |
| 4742 | which++; |
| 4743 | } |
| 4744 | return CEPH_NOSNAP; |
| 4745 | } |
| 4746 | |
| 4747 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4748 | { |
| 4749 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4750 | u32 which; |
| 4751 | bool found = false; |
| 4752 | u64 snap_id; |
| 4753 | |
| 4754 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 4755 | const char *snap_name; |
| 4756 | |
| 4757 | snap_id = snapc->snaps[which]; |
| 4758 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 4759 | if (IS_ERR(snap_name)) { |
| 4760 | /* ignore no-longer existing snapshots */ |
| 4761 | if (PTR_ERR(snap_name) == -ENOENT) |
| 4762 | continue; |
| 4763 | else |
| 4764 | break; |
| 4765 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4766 | found = !strcmp(name, snap_name); |
| 4767 | kfree(snap_name); |
| 4768 | } |
| 4769 | return found ? snap_id : CEPH_NOSNAP; |
| 4770 | } |
| 4771 | |
| 4772 | /* |
| 4773 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 4774 | * no snapshot by that name is found, or if an error occurs. |
| 4775 | */ |
| 4776 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4777 | { |
| 4778 | if (rbd_dev->image_format == 1) |
| 4779 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 4780 | |
| 4781 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 4782 | } |
| 4783 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4784 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4785 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4786 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4787 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 4788 | { |
| 4789 | struct rbd_spec *spec = rbd_dev->spec; |
| 4790 | |
| 4791 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 4792 | rbd_assert(spec->image_id && spec->image_name); |
| 4793 | rbd_assert(spec->snap_name); |
| 4794 | |
| 4795 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 4796 | u64 snap_id; |
| 4797 | |
| 4798 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 4799 | if (snap_id == CEPH_NOSNAP) |
| 4800 | return -ENOENT; |
| 4801 | |
| 4802 | spec->snap_id = snap_id; |
| 4803 | } else { |
| 4804 | spec->snap_id = CEPH_NOSNAP; |
| 4805 | } |
| 4806 | |
| 4807 | return 0; |
| 4808 | } |
| 4809 | |
| 4810 | /* |
| 4811 | * A parent image will have all ids but none of the names. |
| 4812 | * |
| 4813 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 4814 | * can't figure out the name for an image id. |
| 4815 | */ |
| 4816 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4817 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4818 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4819 | struct rbd_spec *spec = rbd_dev->spec; |
| 4820 | const char *pool_name; |
| 4821 | const char *image_name; |
| 4822 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4823 | int ret; |
| 4824 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4825 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 4826 | rbd_assert(spec->image_id); |
| 4827 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4828 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4829 | /* 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] | 4830 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4831 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 4832 | if (!pool_name) { |
| 4833 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4834 | return -EIO; |
| 4835 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4836 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 4837 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4838 | return -ENOMEM; |
| 4839 | |
| 4840 | /* Fetch the image name; tolerate failure here */ |
| 4841 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4842 | image_name = rbd_dev_image_name(rbd_dev); |
| 4843 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4844 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4845 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4846 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4847 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4848 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 4849 | if (IS_ERR(snap_name)) { |
| 4850 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4851 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4852 | } |
| 4853 | |
| 4854 | spec->pool_name = pool_name; |
| 4855 | spec->image_name = image_name; |
| 4856 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4857 | |
| 4858 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4859 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4860 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4861 | kfree(image_name); |
| 4862 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4863 | return ret; |
| 4864 | } |
| 4865 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4866 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4867 | { |
| 4868 | size_t size; |
| 4869 | int ret; |
| 4870 | void *reply_buf; |
| 4871 | void *p; |
| 4872 | void *end; |
| 4873 | u64 seq; |
| 4874 | u32 snap_count; |
| 4875 | struct ceph_snap_context *snapc; |
| 4876 | u32 i; |
| 4877 | |
| 4878 | /* |
| 4879 | * We'll need room for the seq value (maximum snapshot id), |
| 4880 | * snapshot count, and array of that many snapshot ids. |
| 4881 | * For now we have a fixed upper limit on the number we're |
| 4882 | * prepared to receive. |
| 4883 | */ |
| 4884 | size = sizeof (__le64) + sizeof (__le32) + |
| 4885 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 4886 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 4887 | if (!reply_buf) |
| 4888 | return -ENOMEM; |
| 4889 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4890 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4891 | &rbd_dev->header_oloc, "get_snapcontext", |
| 4892 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4893 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4894 | if (ret < 0) |
| 4895 | goto out; |
| 4896 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4897 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4898 | end = reply_buf + ret; |
| 4899 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4900 | ceph_decode_64_safe(&p, end, seq, out); |
| 4901 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 4902 | |
| 4903 | /* |
| 4904 | * Make sure the reported number of snapshot ids wouldn't go |
| 4905 | * beyond the end of our buffer. But before checking that, |
| 4906 | * make sure the computed size of the snapshot context we |
| 4907 | * allocate is representable in a size_t. |
| 4908 | */ |
| 4909 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 4910 | / sizeof (u64)) { |
| 4911 | ret = -EINVAL; |
| 4912 | goto out; |
| 4913 | } |
| 4914 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 4915 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4916 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4917 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 4918 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4919 | if (!snapc) { |
| 4920 | ret = -ENOMEM; |
| 4921 | goto out; |
| 4922 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4923 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4924 | for (i = 0; i < snap_count; i++) |
| 4925 | snapc->snaps[i] = ceph_decode_64(&p); |
| 4926 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 4927 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4928 | rbd_dev->header.snapc = snapc; |
| 4929 | |
| 4930 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4931 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4932 | out: |
| 4933 | kfree(reply_buf); |
| 4934 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4935 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4936 | } |
| 4937 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4938 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 4939 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4940 | { |
| 4941 | size_t size; |
| 4942 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4943 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4944 | int ret; |
| 4945 | void *p; |
| 4946 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4947 | char *snap_name; |
| 4948 | |
| 4949 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 4950 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4951 | if (!reply_buf) |
| 4952 | return ERR_PTR(-ENOMEM); |
| 4953 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4954 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4955 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4956 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 4957 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4958 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4959 | if (ret < 0) { |
| 4960 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4961 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4962 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4963 | |
| 4964 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4965 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4966 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4967 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4968 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4969 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4970 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4971 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4972 | out: |
| 4973 | kfree(reply_buf); |
| 4974 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4975 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4976 | } |
| 4977 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4978 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4979 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4980 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4981 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4982 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 4983 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 4984 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4985 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 4986 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4987 | if (first_time) { |
| 4988 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 4989 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4990 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4991 | } |
| 4992 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4993 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 4994 | if (ret && first_time) { |
| 4995 | kfree(rbd_dev->header.object_prefix); |
| 4996 | rbd_dev->header.object_prefix = NULL; |
| 4997 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 4998 | |
| 4999 | return ret; |
| 5000 | } |
| 5001 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5002 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5003 | { |
| 5004 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5005 | |
| 5006 | if (rbd_dev->image_format == 1) |
| 5007 | return rbd_dev_v1_header_info(rbd_dev); |
| 5008 | |
| 5009 | return rbd_dev_v2_header_info(rbd_dev); |
| 5010 | } |
| 5011 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5012 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5013 | * Skips over white space at *buf, and updates *buf to point to the |
| 5014 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5015 | * the token (string of non-white space characters) found. Note |
| 5016 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5017 | */ |
| 5018 | static inline size_t next_token(const char **buf) |
| 5019 | { |
| 5020 | /* |
| 5021 | * These are the characters that produce nonzero for |
| 5022 | * isspace() in the "C" and "POSIX" locales. |
| 5023 | */ |
| 5024 | const char *spaces = " \f\n\r\t\v"; |
| 5025 | |
| 5026 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5027 | |
| 5028 | return strcspn(*buf, spaces); /* Return token length */ |
| 5029 | } |
| 5030 | |
| 5031 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5032 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5033 | * enough to hold a copy of it, and copies the token into the new |
| 5034 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5035 | * that a duplicate buffer is created even for a zero-length token. |
| 5036 | * |
| 5037 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5038 | * pointer if memory for the duplicate was not available. If |
| 5039 | * the lenp argument is a non-null pointer, the length of the token |
| 5040 | * (not including the '\0') is returned in *lenp. |
| 5041 | * |
| 5042 | * If successful, the *buf pointer will be updated to point beyond |
| 5043 | * the end of the found token. |
| 5044 | * |
| 5045 | * Note: uses GFP_KERNEL for allocation. |
| 5046 | */ |
| 5047 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5048 | { |
| 5049 | char *dup; |
| 5050 | size_t len; |
| 5051 | |
| 5052 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5053 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5054 | if (!dup) |
| 5055 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5056 | *(dup + len) = '\0'; |
| 5057 | *buf += len; |
| 5058 | |
| 5059 | if (lenp) |
| 5060 | *lenp = len; |
| 5061 | |
| 5062 | return dup; |
| 5063 | } |
| 5064 | |
| 5065 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5066 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5067 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5068 | * and the data written is passed here via a NUL-terminated buffer. |
| 5069 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5070 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5071 | * The information extracted from these options is recorded in |
| 5072 | * the other parameters which return dynamically-allocated |
| 5073 | * structures: |
| 5074 | * ceph_opts |
| 5075 | * The address of a pointer that will refer to a ceph options |
| 5076 | * structure. Caller must release the returned pointer using |
| 5077 | * ceph_destroy_options() when it is no longer needed. |
| 5078 | * rbd_opts |
| 5079 | * Address of an rbd options pointer. Fully initialized by |
| 5080 | * this function; caller must release with kfree(). |
| 5081 | * spec |
| 5082 | * Address of an rbd image specification pointer. Fully |
| 5083 | * initialized by this function based on parsed options. |
| 5084 | * Caller must release with rbd_spec_put(). |
| 5085 | * |
| 5086 | * The options passed take this form: |
| 5087 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5088 | * where: |
| 5089 | * <mon_addrs> |
| 5090 | * A comma-separated list of one or more monitor addresses. |
| 5091 | * A monitor address is an ip address, optionally followed |
| 5092 | * by a port number (separated by a colon). |
| 5093 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5094 | * <options> |
| 5095 | * A comma-separated list of ceph and/or rbd options. |
| 5096 | * <pool_name> |
| 5097 | * The name of the rados pool containing the rbd image. |
| 5098 | * <image_name> |
| 5099 | * The name of the image in that pool to map. |
| 5100 | * <snap_id> |
| 5101 | * An optional snapshot id. If provided, the mapping will |
| 5102 | * present data from the image at the time that snapshot was |
| 5103 | * created. The image head is used if no snapshot id is |
| 5104 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5105 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5106 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5107 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5108 | struct rbd_options **opts, |
| 5109 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5110 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5111 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5112 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5113 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5114 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5115 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5116 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5117 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5118 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5119 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5120 | |
| 5121 | /* The first four tokens are required */ |
| 5122 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5123 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5124 | if (!len) { |
| 5125 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5126 | return -EINVAL; |
| 5127 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5128 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5129 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5130 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5131 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5132 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5133 | options = dup_token(&buf, NULL); |
| 5134 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5135 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5136 | if (!*options) { |
| 5137 | rbd_warn(NULL, "no options provided"); |
| 5138 | goto out_err; |
| 5139 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5140 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5141 | spec = rbd_spec_alloc(); |
| 5142 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5143 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5144 | |
| 5145 | spec->pool_name = dup_token(&buf, NULL); |
| 5146 | if (!spec->pool_name) |
| 5147 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5148 | if (!*spec->pool_name) { |
| 5149 | rbd_warn(NULL, "no pool name provided"); |
| 5150 | goto out_err; |
| 5151 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5152 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5153 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5154 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5155 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5156 | if (!*spec->image_name) { |
| 5157 | rbd_warn(NULL, "no image name provided"); |
| 5158 | goto out_err; |
| 5159 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5160 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5161 | /* |
| 5162 | * Snapshot name is optional; default is to use "-" |
| 5163 | * (indicating the head/no snapshot). |
| 5164 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5165 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5166 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5167 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5168 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5169 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5170 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5171 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5172 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5173 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5174 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5175 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5176 | *(snap_name + len) = '\0'; |
| 5177 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5178 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5179 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5180 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5181 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5182 | if (!rbd_opts) |
| 5183 | goto out_mem; |
| 5184 | |
| 5185 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5186 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5187 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5188 | rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5189 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5190 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5191 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5192 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5193 | if (IS_ERR(copts)) { |
| 5194 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5195 | goto out_err; |
| 5196 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5197 | kfree(options); |
| 5198 | |
| 5199 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5200 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5201 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5202 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5203 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5204 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5205 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5206 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5207 | kfree(rbd_opts); |
| 5208 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5209 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5210 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5211 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5212 | } |
| 5213 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5214 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5215 | * Return pool id (>= 0) or a negative error code. |
| 5216 | */ |
| 5217 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5218 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5219 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5220 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5221 | int tries = 0; |
| 5222 | int ret; |
| 5223 | |
| 5224 | again: |
| 5225 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5226 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5227 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5228 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5229 | if (ret < 0) |
| 5230 | return ret; |
| 5231 | |
| 5232 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5233 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5234 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5235 | newest_epoch, |
| 5236 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5237 | goto again; |
| 5238 | } else { |
| 5239 | /* the osdmap we have is new enough */ |
| 5240 | return -ENOENT; |
| 5241 | } |
| 5242 | } |
| 5243 | |
| 5244 | return ret; |
| 5245 | } |
| 5246 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5247 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5248 | { |
| 5249 | down_write(&rbd_dev->lock_rwsem); |
| 5250 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5251 | rbd_unlock(rbd_dev); |
| 5252 | up_write(&rbd_dev->lock_rwsem); |
| 5253 | } |
| 5254 | |
| 5255 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5256 | { |
| 5257 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5258 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5259 | return -EINVAL; |
| 5260 | } |
| 5261 | |
| 5262 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5263 | down_read(&rbd_dev->lock_rwsem); |
| 5264 | rbd_wait_state_locked(rbd_dev); |
| 5265 | up_read(&rbd_dev->lock_rwsem); |
| 5266 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 5267 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5268 | return -EROFS; |
| 5269 | } |
| 5270 | |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5274 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5275 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5276 | * name given to it by the user. Internally, that identifier is |
| 5277 | * what's used to specify the names of objects related to the image. |
| 5278 | * |
| 5279 | * A special "rbd id" object is used to map an rbd image name to its |
| 5280 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5281 | * with the supplied name. |
| 5282 | * |
| 5283 | * This function will record the given rbd_dev's image_id field if |
| 5284 | * it can be determined, and in that case will return 0. If any |
| 5285 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5286 | * image_id field will be unchanged (and should be NULL). |
| 5287 | */ |
| 5288 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5289 | { |
| 5290 | int ret; |
| 5291 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5292 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5293 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5294 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5295 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5296 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5297 | * When probing a parent image, the image id is already |
| 5298 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5299 | * need to fetch the image id again in this case. We |
| 5300 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5301 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5302 | if (rbd_dev->spec->image_id) { |
| 5303 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5304 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5305 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5306 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5307 | |
| 5308 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5309 | * First, see if the format 2 image id file exists, and if |
| 5310 | * so, get the image's persistent id from it. |
| 5311 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5312 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5313 | rbd_dev->spec->image_name); |
| 5314 | if (ret) |
| 5315 | return ret; |
| 5316 | |
| 5317 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5318 | |
| 5319 | /* Response will be an encoded string, which includes a length */ |
| 5320 | |
| 5321 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5322 | response = kzalloc(size, GFP_NOIO); |
| 5323 | if (!response) { |
| 5324 | ret = -ENOMEM; |
| 5325 | goto out; |
| 5326 | } |
| 5327 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5328 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5329 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5330 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5331 | "get_id", NULL, 0, |
| 5332 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5333 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5334 | if (ret == -ENOENT) { |
| 5335 | image_id = kstrdup("", GFP_KERNEL); |
| 5336 | ret = image_id ? 0 : -ENOMEM; |
| 5337 | if (!ret) |
| 5338 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5339 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5340 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5341 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5342 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5343 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5344 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5345 | if (!ret) |
| 5346 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5347 | } |
| 5348 | |
| 5349 | if (!ret) { |
| 5350 | rbd_dev->spec->image_id = image_id; |
| 5351 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5352 | } |
| 5353 | out: |
| 5354 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5355 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5356 | return ret; |
| 5357 | } |
| 5358 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5359 | /* |
| 5360 | * Undo whatever state changes are made by v1 or v2 header info |
| 5361 | * call. |
| 5362 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5363 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5364 | { |
| 5365 | struct rbd_image_header *header; |
| 5366 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5367 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5368 | |
| 5369 | /* Free dynamic fields from the header, then zero it out */ |
| 5370 | |
| 5371 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5372 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5373 | kfree(header->snap_sizes); |
| 5374 | kfree(header->snap_names); |
| 5375 | kfree(header->object_prefix); |
| 5376 | memset(header, 0, sizeof (*header)); |
| 5377 | } |
| 5378 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5379 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5380 | { |
| 5381 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5382 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5383 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5384 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5385 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5386 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5387 | /* |
| 5388 | * Get the and check features for the image. Currently the |
| 5389 | * features are assumed to never change. |
| 5390 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5391 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5392 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5393 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5394 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5395 | /* If the image supports fancy striping, get its parameters */ |
| 5396 | |
| 5397 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5398 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5399 | if (ret < 0) |
| 5400 | goto out_err; |
| 5401 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5402 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5403 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5404 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5405 | if (ret) |
| 5406 | goto out_err; |
| 5407 | } |
| 5408 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5409 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5410 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5411 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5412 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5413 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5414 | kfree(rbd_dev->header.object_prefix); |
| 5415 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5416 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5417 | } |
| 5418 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5419 | /* |
| 5420 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5421 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5422 | * length of the already discovered part of the parent chain. |
| 5423 | */ |
| 5424 | 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] | 5425 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5426 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5427 | int ret; |
| 5428 | |
| 5429 | if (!rbd_dev->parent_spec) |
| 5430 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5431 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5432 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5433 | pr_info("parent chain is too long (%d)\n", depth); |
| 5434 | ret = -EINVAL; |
| 5435 | goto out_err; |
| 5436 | } |
| 5437 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5438 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5439 | if (!parent) { |
| 5440 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5441 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | /* |
| 5445 | * Images related by parent/child relationships always share |
| 5446 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5447 | */ |
| 5448 | __rbd_get_client(rbd_dev->rbd_client); |
| 5449 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5450 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5451 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5452 | if (ret < 0) |
| 5453 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5454 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5455 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5456 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5457 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5458 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5459 | out_err: |
| 5460 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5461 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5462 | return ret; |
| 5463 | } |
| 5464 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5465 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5466 | { |
| 5467 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5468 | rbd_dev_mapping_clear(rbd_dev); |
| 5469 | rbd_free_disk(rbd_dev); |
| 5470 | if (!single_major) |
| 5471 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5472 | } |
| 5473 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5474 | /* |
| 5475 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5476 | * upon return. |
| 5477 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5478 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5479 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5480 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5481 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5482 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5483 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5484 | if (!single_major) { |
| 5485 | ret = register_blkdev(0, rbd_dev->name); |
| 5486 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5487 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5488 | |
| 5489 | rbd_dev->major = ret; |
| 5490 | rbd_dev->minor = 0; |
| 5491 | } else { |
| 5492 | rbd_dev->major = rbd_major; |
| 5493 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5494 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5495 | |
| 5496 | /* Set up the blkdev mapping. */ |
| 5497 | |
| 5498 | ret = rbd_init_disk(rbd_dev); |
| 5499 | if (ret) |
| 5500 | goto err_out_blkdev; |
| 5501 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5502 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5503 | if (ret) |
| 5504 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5505 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5506 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5507 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5508 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5509 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5510 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5511 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5512 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5513 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5514 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5515 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5516 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5517 | err_out_mapping: |
| 5518 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5519 | err_out_disk: |
| 5520 | rbd_free_disk(rbd_dev); |
| 5521 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5522 | if (!single_major) |
| 5523 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5524 | err_out_unlock: |
| 5525 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5526 | return ret; |
| 5527 | } |
| 5528 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5529 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5530 | { |
| 5531 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5532 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5533 | |
| 5534 | /* Record the header object name for this rbd image. */ |
| 5535 | |
| 5536 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5537 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5538 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5539 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5540 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5541 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5542 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5543 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5544 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5545 | } |
| 5546 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5547 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5548 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5549 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5550 | if (rbd_dev->opts) |
| 5551 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5552 | rbd_dev->image_format = 0; |
| 5553 | kfree(rbd_dev->spec->image_id); |
| 5554 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5555 | } |
| 5556 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5557 | /* |
| 5558 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5559 | * device. If this image is the one being mapped (i.e., not a |
| 5560 | * parent), initiate a watch on its header object before using that |
| 5561 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5562 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5563 | 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] | 5564 | { |
| 5565 | int ret; |
| 5566 | |
| 5567 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5568 | * Get the id from the image id object. Unless there's an |
| 5569 | * error, rbd_dev->spec->image_id will be filled in with |
| 5570 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5571 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5572 | */ |
| 5573 | ret = rbd_dev_image_id(rbd_dev); |
| 5574 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5575 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5576 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5577 | ret = rbd_dev_header_name(rbd_dev); |
| 5578 | if (ret) |
| 5579 | goto err_out_format; |
| 5580 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5581 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5582 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5583 | if (ret) { |
| 5584 | if (ret == -ENOENT) |
| 5585 | pr_info("image %s/%s does not exist\n", |
| 5586 | rbd_dev->spec->pool_name, |
| 5587 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5588 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5589 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5590 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5591 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5592 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5593 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5594 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5595 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5596 | /* |
| 5597 | * If this image is the one being mapped, we have pool name and |
| 5598 | * id, image name and id, and snap name - need to fill snap id. |
| 5599 | * Otherwise this is a parent image, identified by pool, image |
| 5600 | * and snap ids - need to fill in names for those ids. |
| 5601 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5602 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5603 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5604 | else |
| 5605 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5606 | if (ret) { |
| 5607 | if (ret == -ENOENT) |
| 5608 | pr_info("snap %s/%s@%s does not exist\n", |
| 5609 | rbd_dev->spec->pool_name, |
| 5610 | rbd_dev->spec->image_name, |
| 5611 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5612 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5613 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5614 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5615 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5616 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5617 | if (ret) |
| 5618 | goto err_out_probe; |
| 5619 | |
| 5620 | /* |
| 5621 | * Need to warn users if this image is the one being |
| 5622 | * mapped and has a parent. |
| 5623 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5624 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5625 | rbd_warn(rbd_dev, |
| 5626 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 5627 | } |
| 5628 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5629 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5630 | if (ret) |
| 5631 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5632 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5633 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5634 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5635 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5636 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5637 | err_out_probe: |
| 5638 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5639 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5640 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5641 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5642 | err_out_format: |
| 5643 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5644 | kfree(rbd_dev->spec->image_id); |
| 5645 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5646 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5647 | } |
| 5648 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5649 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 5650 | const char *buf, |
| 5651 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5652 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5653 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5654 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5655 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5656 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5657 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5658 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5659 | |
| 5660 | if (!try_module_get(THIS_MODULE)) |
| 5661 | return -ENODEV; |
| 5662 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5663 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5664 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5665 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5666 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5667 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5668 | rbdc = rbd_get_client(ceph_opts); |
| 5669 | if (IS_ERR(rbdc)) { |
| 5670 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5671 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5672 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5673 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5674 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5675 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5676 | if (rc < 0) { |
| 5677 | if (rc == -ENOENT) |
| 5678 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5679 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5680 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5681 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5682 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5683 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5684 | if (!rbd_dev) { |
| 5685 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5686 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5687 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5688 | rbdc = NULL; /* rbd_dev now owns this */ |
| 5689 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5690 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5691 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5692 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 5693 | if (!rbd_dev->config_info) { |
| 5694 | rc = -ENOMEM; |
| 5695 | goto err_out_rbd_dev; |
| 5696 | } |
| 5697 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5698 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5699 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5700 | if (rc < 0) { |
| 5701 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5702 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5703 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 5704 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5705 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5706 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5707 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5708 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5709 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5710 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5711 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5712 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5713 | if (rbd_dev->opts->exclusive) { |
| 5714 | rc = rbd_add_acquire_lock(rbd_dev); |
| 5715 | if (rc) |
| 5716 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5717 | } |
| 5718 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5719 | /* Everything's ready. Announce the disk to the world. */ |
| 5720 | |
| 5721 | rc = device_add(&rbd_dev->dev); |
| 5722 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5723 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5724 | |
| 5725 | add_disk(rbd_dev->disk); |
| 5726 | /* see rbd_init_disk() */ |
| 5727 | blk_put_queue(rbd_dev->disk->queue); |
| 5728 | |
| 5729 | spin_lock(&rbd_dev_list_lock); |
| 5730 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5731 | spin_unlock(&rbd_dev_list_lock); |
| 5732 | |
| 5733 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5734 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5735 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5736 | rc = count; |
| 5737 | out: |
| 5738 | module_put(THIS_MODULE); |
| 5739 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5740 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5741 | err_out_image_lock: |
| 5742 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5743 | err_out_device_setup: |
| 5744 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5745 | err_out_image_probe: |
| 5746 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5747 | err_out_rbd_dev: |
| 5748 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5749 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5750 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5751 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5752 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5753 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5754 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5755 | } |
| 5756 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5757 | static ssize_t rbd_add(struct bus_type *bus, |
| 5758 | const char *buf, |
| 5759 | size_t count) |
| 5760 | { |
| 5761 | if (single_major) |
| 5762 | return -EINVAL; |
| 5763 | |
| 5764 | return do_rbd_add(bus, buf, count); |
| 5765 | } |
| 5766 | |
| 5767 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 5768 | const char *buf, |
| 5769 | size_t count) |
| 5770 | { |
| 5771 | return do_rbd_add(bus, buf, count); |
| 5772 | } |
| 5773 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5774 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 5775 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5776 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5777 | struct rbd_device *first = rbd_dev; |
| 5778 | struct rbd_device *second = first->parent; |
| 5779 | struct rbd_device *third; |
| 5780 | |
| 5781 | /* |
| 5782 | * Follow to the parent with no grandparent and |
| 5783 | * remove it. |
| 5784 | */ |
| 5785 | while (second && (third = second->parent)) { |
| 5786 | first = second; |
| 5787 | second = third; |
| 5788 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5789 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5790 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5791 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5792 | first->parent = NULL; |
| 5793 | first->parent_overlap = 0; |
| 5794 | |
| 5795 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5796 | rbd_spec_put(first->parent_spec); |
| 5797 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5798 | } |
| 5799 | } |
| 5800 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5801 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 5802 | const char *buf, |
| 5803 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5804 | { |
| 5805 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5806 | struct list_head *tmp; |
| 5807 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5808 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5809 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5810 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5811 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5812 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5813 | dev_id = -1; |
| 5814 | opt_buf[0] = '\0'; |
| 5815 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 5816 | if (dev_id < 0) { |
| 5817 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5818 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5819 | } |
| 5820 | if (opt_buf[0] != '\0') { |
| 5821 | if (!strcmp(opt_buf, "force")) { |
| 5822 | force = true; |
| 5823 | } else { |
| 5824 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 5825 | return -EINVAL; |
| 5826 | } |
| 5827 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5828 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5829 | ret = -ENOENT; |
| 5830 | spin_lock(&rbd_dev_list_lock); |
| 5831 | list_for_each(tmp, &rbd_dev_list) { |
| 5832 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 5833 | if (rbd_dev->dev_id == dev_id) { |
| 5834 | ret = 0; |
| 5835 | break; |
| 5836 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5837 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5838 | if (!ret) { |
| 5839 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5840 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5841 | ret = -EBUSY; |
| 5842 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5843 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 5844 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5845 | spin_unlock_irq(&rbd_dev->lock); |
| 5846 | } |
| 5847 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5848 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5849 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5850 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5851 | if (force) { |
| 5852 | /* |
| 5853 | * Prevent new IO from being queued and wait for existing |
| 5854 | * IO to complete/fail. |
| 5855 | */ |
| 5856 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 5857 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 5858 | } |
| 5859 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5860 | del_gendisk(rbd_dev->disk); |
| 5861 | spin_lock(&rbd_dev_list_lock); |
| 5862 | list_del_init(&rbd_dev->node); |
| 5863 | spin_unlock(&rbd_dev_list_lock); |
| 5864 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 5865 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5866 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5867 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5868 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5869 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5870 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5871 | } |
| 5872 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5873 | static ssize_t rbd_remove(struct bus_type *bus, |
| 5874 | const char *buf, |
| 5875 | size_t count) |
| 5876 | { |
| 5877 | if (single_major) |
| 5878 | return -EINVAL; |
| 5879 | |
| 5880 | return do_rbd_remove(bus, buf, count); |
| 5881 | } |
| 5882 | |
| 5883 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 5884 | const char *buf, |
| 5885 | size_t count) |
| 5886 | { |
| 5887 | return do_rbd_remove(bus, buf, count); |
| 5888 | } |
| 5889 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5890 | /* |
| 5891 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5892 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5893 | */ |
| 5894 | static int rbd_sysfs_init(void) |
| 5895 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5896 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5897 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5898 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 5899 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5900 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5901 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5902 | ret = bus_register(&rbd_bus_type); |
| 5903 | if (ret < 0) |
| 5904 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5905 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5906 | return ret; |
| 5907 | } |
| 5908 | |
| 5909 | static void rbd_sysfs_cleanup(void) |
| 5910 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5911 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5912 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5915 | static int rbd_slab_init(void) |
| 5916 | { |
| 5917 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5918 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5919 | if (!rbd_img_request_cache) |
| 5920 | return -ENOMEM; |
| 5921 | |
| 5922 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 5923 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 5924 | if (!rbd_obj_request_cache) |
| 5925 | goto out_err; |
| 5926 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5927 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5928 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 5929 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5930 | kmem_cache_destroy(rbd_img_request_cache); |
| 5931 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5932 | return -ENOMEM; |
| 5933 | } |
| 5934 | |
| 5935 | static void rbd_slab_exit(void) |
| 5936 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5937 | rbd_assert(rbd_obj_request_cache); |
| 5938 | kmem_cache_destroy(rbd_obj_request_cache); |
| 5939 | rbd_obj_request_cache = NULL; |
| 5940 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5941 | rbd_assert(rbd_img_request_cache); |
| 5942 | kmem_cache_destroy(rbd_img_request_cache); |
| 5943 | rbd_img_request_cache = NULL; |
| 5944 | } |
| 5945 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 5946 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5947 | { |
| 5948 | int rc; |
| 5949 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 5950 | if (!libceph_compatible(NULL)) { |
| 5951 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 5952 | return -EINVAL; |
| 5953 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5954 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5955 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5956 | if (rc) |
| 5957 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5958 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5959 | /* |
| 5960 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 5961 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5962 | */ |
| 5963 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 5964 | if (!rbd_wq) { |
| 5965 | rc = -ENOMEM; |
| 5966 | goto err_out_slab; |
| 5967 | } |
| 5968 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5969 | if (single_major) { |
| 5970 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 5971 | if (rbd_major < 0) { |
| 5972 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5973 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5974 | } |
| 5975 | } |
| 5976 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5977 | rc = rbd_sysfs_init(); |
| 5978 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5979 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5980 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5981 | if (single_major) |
| 5982 | pr_info("loaded (major %d)\n", rbd_major); |
| 5983 | else |
| 5984 | pr_info("loaded\n"); |
| 5985 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5986 | return 0; |
| 5987 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5988 | err_out_blkdev: |
| 5989 | if (single_major) |
| 5990 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5991 | err_out_wq: |
| 5992 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5993 | err_out_slab: |
| 5994 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 5995 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5996 | } |
| 5997 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 5998 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5999 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6000 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6001 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6002 | if (single_major) |
| 6003 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6004 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6005 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | module_init(rbd_init); |
| 6009 | module_exit(rbd_exit); |
| 6010 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6011 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6012 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6013 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6014 | /* following authorship retained from original osdblk.c */ |
| 6015 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6016 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6017 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6018 | MODULE_LICENSE("GPL"); |