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 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 55 | * Increment the given counter and return its updated value. |
| 56 | * If the counter is already 0 it will not be incremented. |
| 57 | * If the counter is already at its maximum value returns |
| 58 | * -EINVAL without updating it. |
| 59 | */ |
| 60 | static int atomic_inc_return_safe(atomic_t *v) |
| 61 | { |
| 62 | unsigned int counter; |
| 63 | |
Mark Rutland | bfc18e3 | 2018-06-21 13:13:04 +0100 | [diff] [blame] | 64 | counter = (unsigned int)atomic_fetch_add_unless(v, 1, 0); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 65 | if (counter <= (unsigned int)INT_MAX) |
| 66 | return (int)counter; |
| 67 | |
| 68 | atomic_dec(v); |
| 69 | |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 74 | static int atomic_dec_return_safe(atomic_t *v) |
| 75 | { |
| 76 | int counter; |
| 77 | |
| 78 | counter = atomic_dec_return(v); |
| 79 | if (counter >= 0) |
| 80 | return counter; |
| 81 | |
| 82 | atomic_inc(v); |
| 83 | |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 87 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 88 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 89 | #define RBD_MINORS_PER_MAJOR 256 |
| 90 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 91 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 92 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 93 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 94 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 95 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 96 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 97 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 98 | #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] | 99 | |
| 100 | #define RBD_SNAP_HEAD_NAME "-" |
| 101 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 102 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 103 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 104 | /* This allows a single page to hold an image name sent by OSD */ |
| 105 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 106 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 107 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 108 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 109 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 110 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 111 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 112 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 113 | /* Feature bits */ |
| 114 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 115 | #define RBD_FEATURE_LAYERING (1ULL<<0) |
| 116 | #define RBD_FEATURE_STRIPINGV2 (1ULL<<1) |
| 117 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2) |
| 118 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 119 | #define RBD_FEATURE_OPERATIONS (1ULL<<8) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 120 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 121 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 122 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 123 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 124 | RBD_FEATURE_DATA_POOL | \ |
| 125 | RBD_FEATURE_OPERATIONS) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 126 | |
| 127 | /* Features supported by this (client software) implementation. */ |
| 128 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 129 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 130 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 131 | /* |
| 132 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 133 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 134 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 135 | #define DEV_NAME_LEN 32 |
| 136 | |
| 137 | /* |
| 138 | * block device image metadata (in-memory version) |
| 139 | */ |
| 140 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 141 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 142 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 143 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 144 | u64 stripe_unit; |
| 145 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 146 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 147 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 148 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 149 | /* The remaining fields need to be updated occasionally */ |
| 150 | u64 image_size; |
| 151 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 152 | char *snap_names; /* format 1 only */ |
| 153 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 156 | /* |
| 157 | * An rbd image specification. |
| 158 | * |
| 159 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 160 | * identify an image. Each rbd_dev structure includes a pointer to |
| 161 | * an rbd_spec structure that encapsulates this identity. |
| 162 | * |
| 163 | * Each of the id's in an rbd_spec has an associated name. For a |
| 164 | * user-mapped image, the names are supplied and the id's associated |
| 165 | * with them are looked up. For a layered image, a parent image is |
| 166 | * defined by the tuple, and the names are looked up. |
| 167 | * |
| 168 | * An rbd_dev structure contains a parent_spec pointer which is |
| 169 | * non-null if the image it represents is a child in a layered |
| 170 | * image. This pointer will refer to the rbd_spec structure used |
| 171 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 172 | * is shared between the parent and child). |
| 173 | * |
| 174 | * Since these structures are populated once, during the discovery |
| 175 | * phase of image construction, they are effectively immutable so |
| 176 | * we make no effort to synchronize access to them. |
| 177 | * |
| 178 | * Note that code herein does not assume the image name is known (it |
| 179 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 180 | */ |
| 181 | struct rbd_spec { |
| 182 | u64 pool_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 183 | const char *pool_name; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 184 | const char *pool_ns; /* NULL if default, never "" */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 185 | |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 186 | const char *image_id; |
| 187 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 188 | |
| 189 | u64 snap_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 190 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 191 | |
| 192 | struct kref kref; |
| 193 | }; |
| 194 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 195 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 196 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 197 | */ |
| 198 | struct rbd_client { |
| 199 | struct ceph_client *client; |
| 200 | struct kref kref; |
| 201 | struct list_head node; |
| 202 | }; |
| 203 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 204 | struct rbd_img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 205 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 206 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 207 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 208 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 209 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 210 | OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */ |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 211 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 212 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 213 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 214 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 215 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 216 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 217 | }; |
| 218 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 219 | /* |
| 220 | * Writes go through the following state machine to deal with |
| 221 | * layering: |
| 222 | * |
| 223 | * need copyup |
| 224 | * RBD_OBJ_WRITE_GUARD ---------------> RBD_OBJ_WRITE_COPYUP |
| 225 | * | ^ | |
| 226 | * v \------------------------------/ |
| 227 | * done |
| 228 | * ^ |
| 229 | * | |
| 230 | * RBD_OBJ_WRITE_FLAT |
| 231 | * |
| 232 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
| 233 | * there is a parent or not. |
| 234 | */ |
| 235 | enum rbd_obj_write_state { |
| 236 | RBD_OBJ_WRITE_FLAT = 1, |
| 237 | RBD_OBJ_WRITE_GUARD, |
| 238 | RBD_OBJ_WRITE_COPYUP, |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 239 | }; |
| 240 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 241 | struct rbd_obj_request { |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 242 | struct ceph_object_extent ex; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 243 | union { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 244 | bool tried_parent; /* for reads */ |
| 245 | enum rbd_obj_write_state write_state; /* for writes */ |
| 246 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 247 | |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 248 | struct rbd_img_request *img_request; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 249 | struct ceph_file_extent *img_extents; |
| 250 | u32 num_img_extents; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 251 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 252 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 253 | struct ceph_bio_iter bio_pos; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 254 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 255 | struct ceph_bvec_iter bvec_pos; |
| 256 | u32 bvec_count; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 257 | u32 bvec_idx; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 258 | }; |
| 259 | }; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 260 | struct bio_vec *copyup_bvecs; |
| 261 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 262 | |
| 263 | struct ceph_osd_request *osd_req; |
| 264 | |
| 265 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 266 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 267 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 268 | struct kref kref; |
| 269 | }; |
| 270 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 271 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 272 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 273 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 274 | }; |
| 275 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 277 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 278 | enum obj_operation_type op_type; |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 279 | enum obj_request_type data_type; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 280 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 282 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 283 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 284 | }; |
| 285 | union { |
| 286 | struct request *rq; /* block request */ |
| 287 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 288 | }; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 289 | spinlock_t completion_lock; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 290 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 291 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 292 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 293 | struct list_head object_extents; /* obj_req.ex structs */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 294 | u32 obj_request_count; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 295 | u32 pending_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 296 | |
| 297 | struct kref kref; |
| 298 | }; |
| 299 | |
| 300 | #define for_each_obj_request(ireq, oreq) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 301 | list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 303 | 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] | 304 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 305 | enum rbd_watch_state { |
| 306 | RBD_WATCH_STATE_UNREGISTERED, |
| 307 | RBD_WATCH_STATE_REGISTERED, |
| 308 | RBD_WATCH_STATE_ERROR, |
| 309 | }; |
| 310 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 311 | enum rbd_lock_state { |
| 312 | RBD_LOCK_STATE_UNLOCKED, |
| 313 | RBD_LOCK_STATE_LOCKED, |
| 314 | RBD_LOCK_STATE_RELEASING, |
| 315 | }; |
| 316 | |
| 317 | /* WatchNotify::ClientId */ |
| 318 | struct rbd_client_id { |
| 319 | u64 gid; |
| 320 | u64 handle; |
| 321 | }; |
| 322 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 323 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 324 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 325 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 326 | }; |
| 327 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 328 | /* |
| 329 | * a single device |
| 330 | */ |
| 331 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 332 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 333 | |
| 334 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 335 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 336 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 337 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 338 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 339 | struct rbd_client *rbd_client; |
| 340 | |
| 341 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 342 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 343 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 344 | |
| 345 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 346 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 347 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 348 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 349 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 350 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 351 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 352 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 353 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 354 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 355 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 356 | struct mutex watch_mutex; |
| 357 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 358 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 359 | u64 watch_cookie; |
| 360 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 361 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 362 | struct rw_semaphore lock_rwsem; |
| 363 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 364 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 365 | struct rbd_client_id owner_cid; |
| 366 | struct work_struct acquired_lock_work; |
| 367 | struct work_struct released_lock_work; |
| 368 | struct delayed_work lock_dwork; |
| 369 | struct work_struct unlock_work; |
| 370 | wait_queue_head_t lock_waitq; |
| 371 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 372 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 373 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 374 | struct rbd_spec *parent_spec; |
| 375 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 376 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 377 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 378 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 379 | /* Block layer tags. */ |
| 380 | struct blk_mq_tag_set tag_set; |
| 381 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 382 | /* protects updating the header */ |
| 383 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 384 | |
| 385 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 386 | |
| 387 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 388 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 389 | /* sysfs related */ |
| 390 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 391 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 392 | }; |
| 393 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 394 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 395 | * Flag bits for rbd_dev->flags: |
| 396 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 397 | * by rbd_dev->lock |
| 398 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 399 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 400 | enum rbd_dev_flags { |
| 401 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 402 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 403 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 404 | }; |
| 405 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 406 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 407 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 408 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 409 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 410 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 411 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 412 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 413 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 414 | /* Slab caches for frequently-allocated structures */ |
| 415 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 416 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 417 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 418 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 419 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 420 | static DEFINE_IDA(rbd_dev_id_ida); |
| 421 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 422 | static struct workqueue_struct *rbd_wq; |
| 423 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 424 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 425 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 426 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 427 | static bool single_major = true; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 428 | module_param(single_major, bool, 0444); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 429 | 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] | 430 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 431 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 432 | size_t count); |
| 433 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 434 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 435 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 436 | size_t count); |
| 437 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 438 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 439 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 440 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 441 | static int rbd_dev_id_to_minor(int dev_id) |
| 442 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 443 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int minor_to_rbd_dev_id(int minor) |
| 447 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 448 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 449 | } |
| 450 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 451 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 452 | { |
| 453 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 454 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 455 | } |
| 456 | |
| 457 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 458 | { |
| 459 | bool is_lock_owner; |
| 460 | |
| 461 | down_read(&rbd_dev->lock_rwsem); |
| 462 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 463 | up_read(&rbd_dev->lock_rwsem); |
| 464 | return is_lock_owner; |
| 465 | } |
| 466 | |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 467 | static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf) |
| 468 | { |
| 469 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 470 | } |
| 471 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 472 | static BUS_ATTR(add, 0200, NULL, rbd_add); |
| 473 | static BUS_ATTR(remove, 0200, NULL, rbd_remove); |
| 474 | static BUS_ATTR(add_single_major, 0200, NULL, rbd_add_single_major); |
| 475 | static BUS_ATTR(remove_single_major, 0200, NULL, rbd_remove_single_major); |
| 476 | static BUS_ATTR(supported_features, 0444, rbd_supported_features_show, NULL); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 477 | |
| 478 | static struct attribute *rbd_bus_attrs[] = { |
| 479 | &bus_attr_add.attr, |
| 480 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 481 | &bus_attr_add_single_major.attr, |
| 482 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 483 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 484 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 485 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 486 | |
| 487 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 488 | struct attribute *attr, int index) |
| 489 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 490 | if (!single_major && |
| 491 | (attr == &bus_attr_add_single_major.attr || |
| 492 | attr == &bus_attr_remove_single_major.attr)) |
| 493 | return 0; |
| 494 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 495 | return attr->mode; |
| 496 | } |
| 497 | |
| 498 | static const struct attribute_group rbd_bus_group = { |
| 499 | .attrs = rbd_bus_attrs, |
| 500 | .is_visible = rbd_bus_is_visible, |
| 501 | }; |
| 502 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 503 | |
| 504 | static struct bus_type rbd_bus_type = { |
| 505 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 506 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | static void rbd_root_dev_release(struct device *dev) |
| 510 | { |
| 511 | } |
| 512 | |
| 513 | static struct device rbd_root_dev = { |
| 514 | .init_name = "rbd", |
| 515 | .release = rbd_root_dev_release, |
| 516 | }; |
| 517 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 518 | static __printf(2, 3) |
| 519 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 520 | { |
| 521 | struct va_format vaf; |
| 522 | va_list args; |
| 523 | |
| 524 | va_start(args, fmt); |
| 525 | vaf.fmt = fmt; |
| 526 | vaf.va = &args; |
| 527 | |
| 528 | if (!rbd_dev) |
| 529 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 530 | else if (rbd_dev->disk) |
| 531 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 532 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 533 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 534 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 535 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 536 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 537 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 538 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 539 | else /* punt */ |
| 540 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 541 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 542 | va_end(args); |
| 543 | } |
| 544 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 545 | #ifdef RBD_DEBUG |
| 546 | #define rbd_assert(expr) \ |
| 547 | if (unlikely(!(expr))) { \ |
| 548 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 549 | "at line %d:\n\n" \ |
| 550 | "\trbd_assert(%s);\n\n", \ |
| 551 | __func__, __LINE__, #expr); \ |
| 552 | BUG(); \ |
| 553 | } |
| 554 | #else /* !RBD_DEBUG */ |
| 555 | # define rbd_assert(expr) ((void) 0) |
| 556 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 557 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 558 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 559 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 560 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 561 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 562 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 563 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 564 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 565 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 566 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 567 | u8 *order, u64 *snap_size); |
| 568 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 569 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 570 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 571 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 572 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 573 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 574 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 575 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 576 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 577 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 578 | removing = true; |
| 579 | else |
| 580 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 581 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 582 | if (removing) |
| 583 | return -ENOENT; |
| 584 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 585 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 586 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 587 | return 0; |
| 588 | } |
| 589 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 590 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 591 | { |
| 592 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 593 | unsigned long open_count_before; |
| 594 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 595 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 596 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 597 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 598 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 599 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 600 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 603 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 604 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 605 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 606 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 607 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 608 | return -EFAULT; |
| 609 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 610 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 611 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 612 | return -EROFS; |
| 613 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 614 | /* Let blkdev_roset() handle it */ |
| 615 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 619 | unsigned int cmd, unsigned long arg) |
| 620 | { |
| 621 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 622 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 623 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 624 | switch (cmd) { |
| 625 | case BLKROSET: |
| 626 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 627 | break; |
| 628 | default: |
| 629 | ret = -ENOTTY; |
| 630 | } |
| 631 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | #ifdef CONFIG_COMPAT |
| 636 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 637 | unsigned int cmd, unsigned long arg) |
| 638 | { |
| 639 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 640 | } |
| 641 | #endif /* CONFIG_COMPAT */ |
| 642 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 643 | static const struct block_device_operations rbd_bd_ops = { |
| 644 | .owner = THIS_MODULE, |
| 645 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 646 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 647 | .ioctl = rbd_ioctl, |
| 648 | #ifdef CONFIG_COMPAT |
| 649 | .compat_ioctl = rbd_compat_ioctl, |
| 650 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 654 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 655 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 656 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 657 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 658 | { |
| 659 | struct rbd_client *rbdc; |
| 660 | int ret = -ENOMEM; |
| 661 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 662 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 663 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 664 | if (!rbdc) |
| 665 | goto out_opt; |
| 666 | |
| 667 | kref_init(&rbdc->kref); |
| 668 | INIT_LIST_HEAD(&rbdc->node); |
| 669 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 670 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 671 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 672 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 673 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 674 | |
| 675 | ret = ceph_open_session(rbdc->client); |
| 676 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 677 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 678 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 679 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 680 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 681 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 682 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 683 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 684 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 685 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 686 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 687 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 688 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 689 | kfree(rbdc); |
| 690 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 691 | if (ceph_opts) |
| 692 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 693 | dout("%s: error %d\n", __func__, ret); |
| 694 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 695 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 698 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 699 | { |
| 700 | kref_get(&rbdc->kref); |
| 701 | |
| 702 | return rbdc; |
| 703 | } |
| 704 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 705 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 706 | * Find a ceph client with specific addr and configuration. If |
| 707 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 708 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 709 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 710 | { |
| 711 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 712 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 713 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 714 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 715 | return NULL; |
| 716 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 717 | spin_lock(&rbd_client_list_lock); |
| 718 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 719 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 720 | __rbd_get_client(client_node); |
| 721 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 722 | found = true; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | spin_unlock(&rbd_client_list_lock); |
| 727 | |
| 728 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 732 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 733 | */ |
| 734 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 735 | Opt_queue_depth, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 736 | Opt_lock_timeout, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 737 | Opt_last_int, |
| 738 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 739 | Opt_pool_ns, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 740 | Opt_last_string, |
| 741 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 742 | Opt_read_only, |
| 743 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 744 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 745 | Opt_exclusive, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 746 | Opt_notrim, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 747 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 748 | }; |
| 749 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 750 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 751 | {Opt_queue_depth, "queue_depth=%d"}, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 752 | {Opt_lock_timeout, "lock_timeout=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 753 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 754 | {Opt_pool_ns, "_pool_ns=%s"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 755 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 756 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 757 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 758 | {Opt_read_write, "read_write"}, |
| 759 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 760 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 761 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 762 | {Opt_notrim, "notrim"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 763 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 764 | }; |
| 765 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 766 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 767 | int queue_depth; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 768 | unsigned long lock_timeout; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 769 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 770 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 771 | bool exclusive; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 772 | bool trim; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 773 | }; |
| 774 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 775 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 776 | #define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */ |
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 |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 780 | #define RBD_TRIM_DEFAULT true |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 781 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 782 | struct parse_rbd_opts_ctx { |
| 783 | struct rbd_spec *spec; |
| 784 | struct rbd_options *opts; |
| 785 | }; |
| 786 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 787 | static int parse_rbd_opts_token(char *c, void *private) |
| 788 | { |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 789 | struct parse_rbd_opts_ctx *pctx = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 790 | substring_t argstr[MAX_OPT_ARGS]; |
| 791 | int token, intval, ret; |
| 792 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 793 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | if (token < Opt_last_int) { |
| 795 | ret = match_int(&argstr[0], &intval); |
| 796 | if (ret < 0) { |
Ilya Dryomov | 2f56b6b | 2018-06-27 16:38:13 +0200 | [diff] [blame] | 797 | pr_err("bad option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | dout("got int token %d val %d\n", token, intval); |
| 801 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 802 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 803 | } else { |
| 804 | dout("got token %d\n", token); |
| 805 | } |
| 806 | |
| 807 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 808 | case Opt_queue_depth: |
| 809 | if (intval < 1) { |
| 810 | pr_err("queue_depth out of range\n"); |
| 811 | return -EINVAL; |
| 812 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 813 | pctx->opts->queue_depth = intval; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 814 | break; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 815 | case Opt_lock_timeout: |
| 816 | /* 0 is "wait forever" (i.e. infinite timeout) */ |
| 817 | if (intval < 0 || intval > INT_MAX / 1000) { |
| 818 | pr_err("lock_timeout out of range\n"); |
| 819 | return -EINVAL; |
| 820 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 821 | pctx->opts->lock_timeout = msecs_to_jiffies(intval * 1000); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 822 | break; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 823 | case Opt_pool_ns: |
| 824 | kfree(pctx->spec->pool_ns); |
| 825 | pctx->spec->pool_ns = match_strdup(argstr); |
| 826 | if (!pctx->spec->pool_ns) |
| 827 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 828 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 829 | case Opt_read_only: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 830 | pctx->opts->read_only = true; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 831 | break; |
| 832 | case Opt_read_write: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 833 | pctx->opts->read_only = false; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 834 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 835 | case Opt_lock_on_read: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 836 | pctx->opts->lock_on_read = true; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 837 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 838 | case Opt_exclusive: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 839 | pctx->opts->exclusive = true; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 840 | break; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 841 | case Opt_notrim: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 842 | pctx->opts->trim = false; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 843 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 844 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 845 | /* libceph prints "bad option" msg */ |
| 846 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 847 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 848 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 849 | return 0; |
| 850 | } |
| 851 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 852 | static char* obj_op_name(enum obj_operation_type op_type) |
| 853 | { |
| 854 | switch (op_type) { |
| 855 | case OBJ_OP_READ: |
| 856 | return "read"; |
| 857 | case OBJ_OP_WRITE: |
| 858 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 859 | case OBJ_OP_DISCARD: |
| 860 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 861 | default: |
| 862 | return "???"; |
| 863 | } |
| 864 | } |
| 865 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 866 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 867 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 868 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 869 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 870 | */ |
| 871 | static void rbd_client_release(struct kref *kref) |
| 872 | { |
| 873 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 874 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 875 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 876 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 877 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 878 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 879 | |
| 880 | ceph_destroy_client(rbdc->client); |
| 881 | kfree(rbdc); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 886 | * it. |
| 887 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 888 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 890 | if (rbdc) |
| 891 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 894 | static int wait_for_latest_osdmap(struct ceph_client *client) |
| 895 | { |
| 896 | u64 newest_epoch; |
| 897 | int ret; |
| 898 | |
| 899 | ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch); |
| 900 | if (ret) |
| 901 | return ret; |
| 902 | |
| 903 | if (client->osdc.osdmap->epoch >= newest_epoch) |
| 904 | return 0; |
| 905 | |
| 906 | ceph_osdc_maybe_request_map(&client->osdc); |
| 907 | return ceph_monc_wait_osdmap(&client->monc, newest_epoch, |
| 908 | client->options->mount_timeout); |
| 909 | } |
| 910 | |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 911 | /* |
| 912 | * Get a ceph client with specific addr and configuration, if one does |
| 913 | * not exist create it. Either way, ceph_opts is consumed by this |
| 914 | * function. |
| 915 | */ |
| 916 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
| 917 | { |
| 918 | struct rbd_client *rbdc; |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 919 | int ret; |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 920 | |
| 921 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
| 922 | rbdc = rbd_client_find(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 923 | if (rbdc) { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 924 | ceph_destroy_options(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 925 | |
| 926 | /* |
| 927 | * Using an existing client. Make sure ->pg_pools is up to |
| 928 | * date before we look up the pool id in do_rbd_add(). |
| 929 | */ |
| 930 | ret = wait_for_latest_osdmap(rbdc->client); |
| 931 | if (ret) { |
| 932 | rbd_warn(NULL, "failed to get latest osdmap: %d", ret); |
| 933 | rbd_put_client(rbdc); |
| 934 | rbdc = ERR_PTR(ret); |
| 935 | } |
| 936 | } else { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 937 | rbdc = rbd_client_create(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 938 | } |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 939 | mutex_unlock(&client_mutex); |
| 940 | |
| 941 | return rbdc; |
| 942 | } |
| 943 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 944 | static bool rbd_image_format_valid(u32 image_format) |
| 945 | { |
| 946 | return image_format == 1 || image_format == 2; |
| 947 | } |
| 948 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 949 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 950 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 951 | size_t size; |
| 952 | u32 snap_count; |
| 953 | |
| 954 | /* The header has to start with the magic rbd header text */ |
| 955 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 956 | return false; |
| 957 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 958 | /* The bio layer requires at least sector-sized I/O */ |
| 959 | |
| 960 | if (ondisk->options.order < SECTOR_SHIFT) |
| 961 | return false; |
| 962 | |
| 963 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 964 | |
| 965 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 966 | return false; |
| 967 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 968 | /* |
| 969 | * The size of a snapshot header has to fit in a size_t, and |
| 970 | * that limits the number of snapshots. |
| 971 | */ |
| 972 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 973 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 974 | if (snap_count > size / sizeof (__le64)) |
| 975 | return false; |
| 976 | |
| 977 | /* |
| 978 | * Not only that, but the size of the entire the snapshot |
| 979 | * header must also be representable in a size_t. |
| 980 | */ |
| 981 | size -= snap_count * sizeof (__le64); |
| 982 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 983 | return false; |
| 984 | |
| 985 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 986 | } |
| 987 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 988 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 989 | * returns the size of an object in the image |
| 990 | */ |
| 991 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 992 | { |
| 993 | return 1U << header->obj_order; |
| 994 | } |
| 995 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 996 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 997 | { |
| 998 | if (rbd_dev->header.stripe_unit == 0 || |
| 999 | rbd_dev->header.stripe_count == 0) { |
| 1000 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 1001 | rbd_dev->header.stripe_count = 1; |
| 1002 | } |
| 1003 | |
| 1004 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 1005 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 1006 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1007 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 1008 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1009 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 1010 | } |
| 1011 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1012 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1013 | * Fill an rbd image header with information from the given format 1 |
| 1014 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1015 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1016 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1017 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1018 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1019 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1020 | bool first_time = header->object_prefix == NULL; |
| 1021 | struct ceph_snap_context *snapc; |
| 1022 | char *object_prefix = NULL; |
| 1023 | char *snap_names = NULL; |
| 1024 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1025 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1026 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1027 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1028 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | /* Allocate this now to avoid having to handle failure below */ |
| 1030 | |
| 1031 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1032 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1033 | sizeof(ondisk->object_prefix), |
| 1034 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1035 | if (!object_prefix) |
| 1036 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1040 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1041 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1042 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1043 | if (!snapc) |
| 1044 | goto out_err; |
| 1045 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1046 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1047 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1048 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1049 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1050 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1051 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1052 | if (snap_names_len > (u64)SIZE_MAX) |
| 1053 | goto out_2big; |
| 1054 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1055 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1056 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1057 | |
| 1058 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1059 | snap_sizes = kmalloc_array(snap_count, |
| 1060 | sizeof(*header->snap_sizes), |
| 1061 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1062 | if (!snap_sizes) |
| 1063 | goto out_err; |
| 1064 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1065 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1066 | * Copy the names, and fill in each snapshot's id |
| 1067 | * and size. |
| 1068 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1069 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1070 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1071 | * snap_names_len bytes beyond the end of the |
| 1072 | * snapshot id array, this memcpy() is safe. |
| 1073 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1075 | snaps = ondisk->snaps; |
| 1076 | for (i = 0; i < snap_count; i++) { |
| 1077 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1078 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1079 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1080 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1081 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1082 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1083 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1084 | if (first_time) { |
| 1085 | header->object_prefix = object_prefix; |
| 1086 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1087 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1088 | } else { |
| 1089 | ceph_put_snap_context(header->snapc); |
| 1090 | kfree(header->snap_names); |
| 1091 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1095 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1096 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1097 | header->snapc = snapc; |
| 1098 | header->snap_names = snap_names; |
| 1099 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1100 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1101 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1102 | out_2big: |
| 1103 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1104 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1105 | kfree(snap_sizes); |
| 1106 | kfree(snap_names); |
| 1107 | ceph_put_snap_context(snapc); |
| 1108 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1109 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1110 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1113 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1114 | { |
| 1115 | const char *snap_name; |
| 1116 | |
| 1117 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1118 | |
| 1119 | /* Skip over names until we find the one we are looking for */ |
| 1120 | |
| 1121 | snap_name = rbd_dev->header.snap_names; |
| 1122 | while (which--) |
| 1123 | snap_name += strlen(snap_name) + 1; |
| 1124 | |
| 1125 | return kstrdup(snap_name, GFP_KERNEL); |
| 1126 | } |
| 1127 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1128 | /* |
| 1129 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1130 | * Note that result is for snapshots in *descending* order. |
| 1131 | */ |
| 1132 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1133 | { |
| 1134 | u64 snap_id1 = *(u64 *)s1; |
| 1135 | u64 snap_id2 = *(u64 *)s2; |
| 1136 | |
| 1137 | if (snap_id1 < snap_id2) |
| 1138 | return 1; |
| 1139 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Search a snapshot context to see if the given snapshot id is |
| 1144 | * present. |
| 1145 | * |
| 1146 | * Returns the position of the snapshot id in the array if it's found, |
| 1147 | * or BAD_SNAP_INDEX otherwise. |
| 1148 | * |
| 1149 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1150 | * reverse order, highest snapshot id first. |
| 1151 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1152 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1153 | { |
| 1154 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1155 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1156 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1157 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1158 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1159 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1160 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1163 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1164 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1165 | { |
| 1166 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1167 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1168 | |
| 1169 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1170 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1171 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1172 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1173 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1174 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1175 | } |
| 1176 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1177 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1178 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1179 | if (snap_id == CEPH_NOSNAP) |
| 1180 | return RBD_SNAP_HEAD_NAME; |
| 1181 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1182 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1183 | if (rbd_dev->image_format == 1) |
| 1184 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1185 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1186 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1187 | } |
| 1188 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1189 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1190 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1191 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1192 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1193 | if (snap_id == CEPH_NOSNAP) { |
| 1194 | *snap_size = rbd_dev->header.image_size; |
| 1195 | } else if (rbd_dev->image_format == 1) { |
| 1196 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1197 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1198 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1199 | if (which == BAD_SNAP_INDEX) |
| 1200 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1201 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1202 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1203 | } else { |
| 1204 | u64 size = 0; |
| 1205 | int ret; |
| 1206 | |
| 1207 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1208 | if (ret) |
| 1209 | return ret; |
| 1210 | |
| 1211 | *snap_size = size; |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1217 | u64 *snap_features) |
| 1218 | { |
| 1219 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1220 | if (snap_id == CEPH_NOSNAP) { |
| 1221 | *snap_features = rbd_dev->header.features; |
| 1222 | } else if (rbd_dev->image_format == 1) { |
| 1223 | *snap_features = 0; /* No features for format 1 */ |
| 1224 | } else { |
| 1225 | u64 features = 0; |
| 1226 | int ret; |
| 1227 | |
| 1228 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1229 | if (ret) |
| 1230 | return ret; |
| 1231 | |
| 1232 | *snap_features = features; |
| 1233 | } |
| 1234 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1237 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1238 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1239 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1240 | u64 size = 0; |
| 1241 | u64 features = 0; |
| 1242 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1243 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1244 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1245 | if (ret) |
| 1246 | return ret; |
| 1247 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1248 | if (ret) |
| 1249 | return ret; |
| 1250 | |
| 1251 | rbd_dev->mapping.size = size; |
| 1252 | rbd_dev->mapping.features = features; |
| 1253 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1254 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1255 | } |
| 1256 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1257 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1258 | { |
| 1259 | rbd_dev->mapping.size = 0; |
| 1260 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1263 | static void zero_bvec(struct bio_vec *bv) |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1264 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1265 | void *buf; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1266 | unsigned long flags; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1267 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1268 | buf = bvec_kmap_irq(bv, &flags); |
| 1269 | memset(buf, 0, bv->bv_len); |
| 1270 | flush_dcache_page(bv->bv_page); |
| 1271 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1274 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1275 | { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1276 | struct ceph_bio_iter it = *bio_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1277 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1278 | ceph_bio_iter_advance(&it, off); |
| 1279 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1280 | zero_bvec(&bv); |
| 1281 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1282 | } |
| 1283 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1284 | static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1285 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1286 | struct ceph_bvec_iter it = *bvec_pos; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1287 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1288 | ceph_bvec_iter_advance(&it, off); |
| 1289 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1290 | zero_bvec(&bv); |
| 1291 | })); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1292 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1293 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1294 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1295 | * 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] | 1296 | * (private) bio_vec array. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1297 | * |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1298 | * @off is relative to the start of the data buffer. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1299 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1300 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1301 | u32 bytes) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1302 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1303 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1304 | case OBJ_REQUEST_BIO: |
| 1305 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1306 | break; |
| 1307 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1308 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1309 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1310 | break; |
| 1311 | default: |
| 1312 | rbd_assert(0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1313 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1317 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1318 | { |
| 1319 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1320 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1321 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1322 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1323 | } |
| 1324 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1325 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1326 | { |
| 1327 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1328 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1329 | kref_get(&img_request->kref); |
| 1330 | } |
| 1331 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1332 | static void rbd_img_request_destroy(struct kref *kref); |
| 1333 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1334 | { |
| 1335 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1336 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1337 | kref_read(&img_request->kref)); |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1338 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1342 | struct rbd_obj_request *obj_request) |
| 1343 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1344 | rbd_assert(obj_request->img_request == NULL); |
| 1345 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1346 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1347 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1348 | img_request->obj_request_count++; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1349 | img_request->pending_count++; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1350 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1354 | struct rbd_obj_request *obj_request) |
| 1355 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1356 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1357 | list_del(&obj_request->ex.oe_item); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1358 | rbd_assert(img_request->obj_request_count > 0); |
| 1359 | img_request->obj_request_count--; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1360 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1361 | rbd_obj_request_put(obj_request); |
| 1362 | } |
| 1363 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1364 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1365 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1366 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1367 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1368 | 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] | 1369 | obj_request, obj_request->ex.oe_objno, obj_request->ex.oe_off, |
| 1370 | obj_request->ex.oe_len, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1371 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1372 | } |
| 1373 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1374 | /* |
| 1375 | * The default/initial value for all image request flags is 0. Each |
| 1376 | * is conditionally set to 1 at image request initialization time |
| 1377 | * and currently never change thereafter. |
| 1378 | */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1379 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1380 | { |
| 1381 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1382 | smp_mb(); |
| 1383 | } |
| 1384 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1385 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1386 | { |
| 1387 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1388 | smp_mb(); |
| 1389 | } |
| 1390 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1391 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1392 | { |
| 1393 | smp_mb(); |
| 1394 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1395 | } |
| 1396 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1397 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1398 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1399 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1400 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1401 | return !obj_req->ex.oe_off && |
| 1402 | obj_req->ex.oe_len == rbd_dev->layout.object_size; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1405 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1406 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1407 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1408 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1409 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1410 | rbd_dev->layout.object_size; |
| 1411 | } |
| 1412 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1413 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
| 1414 | { |
| 1415 | return ceph_file_extents_bytes(obj_req->img_extents, |
| 1416 | obj_req->num_img_extents); |
| 1417 | } |
| 1418 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1419 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1420 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1421 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1422 | case OBJ_OP_READ: |
| 1423 | return false; |
| 1424 | case OBJ_OP_WRITE: |
| 1425 | case OBJ_OP_DISCARD: |
| 1426 | return true; |
| 1427 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 1428 | BUG(); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1429 | } |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1430 | } |
| 1431 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1432 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1433 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1434 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1435 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1436 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1437 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1438 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1439 | osd_req->r_result, obj_req); |
| 1440 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1441 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1442 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1443 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1444 | obj_req->xferred = osd_req->r_result; |
| 1445 | else |
| 1446 | /* |
| 1447 | * Writes aren't allowed to return a data payload. In some |
| 1448 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1449 | * a stat response makes it through, but we don't care. |
| 1450 | */ |
| 1451 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1452 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1453 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1454 | } |
| 1455 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1456 | 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] | 1457 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1458 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1459 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1460 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1461 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1465 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1466 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1467 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1468 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Arnd Bergmann | fac02dd | 2018-07-13 22:18:37 +0200 | [diff] [blame] | 1469 | ktime_get_real_ts64(&osd_req->r_mtime); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1470 | osd_req->r_data_offset = obj_request->ex.oe_off; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1471 | } |
| 1472 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1473 | static struct ceph_osd_request * |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1474 | 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] | 1475 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1476 | struct rbd_img_request *img_req = obj_req->img_request; |
| 1477 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1478 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1479 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1480 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1481 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1482 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1483 | req = ceph_osdc_alloc_request(osdc, |
| 1484 | (rbd_img_is_write(img_req) ? img_req->snapc : NULL), |
| 1485 | num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1486 | if (!req) |
| 1487 | return NULL; |
| 1488 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1489 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1490 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1491 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1492 | /* |
| 1493 | * Data objects may be stored in a separate pool, but always in |
| 1494 | * the same namespace in that pool as the header in its pool. |
| 1495 | */ |
| 1496 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1497 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1498 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1499 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1500 | rbd_dev->header.object_prefix, obj_req->ex.oe_objno)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1501 | goto err_req; |
| 1502 | |
| 1503 | if (ceph_osdc_alloc_messages(req, GFP_NOIO)) |
| 1504 | goto err_req; |
| 1505 | |
| 1506 | return req; |
| 1507 | |
| 1508 | err_req: |
| 1509 | ceph_osdc_put_request(req); |
| 1510 | return NULL; |
| 1511 | } |
| 1512 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1513 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1514 | { |
| 1515 | ceph_osdc_put_request(osd_req); |
| 1516 | } |
| 1517 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1518 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1519 | { |
| 1520 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1521 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1522 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1523 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1524 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1525 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1526 | ceph_object_extent_init(&obj_request->ex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1527 | kref_init(&obj_request->kref); |
| 1528 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1529 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1530 | return obj_request; |
| 1531 | } |
| 1532 | |
| 1533 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1534 | { |
| 1535 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1536 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1537 | |
| 1538 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1539 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1540 | dout("%s: obj %p\n", __func__, obj_request); |
| 1541 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1542 | if (obj_request->osd_req) |
| 1543 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1544 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1545 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1546 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1547 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1548 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1549 | break; /* Nothing to do */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1550 | case OBJ_REQUEST_OWN_BVECS: |
| 1551 | kfree(obj_request->bvec_pos.bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1552 | break; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1553 | default: |
| 1554 | rbd_assert(0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1555 | } |
| 1556 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1557 | kfree(obj_request->img_extents); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1558 | if (obj_request->copyup_bvecs) { |
| 1559 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1560 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1561 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1562 | } |
| 1563 | kfree(obj_request->copyup_bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1564 | } |
| 1565 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1566 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1567 | } |
| 1568 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1569 | /* It's OK to call this for a device with no parent */ |
| 1570 | |
| 1571 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1572 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1573 | { |
| 1574 | rbd_dev_remove_parent(rbd_dev); |
| 1575 | rbd_spec_put(rbd_dev->parent_spec); |
| 1576 | rbd_dev->parent_spec = NULL; |
| 1577 | rbd_dev->parent_overlap = 0; |
| 1578 | } |
| 1579 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1580 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1581 | * Parent image reference counting is used to determine when an |
| 1582 | * image's parent fields can be safely torn down--after there are no |
| 1583 | * more in-flight requests to the parent image. When the last |
| 1584 | * reference is dropped, cleaning them up is safe. |
| 1585 | */ |
| 1586 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1587 | { |
| 1588 | int counter; |
| 1589 | |
| 1590 | if (!rbd_dev->parent_spec) |
| 1591 | return; |
| 1592 | |
| 1593 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1594 | if (counter > 0) |
| 1595 | return; |
| 1596 | |
| 1597 | /* Last reference; clean up parent data structures */ |
| 1598 | |
| 1599 | if (!counter) |
| 1600 | rbd_dev_unparent(rbd_dev); |
| 1601 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1602 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * If an image has a non-zero parent overlap, get a reference to its |
| 1607 | * parent. |
| 1608 | * |
| 1609 | * Returns true if the rbd device has a parent with a non-zero |
| 1610 | * overlap and a reference for it was successfully taken, or |
| 1611 | * false otherwise. |
| 1612 | */ |
| 1613 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1614 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1615 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1616 | |
| 1617 | if (!rbd_dev->parent_spec) |
| 1618 | return false; |
| 1619 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1620 | down_read(&rbd_dev->header_rwsem); |
| 1621 | if (rbd_dev->parent_overlap) |
| 1622 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1623 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1624 | |
| 1625 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1626 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1627 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1628 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1629 | } |
| 1630 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1631 | /* |
| 1632 | * Caller is responsible for filling in the list of object requests |
| 1633 | * that comprises the image request, and the Linux request pointer |
| 1634 | * (if there is one). |
| 1635 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1636 | static struct rbd_img_request *rbd_img_request_create( |
| 1637 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1638 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1639 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1640 | { |
| 1641 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1642 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1643 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1644 | if (!img_request) |
| 1645 | return NULL; |
| 1646 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1647 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1648 | img_request->op_type = op_type; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1649 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1650 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1651 | else |
| 1652 | img_request->snapc = snapc; |
| 1653 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1654 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1655 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1656 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1657 | spin_lock_init(&img_request->completion_lock); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1658 | INIT_LIST_HEAD(&img_request->object_extents); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1659 | kref_init(&img_request->kref); |
| 1660 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1661 | dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, |
| 1662 | obj_op_name(op_type), img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1663 | return img_request; |
| 1664 | } |
| 1665 | |
| 1666 | static void rbd_img_request_destroy(struct kref *kref) |
| 1667 | { |
| 1668 | struct rbd_img_request *img_request; |
| 1669 | struct rbd_obj_request *obj_request; |
| 1670 | struct rbd_obj_request *next_obj_request; |
| 1671 | |
| 1672 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1673 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1674 | dout("%s: img %p\n", __func__, img_request); |
| 1675 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1676 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1677 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1678 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1679 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1680 | if (img_request_layered_test(img_request)) { |
| 1681 | img_request_layered_clear(img_request); |
| 1682 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1683 | } |
| 1684 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1685 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1686 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1687 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1688 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1689 | } |
| 1690 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1691 | static void prune_extents(struct ceph_file_extent *img_extents, |
| 1692 | u32 *num_img_extents, u64 overlap) |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1693 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1694 | u32 cnt = *num_img_extents; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1695 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1696 | /* drop extents completely beyond the overlap */ |
| 1697 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) |
| 1698 | cnt--; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1699 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1700 | if (cnt) { |
| 1701 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1702 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1703 | /* trim final overlapping extent */ |
| 1704 | if (ex->fe_off + ex->fe_len > overlap) |
| 1705 | ex->fe_len = overlap - ex->fe_off; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1706 | } |
| 1707 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1708 | *num_img_extents = cnt; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1709 | } |
| 1710 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1711 | /* |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1712 | * Determine the byte range(s) covered by either just the object extent |
| 1713 | * or the entire object in the parent image. |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1714 | */ |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1715 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, |
| 1716 | bool entire) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1717 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1718 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1719 | int ret; |
| 1720 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1721 | if (!rbd_dev->parent_overlap) |
| 1722 | return 0; |
| 1723 | |
| 1724 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
| 1725 | entire ? 0 : obj_req->ex.oe_off, |
| 1726 | entire ? rbd_dev->layout.object_size : |
| 1727 | obj_req->ex.oe_len, |
| 1728 | &obj_req->img_extents, |
| 1729 | &obj_req->num_img_extents); |
| 1730 | if (ret) |
| 1731 | return ret; |
| 1732 | |
| 1733 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 1734 | rbd_dev->parent_overlap); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1738 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1739 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1740 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1741 | case OBJ_REQUEST_BIO: |
| 1742 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1743 | &obj_req->bio_pos, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1744 | obj_req->ex.oe_len); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1745 | break; |
| 1746 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1747 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1748 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1749 | obj_req->ex.oe_len); |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1750 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1751 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1752 | &obj_req->bvec_pos); |
| 1753 | break; |
| 1754 | default: |
| 1755 | rbd_assert(0); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1760 | { |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1761 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1762 | if (!obj_req->osd_req) |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1763 | return -ENOMEM; |
| 1764 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1765 | 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] | 1766 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1767 | rbd_osd_req_setup_data(obj_req, 0); |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1768 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1769 | rbd_osd_req_format_read(obj_req); |
| 1770 | return 0; |
| 1771 | } |
| 1772 | |
| 1773 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1774 | unsigned int which) |
| 1775 | { |
| 1776 | struct page **pages; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1777 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1778 | /* |
| 1779 | * The response data for a STAT call consists of: |
| 1780 | * le64 length; |
| 1781 | * struct { |
| 1782 | * le32 tv_sec; |
| 1783 | * le32 tv_nsec; |
| 1784 | * } mtime; |
| 1785 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1786 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1787 | if (IS_ERR(pages)) |
| 1788 | return PTR_ERR(pages); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1789 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1790 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1791 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1792 | 8 + sizeof(struct ceph_timespec), |
| 1793 | 0, false, true); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1794 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1795 | } |
| 1796 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1797 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 1798 | unsigned int which) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1799 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1800 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1801 | u16 opcode; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1802 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1803 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 1804 | rbd_dev->layout.object_size, |
| 1805 | rbd_dev->layout.object_size); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1806 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1807 | if (rbd_obj_is_entire(obj_req)) |
| 1808 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1809 | else |
| 1810 | opcode = CEPH_OSD_OP_WRITE; |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1811 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1812 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1813 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1814 | rbd_osd_req_setup_data(obj_req, which++); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1815 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1816 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1817 | rbd_osd_req_format_write(obj_req); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1818 | } |
| 1819 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1820 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1821 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1822 | unsigned int num_osd_ops, which = 0; |
| 1823 | int ret; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 1824 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1825 | /* reverse map the entire object onto the parent */ |
| 1826 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1827 | if (ret) |
| 1828 | return ret; |
| 1829 | |
| 1830 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1831 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1832 | num_osd_ops = 3; /* stat + setallochint + write/writefull */ |
| 1833 | } else { |
| 1834 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1835 | num_osd_ops = 2; /* setallochint + write/writefull */ |
| 1836 | } |
| 1837 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1838 | 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] | 1839 | if (!obj_req->osd_req) |
| 1840 | return -ENOMEM; |
| 1841 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1842 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1843 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1844 | if (ret) |
| 1845 | return ret; |
| 1846 | } |
| 1847 | |
| 1848 | __rbd_obj_setup_write(obj_req, which); |
| 1849 | return 0; |
| 1850 | } |
| 1851 | |
| 1852 | static void __rbd_obj_setup_discard(struct rbd_obj_request *obj_req, |
| 1853 | unsigned int which) |
| 1854 | { |
| 1855 | u16 opcode; |
| 1856 | |
| 1857 | if (rbd_obj_is_entire(obj_req)) { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1858 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1859 | osd_req_op_init(obj_req->osd_req, which++, |
| 1860 | CEPH_OSD_OP_CREATE, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1861 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1862 | } else { |
| 1863 | osd_req_op_init(obj_req->osd_req, which++, |
| 1864 | CEPH_OSD_OP_DELETE, 0); |
| 1865 | opcode = 0; |
| 1866 | } |
| 1867 | } else if (rbd_obj_is_tail(obj_req)) { |
| 1868 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1869 | } else { |
| 1870 | opcode = CEPH_OSD_OP_ZERO; |
| 1871 | } |
| 1872 | |
| 1873 | if (opcode) |
| 1874 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1875 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1876 | 0, 0); |
| 1877 | |
| 1878 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1879 | rbd_osd_req_format_write(obj_req); |
| 1880 | } |
| 1881 | |
| 1882 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 1883 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1884 | unsigned int num_osd_ops, which = 0; |
| 1885 | int ret; |
| 1886 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1887 | /* reverse map the entire object onto the parent */ |
| 1888 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1889 | if (ret) |
| 1890 | return ret; |
| 1891 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1892 | if (rbd_obj_is_entire(obj_req)) { |
| 1893 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1894 | if (obj_req->num_img_extents) |
| 1895 | num_osd_ops = 2; /* create + truncate */ |
| 1896 | else |
| 1897 | num_osd_ops = 1; /* delete */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1898 | } else { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1899 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1900 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1901 | num_osd_ops = 2; /* stat + truncate/zero */ |
| 1902 | } else { |
| 1903 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1904 | num_osd_ops = 1; /* truncate/zero */ |
| 1905 | } |
| 1906 | } |
| 1907 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1908 | 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] | 1909 | if (!obj_req->osd_req) |
| 1910 | return -ENOMEM; |
| 1911 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1912 | if (!rbd_obj_is_entire(obj_req) && obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1913 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1914 | if (ret) |
| 1915 | return ret; |
| 1916 | } |
| 1917 | |
| 1918 | __rbd_obj_setup_discard(obj_req, which); |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | /* |
| 1923 | * For each object request in @img_req, allocate an OSD request, add |
| 1924 | * individual OSD ops and prepare them for submission. The number of |
| 1925 | * OSD ops depends on op_type and the overlap point (if any). |
| 1926 | */ |
| 1927 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 1928 | { |
| 1929 | struct rbd_obj_request *obj_req; |
| 1930 | int ret; |
| 1931 | |
| 1932 | for_each_obj_request(img_req, obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1933 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1934 | case OBJ_OP_READ: |
| 1935 | ret = rbd_obj_setup_read(obj_req); |
| 1936 | break; |
| 1937 | case OBJ_OP_WRITE: |
| 1938 | ret = rbd_obj_setup_write(obj_req); |
| 1939 | break; |
| 1940 | case OBJ_OP_DISCARD: |
| 1941 | ret = rbd_obj_setup_discard(obj_req); |
| 1942 | break; |
| 1943 | default: |
| 1944 | rbd_assert(0); |
| 1945 | } |
| 1946 | if (ret) |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
| 1950 | return 0; |
| 1951 | } |
| 1952 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1953 | union rbd_img_fill_iter { |
| 1954 | struct ceph_bio_iter bio_iter; |
| 1955 | struct ceph_bvec_iter bvec_iter; |
| 1956 | }; |
| 1957 | |
| 1958 | struct rbd_img_fill_ctx { |
| 1959 | enum obj_request_type pos_type; |
| 1960 | union rbd_img_fill_iter *pos; |
| 1961 | union rbd_img_fill_iter iter; |
| 1962 | ceph_object_extent_fn_t set_pos_fn; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1963 | ceph_object_extent_fn_t count_fn; |
| 1964 | ceph_object_extent_fn_t copy_fn; |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1965 | }; |
| 1966 | |
| 1967 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
| 1968 | { |
| 1969 | struct rbd_img_request *img_req = arg; |
| 1970 | struct rbd_obj_request *obj_req; |
| 1971 | |
| 1972 | obj_req = rbd_obj_request_create(); |
| 1973 | if (!obj_req) |
| 1974 | return NULL; |
| 1975 | |
| 1976 | rbd_img_obj_request_add(img_req, obj_req); |
| 1977 | return &obj_req->ex; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1981 | * While su != os && sc == 1 is technically not fancy (it's the same |
| 1982 | * layout as su == os && sc == 1), we can't use the nocopy path for it |
| 1983 | * because ->set_pos_fn() should be called only once per object. |
| 1984 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so |
| 1985 | * treat su != os && sc == 1 as fancy. |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1986 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1987 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) |
| 1988 | { |
| 1989 | return l->stripe_unit != l->object_size; |
| 1990 | } |
| 1991 | |
| 1992 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
| 1993 | struct ceph_file_extent *img_extents, |
| 1994 | u32 num_img_extents, |
| 1995 | struct rbd_img_fill_ctx *fctx) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1996 | { |
| 1997 | u32 i; |
| 1998 | int ret; |
| 1999 | |
| 2000 | img_req->data_type = fctx->pos_type; |
| 2001 | |
| 2002 | /* |
| 2003 | * Create object requests and set each object request's starting |
| 2004 | * position in the provided bio (list) or bio_vec array. |
| 2005 | */ |
| 2006 | fctx->iter = *fctx->pos; |
| 2007 | for (i = 0; i < num_img_extents; i++) { |
| 2008 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, |
| 2009 | img_extents[i].fe_off, |
| 2010 | img_extents[i].fe_len, |
| 2011 | &img_req->object_extents, |
| 2012 | alloc_object_extent, img_req, |
| 2013 | fctx->set_pos_fn, &fctx->iter); |
| 2014 | if (ret) |
| 2015 | return ret; |
| 2016 | } |
| 2017 | |
| 2018 | return __rbd_img_fill_request(img_req); |
| 2019 | } |
| 2020 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2021 | /* |
| 2022 | * Map a list of image extents to a list of object extents, create the |
| 2023 | * corresponding object requests (normally each to a different object, |
| 2024 | * but not always) and add them to @img_req. For each object request, |
| 2025 | * set up its data descriptor to point to the corresponding chunk(s) of |
| 2026 | * @fctx->pos data buffer. |
| 2027 | * |
| 2028 | * Because ceph_file_to_extents() will merge adjacent object extents |
| 2029 | * together, each object request's data descriptor may point to multiple |
| 2030 | * different chunks of @fctx->pos data buffer. |
| 2031 | * |
| 2032 | * @fctx->pos data buffer is assumed to be large enough. |
| 2033 | */ |
| 2034 | static int rbd_img_fill_request(struct rbd_img_request *img_req, |
| 2035 | struct ceph_file_extent *img_extents, |
| 2036 | u32 num_img_extents, |
| 2037 | struct rbd_img_fill_ctx *fctx) |
| 2038 | { |
| 2039 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2040 | struct rbd_obj_request *obj_req; |
| 2041 | u32 i; |
| 2042 | int ret; |
| 2043 | |
| 2044 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
| 2045 | !rbd_layout_is_fancy(&rbd_dev->layout)) |
| 2046 | return rbd_img_fill_request_nocopy(img_req, img_extents, |
| 2047 | num_img_extents, fctx); |
| 2048 | |
| 2049 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
| 2050 | |
| 2051 | /* |
| 2052 | * Create object requests and determine ->bvec_count for each object |
| 2053 | * request. Note that ->bvec_count sum over all object requests may |
| 2054 | * be greater than the number of bio_vecs in the provided bio (list) |
| 2055 | * or bio_vec array because when mapped, those bio_vecs can straddle |
| 2056 | * stripe unit boundaries. |
| 2057 | */ |
| 2058 | fctx->iter = *fctx->pos; |
| 2059 | for (i = 0; i < num_img_extents; i++) { |
| 2060 | ret = ceph_file_to_extents(&rbd_dev->layout, |
| 2061 | img_extents[i].fe_off, |
| 2062 | img_extents[i].fe_len, |
| 2063 | &img_req->object_extents, |
| 2064 | alloc_object_extent, img_req, |
| 2065 | fctx->count_fn, &fctx->iter); |
| 2066 | if (ret) |
| 2067 | return ret; |
| 2068 | } |
| 2069 | |
| 2070 | for_each_obj_request(img_req, obj_req) { |
| 2071 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, |
| 2072 | sizeof(*obj_req->bvec_pos.bvecs), |
| 2073 | GFP_NOIO); |
| 2074 | if (!obj_req->bvec_pos.bvecs) |
| 2075 | return -ENOMEM; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2079 | * Fill in each object request's private bio_vec array, splitting and |
| 2080 | * rearranging the provided bio_vecs in stripe unit chunks as needed. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2081 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2082 | fctx->iter = *fctx->pos; |
| 2083 | for (i = 0; i < num_img_extents; i++) { |
| 2084 | ret = ceph_iterate_extents(&rbd_dev->layout, |
| 2085 | img_extents[i].fe_off, |
| 2086 | img_extents[i].fe_len, |
| 2087 | &img_req->object_extents, |
| 2088 | fctx->copy_fn, &fctx->iter); |
| 2089 | if (ret) |
| 2090 | return ret; |
| 2091 | } |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2092 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2093 | return __rbd_img_fill_request(img_req); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2094 | } |
| 2095 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2096 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, |
| 2097 | u64 off, u64 len) |
| 2098 | { |
| 2099 | struct ceph_file_extent ex = { off, len }; |
| 2100 | union rbd_img_fill_iter dummy; |
| 2101 | struct rbd_img_fill_ctx fctx = { |
| 2102 | .pos_type = OBJ_REQUEST_NODATA, |
| 2103 | .pos = &dummy, |
| 2104 | }; |
| 2105 | |
| 2106 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); |
| 2107 | } |
| 2108 | |
| 2109 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2110 | { |
| 2111 | struct rbd_obj_request *obj_req = |
| 2112 | container_of(ex, struct rbd_obj_request, ex); |
| 2113 | struct ceph_bio_iter *it = arg; |
| 2114 | |
| 2115 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2116 | obj_req->bio_pos = *it; |
| 2117 | ceph_bio_iter_advance(it, bytes); |
| 2118 | } |
| 2119 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2120 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2121 | { |
| 2122 | struct rbd_obj_request *obj_req = |
| 2123 | container_of(ex, struct rbd_obj_request, ex); |
| 2124 | struct ceph_bio_iter *it = arg; |
| 2125 | |
| 2126 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2127 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2128 | obj_req->bvec_count++; |
| 2129 | })); |
| 2130 | |
| 2131 | } |
| 2132 | |
| 2133 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2134 | { |
| 2135 | struct rbd_obj_request *obj_req = |
| 2136 | container_of(ex, struct rbd_obj_request, ex); |
| 2137 | struct ceph_bio_iter *it = arg; |
| 2138 | |
| 2139 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2140 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2141 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2142 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2143 | })); |
| 2144 | } |
| 2145 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2146 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2147 | struct ceph_file_extent *img_extents, |
| 2148 | u32 num_img_extents, |
| 2149 | struct ceph_bio_iter *bio_pos) |
| 2150 | { |
| 2151 | struct rbd_img_fill_ctx fctx = { |
| 2152 | .pos_type = OBJ_REQUEST_BIO, |
| 2153 | .pos = (union rbd_img_fill_iter *)bio_pos, |
| 2154 | .set_pos_fn = set_bio_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2155 | .count_fn = count_bio_bvecs, |
| 2156 | .copy_fn = copy_bio_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2157 | }; |
| 2158 | |
| 2159 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2160 | &fctx); |
| 2161 | } |
| 2162 | |
| 2163 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2164 | u64 off, u64 len, struct bio *bio) |
| 2165 | { |
| 2166 | struct ceph_file_extent ex = { off, len }; |
| 2167 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; |
| 2168 | |
| 2169 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
| 2170 | } |
| 2171 | |
| 2172 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2173 | { |
| 2174 | struct rbd_obj_request *obj_req = |
| 2175 | container_of(ex, struct rbd_obj_request, ex); |
| 2176 | struct ceph_bvec_iter *it = arg; |
| 2177 | |
| 2178 | obj_req->bvec_pos = *it; |
| 2179 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); |
| 2180 | ceph_bvec_iter_advance(it, bytes); |
| 2181 | } |
| 2182 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2183 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2184 | { |
| 2185 | struct rbd_obj_request *obj_req = |
| 2186 | container_of(ex, struct rbd_obj_request, ex); |
| 2187 | struct ceph_bvec_iter *it = arg; |
| 2188 | |
| 2189 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2190 | obj_req->bvec_count++; |
| 2191 | })); |
| 2192 | } |
| 2193 | |
| 2194 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2195 | { |
| 2196 | struct rbd_obj_request *obj_req = |
| 2197 | container_of(ex, struct rbd_obj_request, ex); |
| 2198 | struct ceph_bvec_iter *it = arg; |
| 2199 | |
| 2200 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2201 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2202 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2203 | })); |
| 2204 | } |
| 2205 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2206 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2207 | struct ceph_file_extent *img_extents, |
| 2208 | u32 num_img_extents, |
| 2209 | struct ceph_bvec_iter *bvec_pos) |
| 2210 | { |
| 2211 | struct rbd_img_fill_ctx fctx = { |
| 2212 | .pos_type = OBJ_REQUEST_BVECS, |
| 2213 | .pos = (union rbd_img_fill_iter *)bvec_pos, |
| 2214 | .set_pos_fn = set_bvec_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2215 | .count_fn = count_bvecs, |
| 2216 | .copy_fn = copy_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2217 | }; |
| 2218 | |
| 2219 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2220 | &fctx); |
| 2221 | } |
| 2222 | |
| 2223 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2224 | struct ceph_file_extent *img_extents, |
| 2225 | u32 num_img_extents, |
| 2226 | struct bio_vec *bvecs) |
| 2227 | { |
| 2228 | struct ceph_bvec_iter it = { |
| 2229 | .bvecs = bvecs, |
| 2230 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, |
| 2231 | num_img_extents) }, |
| 2232 | }; |
| 2233 | |
| 2234 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
| 2235 | &it); |
| 2236 | } |
| 2237 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2238 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2239 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2240 | struct rbd_obj_request *obj_request; |
| 2241 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2242 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2243 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2244 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2245 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2246 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2247 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2248 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2249 | } |
| 2250 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2251 | static int rbd_obj_read_from_parent(struct rbd_obj_request *obj_req) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2252 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2253 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2254 | struct rbd_img_request *child_img_req; |
| 2255 | int ret; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2256 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2257 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
| 2258 | OBJ_OP_READ, NULL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2259 | if (!child_img_req) |
| 2260 | return -ENOMEM; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2261 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2262 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
| 2263 | child_img_req->obj_request = obj_req; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2264 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2265 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2266 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2267 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2268 | ret = __rbd_img_fill_from_bio(child_img_req, |
| 2269 | obj_req->img_extents, |
| 2270 | obj_req->num_img_extents, |
| 2271 | &obj_req->bio_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2272 | break; |
| 2273 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2274 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2275 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
| 2276 | obj_req->img_extents, |
| 2277 | obj_req->num_img_extents, |
| 2278 | &obj_req->bvec_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2279 | break; |
| 2280 | default: |
| 2281 | rbd_assert(0); |
| 2282 | } |
| 2283 | } else { |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2284 | ret = rbd_img_fill_from_bvecs(child_img_req, |
| 2285 | obj_req->img_extents, |
| 2286 | obj_req->num_img_extents, |
| 2287 | obj_req->copyup_bvecs); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2288 | } |
| 2289 | if (ret) { |
| 2290 | rbd_img_request_put(child_img_req); |
| 2291 | return ret; |
| 2292 | } |
| 2293 | |
| 2294 | rbd_img_request_submit(child_img_req); |
| 2295 | return 0; |
| 2296 | } |
| 2297 | |
| 2298 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2299 | { |
| 2300 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2301 | int ret; |
| 2302 | |
| 2303 | if (obj_req->result == -ENOENT && |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2304 | rbd_dev->parent_overlap && !obj_req->tried_parent) { |
| 2305 | /* reverse map this object extent onto the parent */ |
| 2306 | ret = rbd_obj_calc_img_extents(obj_req, false); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2307 | if (ret) { |
| 2308 | obj_req->result = ret; |
| 2309 | return true; |
| 2310 | } |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2311 | |
| 2312 | if (obj_req->num_img_extents) { |
| 2313 | obj_req->tried_parent = true; |
| 2314 | ret = rbd_obj_read_from_parent(obj_req); |
| 2315 | if (ret) { |
| 2316 | obj_req->result = ret; |
| 2317 | return true; |
| 2318 | } |
| 2319 | return false; |
| 2320 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2321 | } |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2322 | |
| 2323 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2324 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2325 | * length of the request. A short read also implies zero-fill |
| 2326 | * to the end of the request. In both cases we update xferred |
| 2327 | * count to indicate the whole request was satisfied. |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2328 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2329 | if (obj_req->result == -ENOENT || |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2330 | (!obj_req->result && obj_req->xferred < obj_req->ex.oe_len)) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2331 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2332 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2333 | obj_req->ex.oe_len - obj_req->xferred); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2334 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2335 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
| 2341 | /* |
| 2342 | * copyup_bvecs pages are never highmem pages |
| 2343 | */ |
| 2344 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2345 | { |
| 2346 | struct ceph_bvec_iter it = { |
| 2347 | .bvecs = bvecs, |
| 2348 | .iter = { .bi_size = bytes }, |
| 2349 | }; |
| 2350 | |
| 2351 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2352 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2353 | bv.bv_len)) |
| 2354 | return false; |
| 2355 | })); |
| 2356 | return true; |
| 2357 | } |
| 2358 | |
| 2359 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2360 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2361 | unsigned int num_osd_ops = obj_req->osd_req->r_num_ops; |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2362 | int ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2363 | |
| 2364 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2365 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
| 2366 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2367 | |
| 2368 | /* |
| 2369 | * Create a copyup request with the same number of OSD ops as |
| 2370 | * the original request. The original request was stat + op(s), |
| 2371 | * the new copyup request will be copyup + the same op(s). |
| 2372 | */ |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2373 | 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] | 2374 | if (!obj_req->osd_req) |
| 2375 | return -ENOMEM; |
| 2376 | |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2377 | ret = osd_req_op_cls_init(obj_req->osd_req, 0, CEPH_OSD_OP_CALL, "rbd", |
| 2378 | "copyup"); |
| 2379 | if (ret) |
| 2380 | return ret; |
| 2381 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2382 | /* |
| 2383 | * Only send non-zero copyup data to save some I/O and network |
| 2384 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2385 | * existing. |
| 2386 | */ |
| 2387 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2388 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2389 | bytes = 0; |
| 2390 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2391 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
Ilya Dryomov | 0010f70 | 2018-05-04 16:57:30 +0200 | [diff] [blame] | 2392 | obj_req->copyup_bvecs, |
| 2393 | obj_req->copyup_bvec_count, |
| 2394 | bytes); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2395 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2396 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2397 | case OBJ_OP_WRITE: |
| 2398 | __rbd_obj_setup_write(obj_req, 1); |
| 2399 | break; |
| 2400 | case OBJ_OP_DISCARD: |
| 2401 | rbd_assert(!rbd_obj_is_entire(obj_req)); |
| 2402 | __rbd_obj_setup_discard(obj_req, 1); |
| 2403 | break; |
| 2404 | default: |
| 2405 | rbd_assert(0); |
| 2406 | } |
| 2407 | |
| 2408 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2409 | return 0; |
| 2410 | } |
| 2411 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2412 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2413 | { |
| 2414 | u32 i; |
| 2415 | |
| 2416 | rbd_assert(!obj_req->copyup_bvecs); |
| 2417 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2418 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2419 | sizeof(*obj_req->copyup_bvecs), |
| 2420 | GFP_NOIO); |
| 2421 | if (!obj_req->copyup_bvecs) |
| 2422 | return -ENOMEM; |
| 2423 | |
| 2424 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2425 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2426 | |
| 2427 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2428 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2429 | return -ENOMEM; |
| 2430 | |
| 2431 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2432 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2433 | obj_overlap -= len; |
| 2434 | } |
| 2435 | |
| 2436 | rbd_assert(!obj_overlap); |
| 2437 | return 0; |
| 2438 | } |
| 2439 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2440 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2441 | { |
| 2442 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2443 | int ret; |
| 2444 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2445 | rbd_assert(obj_req->num_img_extents); |
| 2446 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 2447 | rbd_dev->parent_overlap); |
| 2448 | if (!obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2449 | /* |
| 2450 | * The overlap has become 0 (most likely because the |
| 2451 | * image has been flattened). Use rbd_obj_issue_copyup() |
| 2452 | * to re-submit the original write request -- the copyup |
| 2453 | * operation itself will be a no-op, since someone must |
| 2454 | * have populated the child object while we weren't |
| 2455 | * looking. Move to WRITE_FLAT state as we'll be done |
| 2456 | * with the operation once the null copyup completes. |
| 2457 | */ |
| 2458 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2459 | return rbd_obj_issue_copyup(obj_req, 0); |
| 2460 | } |
| 2461 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2462 | 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] | 2463 | if (ret) |
| 2464 | return ret; |
| 2465 | |
| 2466 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2467 | return rbd_obj_read_from_parent(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2471 | { |
| 2472 | int ret; |
| 2473 | |
| 2474 | again: |
| 2475 | switch (obj_req->write_state) { |
| 2476 | case RBD_OBJ_WRITE_GUARD: |
| 2477 | rbd_assert(!obj_req->xferred); |
| 2478 | if (obj_req->result == -ENOENT) { |
| 2479 | /* |
| 2480 | * The target object doesn't exist. Read the data for |
| 2481 | * the entire target object up to the overlap point (if |
| 2482 | * any) from the parent, so we can use it for a copyup. |
| 2483 | */ |
| 2484 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2485 | if (ret) { |
| 2486 | obj_req->result = ret; |
| 2487 | return true; |
| 2488 | } |
| 2489 | return false; |
| 2490 | } |
| 2491 | /* fall through */ |
| 2492 | case RBD_OBJ_WRITE_FLAT: |
| 2493 | if (!obj_req->result) |
| 2494 | /* |
| 2495 | * There is no such thing as a successful short |
| 2496 | * write -- indicate the whole request was satisfied. |
| 2497 | */ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2498 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2499 | return true; |
| 2500 | case RBD_OBJ_WRITE_COPYUP: |
| 2501 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2502 | if (obj_req->result) |
| 2503 | goto again; |
| 2504 | |
| 2505 | rbd_assert(obj_req->xferred); |
| 2506 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2507 | if (ret) { |
| 2508 | obj_req->result = ret; |
| 2509 | return true; |
| 2510 | } |
| 2511 | return false; |
| 2512 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2513 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | /* |
| 2518 | * Returns true if @obj_req is completed, or false otherwise. |
| 2519 | */ |
| 2520 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2521 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2522 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2523 | case OBJ_OP_READ: |
| 2524 | return rbd_obj_handle_read(obj_req); |
| 2525 | case OBJ_OP_WRITE: |
| 2526 | return rbd_obj_handle_write(obj_req); |
| 2527 | case OBJ_OP_DISCARD: |
| 2528 | if (rbd_obj_handle_write(obj_req)) { |
| 2529 | /* |
| 2530 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2531 | * a non-existent object is not a problem. |
| 2532 | */ |
| 2533 | if (obj_req->result == -ENOENT) { |
| 2534 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2535 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2536 | } |
| 2537 | return true; |
| 2538 | } |
| 2539 | return false; |
| 2540 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2541 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2542 | } |
| 2543 | } |
| 2544 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2545 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2546 | { |
| 2547 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2548 | |
| 2549 | rbd_assert((!obj_req->result && |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2550 | obj_req->xferred == obj_req->ex.oe_len) || |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2551 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2552 | if (!obj_req->result) { |
| 2553 | img_req->xferred += obj_req->xferred; |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2554 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2555 | } |
| 2556 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2557 | rbd_warn(img_req->rbd_dev, |
| 2558 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2559 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, |
| 2560 | 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] | 2561 | obj_req->xferred); |
| 2562 | if (!img_req->result) { |
| 2563 | img_req->result = obj_req->result; |
| 2564 | img_req->xferred = 0; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2565 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2566 | } |
| 2567 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2568 | static void rbd_img_end_child_request(struct rbd_img_request *img_req) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2569 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2570 | struct rbd_obj_request *obj_req = img_req->obj_request; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2571 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2572 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2573 | rbd_assert((!img_req->result && |
| 2574 | img_req->xferred == rbd_obj_img_extents_bytes(obj_req)) || |
| 2575 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2576 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2577 | obj_req->result = img_req->result; |
| 2578 | obj_req->xferred = img_req->xferred; |
| 2579 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2580 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2581 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2582 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2583 | { |
| 2584 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2585 | rbd_assert((!img_req->result && |
| 2586 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2587 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2588 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2589 | blk_mq_end_request(img_req->rq, |
| 2590 | errno_to_blk_status(img_req->result)); |
| 2591 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2592 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2593 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2594 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2595 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2596 | struct rbd_img_request *img_req; |
| 2597 | |
| 2598 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2599 | if (!__rbd_obj_handle_request(obj_req)) |
| 2600 | return; |
| 2601 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2602 | img_req = obj_req->img_request; |
| 2603 | spin_lock(&img_req->completion_lock); |
| 2604 | rbd_obj_end_request(obj_req); |
| 2605 | rbd_assert(img_req->pending_count); |
| 2606 | if (--img_req->pending_count) { |
| 2607 | spin_unlock(&img_req->completion_lock); |
| 2608 | return; |
| 2609 | } |
| 2610 | |
| 2611 | spin_unlock(&img_req->completion_lock); |
| 2612 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2613 | obj_req = img_req->obj_request; |
| 2614 | rbd_img_end_child_request(img_req); |
| 2615 | goto again; |
| 2616 | } |
| 2617 | rbd_img_end_request(img_req); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2618 | } |
| 2619 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2620 | static const struct rbd_client_id rbd_empty_cid; |
| 2621 | |
| 2622 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2623 | const struct rbd_client_id *rhs) |
| 2624 | { |
| 2625 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2626 | } |
| 2627 | |
| 2628 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2629 | { |
| 2630 | struct rbd_client_id cid; |
| 2631 | |
| 2632 | mutex_lock(&rbd_dev->watch_mutex); |
| 2633 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2634 | cid.handle = rbd_dev->watch_cookie; |
| 2635 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2636 | return cid; |
| 2637 | } |
| 2638 | |
| 2639 | /* |
| 2640 | * lock_rwsem must be held for write |
| 2641 | */ |
| 2642 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2643 | const struct rbd_client_id *cid) |
| 2644 | { |
| 2645 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2646 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2647 | cid->gid, cid->handle); |
| 2648 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2649 | } |
| 2650 | |
| 2651 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2652 | { |
| 2653 | mutex_lock(&rbd_dev->watch_mutex); |
| 2654 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2655 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2656 | } |
| 2657 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2658 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2659 | { |
| 2660 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2661 | |
| 2662 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2663 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2664 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2665 | } |
| 2666 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2667 | /* |
| 2668 | * lock_rwsem must be held for write |
| 2669 | */ |
| 2670 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2671 | { |
| 2672 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2673 | char cookie[32]; |
| 2674 | int ret; |
| 2675 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2676 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2677 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2678 | |
| 2679 | format_lock_cookie(rbd_dev, cookie); |
| 2680 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2681 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2682 | RBD_LOCK_TAG, "", 0); |
| 2683 | if (ret) |
| 2684 | return ret; |
| 2685 | |
| 2686 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2687 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2688 | return 0; |
| 2689 | } |
| 2690 | |
| 2691 | /* |
| 2692 | * lock_rwsem must be held for write |
| 2693 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2694 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2695 | { |
| 2696 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2697 | int ret; |
| 2698 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2699 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2700 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2701 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2702 | 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] | 2703 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2704 | if (ret && ret != -ENOENT) |
| 2705 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2706 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2707 | /* treat errors as the image is unlocked */ |
| 2708 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2709 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2710 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2711 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2715 | enum rbd_notify_op notify_op, |
| 2716 | struct page ***preply_pages, |
| 2717 | size_t *preply_len) |
| 2718 | { |
| 2719 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2720 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 2721 | char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN]; |
| 2722 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2723 | void *p = buf; |
| 2724 | |
| 2725 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2726 | |
| 2727 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2728 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2729 | ceph_encode_32(&p, notify_op); |
| 2730 | ceph_encode_64(&p, cid.gid); |
| 2731 | ceph_encode_64(&p, cid.handle); |
| 2732 | |
| 2733 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2734 | &rbd_dev->header_oloc, buf, buf_size, |
| 2735 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2736 | } |
| 2737 | |
| 2738 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2739 | enum rbd_notify_op notify_op) |
| 2740 | { |
| 2741 | struct page **reply_pages; |
| 2742 | size_t reply_len; |
| 2743 | |
| 2744 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2745 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2746 | } |
| 2747 | |
| 2748 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2749 | { |
| 2750 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2751 | acquired_lock_work); |
| 2752 | |
| 2753 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2754 | } |
| 2755 | |
| 2756 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2757 | { |
| 2758 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2759 | released_lock_work); |
| 2760 | |
| 2761 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2762 | } |
| 2763 | |
| 2764 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2765 | { |
| 2766 | struct page **reply_pages; |
| 2767 | size_t reply_len; |
| 2768 | bool lock_owner_responded = false; |
| 2769 | int ret; |
| 2770 | |
| 2771 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2772 | |
| 2773 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2774 | &reply_pages, &reply_len); |
| 2775 | if (ret && ret != -ETIMEDOUT) { |
| 2776 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2777 | goto out; |
| 2778 | } |
| 2779 | |
| 2780 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2781 | void *p = page_address(reply_pages[0]); |
| 2782 | void *const end = p + reply_len; |
| 2783 | u32 n; |
| 2784 | |
| 2785 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2786 | while (n--) { |
| 2787 | u8 struct_v; |
| 2788 | u32 len; |
| 2789 | |
| 2790 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2791 | p += 8 + 8; /* skip gid and cookie */ |
| 2792 | |
| 2793 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2794 | if (!len) |
| 2795 | continue; |
| 2796 | |
| 2797 | if (lock_owner_responded) { |
| 2798 | rbd_warn(rbd_dev, |
| 2799 | "duplicate lock owners detected"); |
| 2800 | ret = -EIO; |
| 2801 | goto out; |
| 2802 | } |
| 2803 | |
| 2804 | lock_owner_responded = true; |
| 2805 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2806 | &struct_v, &len); |
| 2807 | if (ret) { |
| 2808 | rbd_warn(rbd_dev, |
| 2809 | "failed to decode ResponseMessage: %d", |
| 2810 | ret); |
| 2811 | goto e_inval; |
| 2812 | } |
| 2813 | |
| 2814 | ret = ceph_decode_32(&p); |
| 2815 | } |
| 2816 | } |
| 2817 | |
| 2818 | if (!lock_owner_responded) { |
| 2819 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 2820 | ret = -ETIMEDOUT; |
| 2821 | } |
| 2822 | |
| 2823 | out: |
| 2824 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2825 | return ret; |
| 2826 | |
| 2827 | e_inval: |
| 2828 | ret = -EINVAL; |
| 2829 | goto out; |
| 2830 | } |
| 2831 | |
| 2832 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 2833 | { |
| 2834 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 2835 | |
| 2836 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 2837 | if (wake_all) |
| 2838 | wake_up_all(&rbd_dev->lock_waitq); |
| 2839 | else |
| 2840 | wake_up(&rbd_dev->lock_waitq); |
| 2841 | } |
| 2842 | |
| 2843 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 2844 | struct ceph_locker **lockers, u32 *num_lockers) |
| 2845 | { |
| 2846 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2847 | u8 lock_type; |
| 2848 | char *lock_tag; |
| 2849 | int ret; |
| 2850 | |
| 2851 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2852 | |
| 2853 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 2854 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2855 | &lock_type, &lock_tag, lockers, num_lockers); |
| 2856 | if (ret) |
| 2857 | return ret; |
| 2858 | |
| 2859 | if (*num_lockers == 0) { |
| 2860 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 2861 | goto out; |
| 2862 | } |
| 2863 | |
| 2864 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 2865 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 2866 | lock_tag); |
| 2867 | ret = -EBUSY; |
| 2868 | goto out; |
| 2869 | } |
| 2870 | |
| 2871 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 2872 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 2873 | ret = -EBUSY; |
| 2874 | goto out; |
| 2875 | } |
| 2876 | |
| 2877 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 2878 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 2879 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 2880 | (*lockers)[0].id.cookie); |
| 2881 | ret = -EBUSY; |
| 2882 | goto out; |
| 2883 | } |
| 2884 | |
| 2885 | out: |
| 2886 | kfree(lock_tag); |
| 2887 | return ret; |
| 2888 | } |
| 2889 | |
| 2890 | static int find_watcher(struct rbd_device *rbd_dev, |
| 2891 | const struct ceph_locker *locker) |
| 2892 | { |
| 2893 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2894 | struct ceph_watch_item *watchers; |
| 2895 | u32 num_watchers; |
| 2896 | u64 cookie; |
| 2897 | int i; |
| 2898 | int ret; |
| 2899 | |
| 2900 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 2901 | &rbd_dev->header_oloc, &watchers, |
| 2902 | &num_watchers); |
| 2903 | if (ret) |
| 2904 | return ret; |
| 2905 | |
| 2906 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 2907 | for (i = 0; i < num_watchers; i++) { |
| 2908 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 2909 | sizeof(locker->info.addr)) && |
| 2910 | watchers[i].cookie == cookie) { |
| 2911 | struct rbd_client_id cid = { |
| 2912 | .gid = le64_to_cpu(watchers[i].name.num), |
| 2913 | .handle = cookie, |
| 2914 | }; |
| 2915 | |
| 2916 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 2917 | rbd_dev, cid.gid, cid.handle); |
| 2918 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2919 | ret = 1; |
| 2920 | goto out; |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 2925 | ret = 0; |
| 2926 | out: |
| 2927 | kfree(watchers); |
| 2928 | return ret; |
| 2929 | } |
| 2930 | |
| 2931 | /* |
| 2932 | * lock_rwsem must be held for write |
| 2933 | */ |
| 2934 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 2935 | { |
| 2936 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 2937 | struct ceph_locker *lockers; |
| 2938 | u32 num_lockers; |
| 2939 | int ret; |
| 2940 | |
| 2941 | for (;;) { |
| 2942 | ret = rbd_lock(rbd_dev); |
| 2943 | if (ret != -EBUSY) |
| 2944 | return ret; |
| 2945 | |
| 2946 | /* determine if the current lock holder is still alive */ |
| 2947 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 2948 | if (ret) |
| 2949 | return ret; |
| 2950 | |
| 2951 | if (num_lockers == 0) |
| 2952 | goto again; |
| 2953 | |
| 2954 | ret = find_watcher(rbd_dev, lockers); |
| 2955 | if (ret) { |
| 2956 | if (ret > 0) |
| 2957 | ret = 0; /* have to request lock */ |
| 2958 | goto out; |
| 2959 | } |
| 2960 | |
| 2961 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 2962 | ENTITY_NAME(lockers[0].id.name)); |
| 2963 | |
| 2964 | ret = ceph_monc_blacklist_add(&client->monc, |
| 2965 | &lockers[0].info.addr); |
| 2966 | if (ret) { |
| 2967 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 2968 | ENTITY_NAME(lockers[0].id.name), ret); |
| 2969 | goto out; |
| 2970 | } |
| 2971 | |
| 2972 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 2973 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2974 | lockers[0].id.cookie, |
| 2975 | &lockers[0].id.name); |
| 2976 | if (ret && ret != -ENOENT) |
| 2977 | goto out; |
| 2978 | |
| 2979 | again: |
| 2980 | ceph_free_lockers(lockers, num_lockers); |
| 2981 | } |
| 2982 | |
| 2983 | out: |
| 2984 | ceph_free_lockers(lockers, num_lockers); |
| 2985 | return ret; |
| 2986 | } |
| 2987 | |
| 2988 | /* |
| 2989 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 2990 | */ |
| 2991 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 2992 | int *pret) |
| 2993 | { |
| 2994 | enum rbd_lock_state lock_state; |
| 2995 | |
| 2996 | down_read(&rbd_dev->lock_rwsem); |
| 2997 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 2998 | rbd_dev->lock_state); |
| 2999 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3000 | lock_state = rbd_dev->lock_state; |
| 3001 | up_read(&rbd_dev->lock_rwsem); |
| 3002 | return lock_state; |
| 3003 | } |
| 3004 | |
| 3005 | up_read(&rbd_dev->lock_rwsem); |
| 3006 | down_write(&rbd_dev->lock_rwsem); |
| 3007 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3008 | rbd_dev->lock_state); |
| 3009 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3010 | *pret = rbd_try_lock(rbd_dev); |
| 3011 | if (*pret) |
| 3012 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3013 | } |
| 3014 | |
| 3015 | lock_state = rbd_dev->lock_state; |
| 3016 | up_write(&rbd_dev->lock_rwsem); |
| 3017 | return lock_state; |
| 3018 | } |
| 3019 | |
| 3020 | static void rbd_acquire_lock(struct work_struct *work) |
| 3021 | { |
| 3022 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3023 | struct rbd_device, lock_dwork); |
| 3024 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3025 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3026 | |
| 3027 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3028 | again: |
| 3029 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3030 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3031 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3032 | wake_requests(rbd_dev, true); |
| 3033 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3034 | rbd_dev, lock_state, ret); |
| 3035 | return; |
| 3036 | } |
| 3037 | |
| 3038 | ret = rbd_request_lock(rbd_dev); |
| 3039 | if (ret == -ETIMEDOUT) { |
| 3040 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3041 | } else if (ret == -EROFS) { |
| 3042 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3043 | /* |
| 3044 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3045 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3046 | * want to block. |
| 3047 | */ |
| 3048 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3049 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3050 | /* wake "rbd map --exclusive" process */ |
| 3051 | wake_requests(rbd_dev, false); |
| 3052 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3053 | } else if (ret < 0) { |
| 3054 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3055 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3056 | RBD_RETRY_DELAY); |
| 3057 | } else { |
| 3058 | /* |
| 3059 | * lock owner acked, but resend if we don't see them |
| 3060 | * release the lock |
| 3061 | */ |
| 3062 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3063 | rbd_dev); |
| 3064 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3065 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3066 | } |
| 3067 | } |
| 3068 | |
| 3069 | /* |
| 3070 | * lock_rwsem must be held for write |
| 3071 | */ |
| 3072 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3073 | { |
| 3074 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3075 | rbd_dev->lock_state); |
| 3076 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3077 | return false; |
| 3078 | |
| 3079 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3080 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3081 | /* |
| 3082 | * Ensure that all in-flight IO is flushed. |
| 3083 | * |
| 3084 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3085 | * may be shared with other devices. |
| 3086 | */ |
| 3087 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3088 | up_read(&rbd_dev->lock_rwsem); |
| 3089 | |
| 3090 | down_write(&rbd_dev->lock_rwsem); |
| 3091 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3092 | rbd_dev->lock_state); |
| 3093 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3094 | return false; |
| 3095 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3096 | rbd_unlock(rbd_dev); |
| 3097 | /* |
| 3098 | * Give others a chance to grab the lock - we would re-acquire |
| 3099 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3100 | * otherwise. We need to ack our own notifications, so this |
| 3101 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3102 | * after wake_requests() in rbd_handle_released_lock(). |
| 3103 | */ |
| 3104 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3105 | return true; |
| 3106 | } |
| 3107 | |
| 3108 | static void rbd_release_lock_work(struct work_struct *work) |
| 3109 | { |
| 3110 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3111 | unlock_work); |
| 3112 | |
| 3113 | down_write(&rbd_dev->lock_rwsem); |
| 3114 | rbd_release_lock(rbd_dev); |
| 3115 | up_write(&rbd_dev->lock_rwsem); |
| 3116 | } |
| 3117 | |
| 3118 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3119 | void **p) |
| 3120 | { |
| 3121 | struct rbd_client_id cid = { 0 }; |
| 3122 | |
| 3123 | if (struct_v >= 2) { |
| 3124 | cid.gid = ceph_decode_64(p); |
| 3125 | cid.handle = ceph_decode_64(p); |
| 3126 | } |
| 3127 | |
| 3128 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3129 | cid.handle); |
| 3130 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3131 | down_write(&rbd_dev->lock_rwsem); |
| 3132 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3133 | /* |
| 3134 | * we already know that the remote client is |
| 3135 | * the owner |
| 3136 | */ |
| 3137 | up_write(&rbd_dev->lock_rwsem); |
| 3138 | return; |
| 3139 | } |
| 3140 | |
| 3141 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3142 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3143 | } else { |
| 3144 | down_read(&rbd_dev->lock_rwsem); |
| 3145 | } |
| 3146 | |
| 3147 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3148 | wake_requests(rbd_dev, false); |
| 3149 | up_read(&rbd_dev->lock_rwsem); |
| 3150 | } |
| 3151 | |
| 3152 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3153 | void **p) |
| 3154 | { |
| 3155 | struct rbd_client_id cid = { 0 }; |
| 3156 | |
| 3157 | if (struct_v >= 2) { |
| 3158 | cid.gid = ceph_decode_64(p); |
| 3159 | cid.handle = ceph_decode_64(p); |
| 3160 | } |
| 3161 | |
| 3162 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3163 | cid.handle); |
| 3164 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3165 | down_write(&rbd_dev->lock_rwsem); |
| 3166 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3167 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3168 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3169 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3170 | up_write(&rbd_dev->lock_rwsem); |
| 3171 | return; |
| 3172 | } |
| 3173 | |
| 3174 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3175 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3176 | } else { |
| 3177 | down_read(&rbd_dev->lock_rwsem); |
| 3178 | } |
| 3179 | |
| 3180 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3181 | wake_requests(rbd_dev, false); |
| 3182 | up_read(&rbd_dev->lock_rwsem); |
| 3183 | } |
| 3184 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3185 | /* |
| 3186 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3187 | * ResponseMessage is needed. |
| 3188 | */ |
| 3189 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3190 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3191 | { |
| 3192 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3193 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3194 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3195 | |
| 3196 | if (struct_v >= 2) { |
| 3197 | cid.gid = ceph_decode_64(p); |
| 3198 | cid.handle = ceph_decode_64(p); |
| 3199 | } |
| 3200 | |
| 3201 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3202 | cid.handle); |
| 3203 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3204 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3205 | |
| 3206 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3207 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3208 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3209 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3210 | goto out_unlock; |
| 3211 | |
| 3212 | /* |
| 3213 | * encode ResponseMessage(0) so the peer can detect |
| 3214 | * a missing owner |
| 3215 | */ |
| 3216 | result = 0; |
| 3217 | |
| 3218 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3219 | if (!rbd_dev->opts->exclusive) { |
| 3220 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3221 | __func__, rbd_dev); |
| 3222 | queue_work(rbd_dev->task_wq, |
| 3223 | &rbd_dev->unlock_work); |
| 3224 | } else { |
| 3225 | /* refuse to release the lock */ |
| 3226 | result = -EROFS; |
| 3227 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3228 | } |
| 3229 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3230 | |
| 3231 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3232 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3233 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3234 | } |
| 3235 | |
| 3236 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3237 | u64 notify_id, u64 cookie, s32 *result) |
| 3238 | { |
| 3239 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 3240 | char buf[4 + CEPH_ENCODING_START_BLK_LEN]; |
| 3241 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3242 | int ret; |
| 3243 | |
| 3244 | if (result) { |
| 3245 | void *p = buf; |
| 3246 | |
| 3247 | /* encode ResponseMessage */ |
| 3248 | ceph_start_encoding(&p, 1, 1, |
| 3249 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3250 | ceph_encode_32(&p, *result); |
| 3251 | } else { |
| 3252 | buf_size = 0; |
| 3253 | } |
| 3254 | |
| 3255 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3256 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3257 | buf, buf_size); |
| 3258 | if (ret) |
| 3259 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3260 | } |
| 3261 | |
| 3262 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3263 | u64 cookie) |
| 3264 | { |
| 3265 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3266 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3267 | } |
| 3268 | |
| 3269 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3270 | u64 notify_id, u64 cookie, s32 result) |
| 3271 | { |
| 3272 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3273 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3274 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3275 | |
| 3276 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3277 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3278 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3279 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3280 | void *p = data; |
| 3281 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3282 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3283 | u32 len; |
| 3284 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3285 | int ret; |
| 3286 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3287 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3288 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3289 | if (data_len) { |
| 3290 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3291 | &struct_v, &len); |
| 3292 | if (ret) { |
| 3293 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3294 | ret); |
| 3295 | return; |
| 3296 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3297 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3298 | notify_op = ceph_decode_32(&p); |
| 3299 | } else { |
| 3300 | /* legacy notification for header updates */ |
| 3301 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3302 | len = 0; |
| 3303 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3304 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3305 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3306 | switch (notify_op) { |
| 3307 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3308 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3309 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3310 | break; |
| 3311 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3312 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3313 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3314 | break; |
| 3315 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3316 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3317 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3318 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3319 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3320 | else |
| 3321 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3322 | break; |
| 3323 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3324 | ret = rbd_dev_refresh(rbd_dev); |
| 3325 | if (ret) |
| 3326 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3327 | |
| 3328 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3329 | break; |
| 3330 | default: |
| 3331 | if (rbd_is_lock_owner(rbd_dev)) |
| 3332 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3333 | cookie, -EOPNOTSUPP); |
| 3334 | else |
| 3335 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3336 | break; |
| 3337 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3338 | } |
| 3339 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3340 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3341 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3342 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3343 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3344 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3345 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3346 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3347 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3348 | down_write(&rbd_dev->lock_rwsem); |
| 3349 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3350 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3351 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3352 | mutex_lock(&rbd_dev->watch_mutex); |
| 3353 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3354 | __rbd_unregister_watch(rbd_dev); |
| 3355 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3356 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3357 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3358 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3359 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3363 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3364 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3365 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3366 | { |
| 3367 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3368 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3369 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3370 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3371 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3372 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3373 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3374 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3375 | rbd_watch_errcb, rbd_dev); |
| 3376 | if (IS_ERR(handle)) |
| 3377 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3378 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3379 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3380 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3381 | } |
| 3382 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3383 | /* |
| 3384 | * watch_mutex must be locked |
| 3385 | */ |
| 3386 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3387 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3388 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3389 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3390 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3391 | rbd_assert(rbd_dev->watch_handle); |
| 3392 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3393 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3394 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3395 | if (ret) |
| 3396 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3397 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3398 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3399 | } |
| 3400 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3401 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3402 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3403 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3404 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3405 | mutex_lock(&rbd_dev->watch_mutex); |
| 3406 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3407 | ret = __rbd_register_watch(rbd_dev); |
| 3408 | if (ret) |
| 3409 | goto out; |
| 3410 | |
| 3411 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3412 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3413 | |
| 3414 | out: |
| 3415 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3416 | return ret; |
| 3417 | } |
| 3418 | |
| 3419 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3420 | { |
| 3421 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3422 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3423 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3424 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3425 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3426 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3427 | } |
| 3428 | |
| 3429 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3430 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3431 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3432 | cancel_tasks_sync(rbd_dev); |
| 3433 | |
| 3434 | mutex_lock(&rbd_dev->watch_mutex); |
| 3435 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3436 | __rbd_unregister_watch(rbd_dev); |
| 3437 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3438 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3439 | |
Dongsheng Yang | 23edca8 | 2018-06-04 06:24:37 -0400 | [diff] [blame] | 3440 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3441 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3442 | } |
| 3443 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3444 | /* |
| 3445 | * lock_rwsem must be held for write |
| 3446 | */ |
| 3447 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3448 | { |
| 3449 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3450 | char cookie[32]; |
| 3451 | int ret; |
| 3452 | |
| 3453 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3454 | |
| 3455 | format_lock_cookie(rbd_dev, cookie); |
| 3456 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3457 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3458 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3459 | RBD_LOCK_TAG, cookie); |
| 3460 | if (ret) { |
| 3461 | if (ret != -EOPNOTSUPP) |
| 3462 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3463 | ret); |
| 3464 | |
| 3465 | /* |
| 3466 | * Lock cookie cannot be updated on older OSDs, so do |
| 3467 | * a manual release and queue an acquire. |
| 3468 | */ |
| 3469 | if (rbd_release_lock(rbd_dev)) |
| 3470 | queue_delayed_work(rbd_dev->task_wq, |
| 3471 | &rbd_dev->lock_dwork, 0); |
| 3472 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3473 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3477 | static void rbd_reregister_watch(struct work_struct *work) |
| 3478 | { |
| 3479 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3480 | struct rbd_device, watch_dwork); |
| 3481 | int ret; |
| 3482 | |
| 3483 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3484 | |
| 3485 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3486 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3487 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3488 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3489 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3490 | |
| 3491 | ret = __rbd_register_watch(rbd_dev); |
| 3492 | if (ret) { |
| 3493 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3494 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3495 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3496 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3497 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3498 | queue_delayed_work(rbd_dev->task_wq, |
| 3499 | &rbd_dev->watch_dwork, |
| 3500 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3501 | } |
| 3502 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3503 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3507 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3508 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3509 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3510 | down_write(&rbd_dev->lock_rwsem); |
| 3511 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3512 | rbd_reacquire_lock(rbd_dev); |
| 3513 | up_write(&rbd_dev->lock_rwsem); |
| 3514 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3515 | ret = rbd_dev_refresh(rbd_dev); |
| 3516 | if (ret) |
Colin Ian King | f6870cc | 2018-03-19 13:33:10 +0000 | [diff] [blame] | 3517 | rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3518 | } |
| 3519 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3520 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3521 | * Synchronous osd object method call. Returns the number of bytes |
| 3522 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3523 | */ |
| 3524 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3525 | struct ceph_object_id *oid, |
| 3526 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3527 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3528 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3529 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3530 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3531 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3532 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3533 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3534 | struct page *req_page = NULL; |
| 3535 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3536 | int ret; |
| 3537 | |
| 3538 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3539 | * Method calls are ultimately read operations. The result |
| 3540 | * should placed into the inbound buffer provided. They |
| 3541 | * also supply outbound data--parameters for the object |
| 3542 | * method. Currently if this is present it will be a |
| 3543 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3544 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3545 | if (outbound) { |
| 3546 | if (outbound_size > PAGE_SIZE) |
| 3547 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3548 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3549 | req_page = alloc_page(GFP_KERNEL); |
| 3550 | if (!req_page) |
| 3551 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3552 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3553 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3554 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3555 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3556 | reply_page = alloc_page(GFP_KERNEL); |
| 3557 | if (!reply_page) { |
| 3558 | if (req_page) |
| 3559 | __free_page(req_page); |
| 3560 | return -ENOMEM; |
| 3561 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3562 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3563 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3564 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3565 | reply_page, &inbound_size); |
| 3566 | if (!ret) { |
| 3567 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3568 | ret = inbound_size; |
| 3569 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3570 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3571 | if (req_page) |
| 3572 | __free_page(req_page); |
| 3573 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3574 | return ret; |
| 3575 | } |
| 3576 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3577 | /* |
| 3578 | * lock_rwsem must be held for read |
| 3579 | */ |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3580 | static int rbd_wait_state_locked(struct rbd_device *rbd_dev, bool may_acquire) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3581 | { |
| 3582 | DEFINE_WAIT(wait); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3583 | unsigned long timeout; |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3584 | int ret = 0; |
| 3585 | |
| 3586 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
| 3587 | return -EBLACKLISTED; |
| 3588 | |
| 3589 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3590 | return 0; |
| 3591 | |
| 3592 | if (!may_acquire) { |
| 3593 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3594 | return -EROFS; |
| 3595 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3596 | |
| 3597 | do { |
| 3598 | /* |
| 3599 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3600 | * and cancel_delayed_work() in wake_requests(). |
| 3601 | */ |
| 3602 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3603 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3604 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3605 | TASK_UNINTERRUPTIBLE); |
| 3606 | up_read(&rbd_dev->lock_rwsem); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3607 | timeout = schedule_timeout(ceph_timeout_jiffies( |
| 3608 | rbd_dev->opts->lock_timeout)); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3609 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3610 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3611 | ret = -EBLACKLISTED; |
| 3612 | break; |
| 3613 | } |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3614 | if (!timeout) { |
| 3615 | rbd_warn(rbd_dev, "timed out waiting for lock"); |
| 3616 | ret = -ETIMEDOUT; |
| 3617 | break; |
| 3618 | } |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3619 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3620 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3621 | finish_wait(&rbd_dev->lock_waitq, &wait); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3622 | return ret; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3623 | } |
| 3624 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3625 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3626 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3627 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3628 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3629 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3630 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3631 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3632 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3633 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3634 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3635 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3636 | int result; |
| 3637 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3638 | switch (req_op(rq)) { |
| 3639 | case REQ_OP_DISCARD: |
Ilya Dryomov | 6ac5695 | 2017-05-22 19:59:24 +0200 | [diff] [blame] | 3640 | case REQ_OP_WRITE_ZEROES: |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3641 | op_type = OBJ_OP_DISCARD; |
| 3642 | break; |
| 3643 | case REQ_OP_WRITE: |
| 3644 | op_type = OBJ_OP_WRITE; |
| 3645 | break; |
| 3646 | case REQ_OP_READ: |
| 3647 | op_type = OBJ_OP_READ; |
| 3648 | break; |
| 3649 | default: |
| 3650 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3651 | result = -EIO; |
| 3652 | goto err; |
| 3653 | } |
| 3654 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3655 | /* Ignore/skip any zero-length requests */ |
| 3656 | |
| 3657 | if (!length) { |
| 3658 | dout("%s: zero-length request\n", __func__); |
| 3659 | result = 0; |
| 3660 | goto err_rq; |
| 3661 | } |
| 3662 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3663 | rbd_assert(op_type == OBJ_OP_READ || |
| 3664 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3665 | |
| 3666 | /* |
| 3667 | * Quit early if the mapped snapshot no longer exists. It's |
| 3668 | * still possible the snapshot will have disappeared by the |
| 3669 | * time our request arrives at the osd, but there's no sense in |
| 3670 | * sending it if we already know. |
| 3671 | */ |
| 3672 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3673 | dout("request for non-existent snapshot"); |
| 3674 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3675 | result = -ENXIO; |
| 3676 | goto err_rq; |
| 3677 | } |
| 3678 | |
| 3679 | if (offset && length > U64_MAX - offset + 1) { |
| 3680 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3681 | length); |
| 3682 | result = -EINVAL; |
| 3683 | goto err_rq; /* Shouldn't happen */ |
| 3684 | } |
| 3685 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3686 | blk_mq_start_request(rq); |
| 3687 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3688 | down_read(&rbd_dev->header_rwsem); |
| 3689 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3690 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3691 | snapc = rbd_dev->header.snapc; |
| 3692 | ceph_get_snap_context(snapc); |
| 3693 | } |
| 3694 | up_read(&rbd_dev->header_rwsem); |
| 3695 | |
| 3696 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3697 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3698 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3699 | result = -EIO; |
| 3700 | goto err_rq; |
| 3701 | } |
| 3702 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3703 | must_be_locked = |
| 3704 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3705 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3706 | if (must_be_locked) { |
| 3707 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3708 | result = rbd_wait_state_locked(rbd_dev, |
| 3709 | !rbd_dev->opts->exclusive); |
| 3710 | if (result) |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3711 | goto err_unlock; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3712 | } |
| 3713 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 3714 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3715 | if (!img_request) { |
| 3716 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3717 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3718 | } |
| 3719 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3720 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3721 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3722 | if (op_type == OBJ_OP_DISCARD) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3723 | result = rbd_img_fill_nodata(img_request, offset, length); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3724 | else |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3725 | result = rbd_img_fill_from_bio(img_request, offset, length, |
| 3726 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3727 | if (result) |
| 3728 | goto err_img_request; |
| 3729 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3730 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3731 | if (must_be_locked) |
| 3732 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3733 | return; |
| 3734 | |
| 3735 | err_img_request: |
| 3736 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3737 | err_unlock: |
| 3738 | if (must_be_locked) |
| 3739 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3740 | err_rq: |
| 3741 | if (result) |
| 3742 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3743 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3744 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3745 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3746 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3747 | } |
| 3748 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3749 | 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] | 3750 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3751 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3752 | struct request *rq = bd->rq; |
| 3753 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3754 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3755 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3756 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3757 | } |
| 3758 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3759 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3760 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3761 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3762 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3763 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3764 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3767 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3768 | struct ceph_object_id *oid, |
| 3769 | struct ceph_object_locator *oloc, |
| 3770 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3771 | |
| 3772 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3773 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3774 | struct ceph_osd_request *req; |
| 3775 | struct page **pages; |
| 3776 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3777 | int ret; |
| 3778 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3779 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3780 | if (!req) |
| 3781 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3782 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3783 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3784 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3785 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3786 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3787 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3788 | if (ret) |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3789 | goto out_req; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3790 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3791 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3792 | if (IS_ERR(pages)) { |
| 3793 | ret = PTR_ERR(pages); |
| 3794 | goto out_req; |
| 3795 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3796 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3797 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3798 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3799 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3800 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3801 | ceph_osdc_start_request(osdc, req, false); |
| 3802 | ret = ceph_osdc_wait_request(osdc, req); |
| 3803 | if (ret >= 0) |
| 3804 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3805 | |
| 3806 | out_req: |
| 3807 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3808 | return ret; |
| 3809 | } |
| 3810 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3811 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3812 | * Read the complete header for the given rbd device. On successful |
| 3813 | * return, the rbd_dev->header field will contain up-to-date |
| 3814 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3815 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3816 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3817 | { |
| 3818 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 3819 | u32 snap_count = 0; |
| 3820 | u64 names_size = 0; |
| 3821 | u32 want_count; |
| 3822 | int ret; |
| 3823 | |
| 3824 | /* |
| 3825 | * The complete header will include an array of its 64-bit |
| 3826 | * snapshot ids, followed by the names of those snapshots as |
| 3827 | * a contiguous block of NUL-terminated strings. Note that |
| 3828 | * the number of snapshots could change by the time we read |
| 3829 | * it in, in which case we re-read it. |
| 3830 | */ |
| 3831 | do { |
| 3832 | size_t size; |
| 3833 | |
| 3834 | kfree(ondisk); |
| 3835 | |
| 3836 | size = sizeof (*ondisk); |
| 3837 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 3838 | size += names_size; |
| 3839 | ondisk = kmalloc(size, GFP_KERNEL); |
| 3840 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3841 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3842 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3843 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 3844 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3845 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3846 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 3847 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3848 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3849 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 3850 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3851 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3852 | } |
| 3853 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 3854 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3855 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3856 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 3860 | want_count = snap_count; |
| 3861 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 3862 | } while (snap_count != want_count); |
| 3863 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3864 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 3865 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3866 | kfree(ondisk); |
| 3867 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3868 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3869 | } |
| 3870 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3871 | /* |
| 3872 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 3873 | * has disappeared from the (just updated) snapshot context. |
| 3874 | */ |
| 3875 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 3876 | { |
| 3877 | u64 snap_id; |
| 3878 | |
| 3879 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 3880 | return; |
| 3881 | |
| 3882 | snap_id = rbd_dev->spec->snap_id; |
| 3883 | if (snap_id == CEPH_NOSNAP) |
| 3884 | return; |
| 3885 | |
| 3886 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 3887 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 3888 | } |
| 3889 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3890 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 3891 | { |
| 3892 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3893 | |
| 3894 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3895 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 3896 | * try to update its size. If REMOVING is set, updating size |
| 3897 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3898 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3899 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 3900 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3901 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 3902 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 3903 | set_capacity(rbd_dev->disk, size); |
| 3904 | revalidate_disk(rbd_dev->disk); |
| 3905 | } |
| 3906 | } |
| 3907 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3908 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3909 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3910 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3911 | int ret; |
| 3912 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3913 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 3914 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 3915 | |
| 3916 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3917 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3918 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3919 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3920 | /* |
| 3921 | * If there is a parent, see if it has disappeared due to the |
| 3922 | * mapped image getting flattened. |
| 3923 | */ |
| 3924 | if (rbd_dev->parent) { |
| 3925 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 3926 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3927 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 3928 | } |
| 3929 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3930 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3931 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 3932 | } else { |
| 3933 | /* validate mapped snapshot's EXISTS flag */ |
| 3934 | rbd_exists_validate(rbd_dev); |
| 3935 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3936 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3937 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 3938 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3939 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3940 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3941 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 3942 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3943 | } |
| 3944 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 3945 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 3946 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3947 | { |
| 3948 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 3949 | |
| 3950 | INIT_WORK(work, rbd_queue_workfn); |
| 3951 | return 0; |
| 3952 | } |
| 3953 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 3954 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3955 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3956 | .init_request = rbd_init_request, |
| 3957 | }; |
| 3958 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3959 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 3960 | { |
| 3961 | struct gendisk *disk; |
| 3962 | struct request_queue *q; |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 3963 | unsigned int objset_bytes = |
| 3964 | rbd_dev->layout.object_size * rbd_dev->layout.stripe_count; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3965 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3966 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3967 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3968 | disk = alloc_disk(single_major ? |
| 3969 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 3970 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3971 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 3972 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3973 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 3974 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 3975 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3976 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 3977 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 3978 | if (single_major) |
| 3979 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3980 | disk->fops = &rbd_bd_ops; |
| 3981 | disk->private_data = rbd_dev; |
| 3982 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3983 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 3984 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3985 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3986 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 3987 | 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] | 3988 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 3989 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 3990 | |
| 3991 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 3992 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3993 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 3994 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3995 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 3996 | if (IS_ERR(q)) { |
| 3997 | err = PTR_ERR(q); |
| 3998 | goto out_tag_set; |
| 3999 | } |
| 4000 | |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 4001 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4002 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4003 | |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4004 | blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4005 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 4006 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 4007 | blk_queue_max_segment_size(q, UINT_MAX); |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4008 | blk_queue_io_min(q, objset_bytes); |
| 4009 | blk_queue_io_opt(q, objset_bytes); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4010 | |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4011 | if (rbd_dev->opts->trim) { |
| 4012 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
| 4013 | q->limits.discard_granularity = objset_bytes; |
| 4014 | blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4015 | blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4016 | } |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4017 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4018 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4019 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4020 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4021 | /* |
| 4022 | * disk_release() expects a queue ref from add_disk() and will |
| 4023 | * put it. Hold an extra ref until add_disk() is called. |
| 4024 | */ |
| 4025 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4026 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4027 | q->queuedata = rbd_dev; |
| 4028 | |
| 4029 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4030 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4031 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4032 | out_tag_set: |
| 4033 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4034 | out_disk: |
| 4035 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4036 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4037 | } |
| 4038 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4039 | /* |
| 4040 | sysfs |
| 4041 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4042 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4043 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4044 | { |
| 4045 | return container_of(dev, struct rbd_device, dev); |
| 4046 | } |
| 4047 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4048 | static ssize_t rbd_size_show(struct device *dev, |
| 4049 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4050 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4051 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4052 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4053 | return sprintf(buf, "%llu\n", |
| 4054 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4055 | } |
| 4056 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4057 | /* |
| 4058 | * Note this shows the features for whatever's mapped, which is not |
| 4059 | * necessarily the base image. |
| 4060 | */ |
| 4061 | static ssize_t rbd_features_show(struct device *dev, |
| 4062 | struct device_attribute *attr, char *buf) |
| 4063 | { |
| 4064 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4065 | |
| 4066 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4067 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4068 | } |
| 4069 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4070 | static ssize_t rbd_major_show(struct device *dev, |
| 4071 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4072 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4073 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4074 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4075 | if (rbd_dev->major) |
| 4076 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4077 | |
| 4078 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4079 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4080 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4081 | static ssize_t rbd_minor_show(struct device *dev, |
| 4082 | struct device_attribute *attr, char *buf) |
| 4083 | { |
| 4084 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4085 | |
| 4086 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4087 | } |
| 4088 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4089 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4090 | struct device_attribute *attr, char *buf) |
| 4091 | { |
| 4092 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4093 | struct ceph_entity_addr *client_addr = |
| 4094 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4095 | |
| 4096 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4097 | le32_to_cpu(client_addr->nonce)); |
| 4098 | } |
| 4099 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4100 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4101 | struct device_attribute *attr, char *buf) |
| 4102 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4103 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4104 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4105 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4106 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4107 | } |
| 4108 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4109 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4110 | struct device_attribute *attr, char *buf) |
| 4111 | { |
| 4112 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4113 | |
| 4114 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4115 | } |
| 4116 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4117 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4118 | struct device_attribute *attr, char *buf) |
| 4119 | { |
| 4120 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4121 | |
| 4122 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4123 | } |
| 4124 | |
| 4125 | static ssize_t rbd_pool_show(struct device *dev, |
| 4126 | struct device_attribute *attr, char *buf) |
| 4127 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4128 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4129 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4130 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4131 | } |
| 4132 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4133 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4134 | struct device_attribute *attr, char *buf) |
| 4135 | { |
| 4136 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4137 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4138 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4139 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4140 | } |
| 4141 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4142 | static ssize_t rbd_pool_ns_show(struct device *dev, |
| 4143 | struct device_attribute *attr, char *buf) |
| 4144 | { |
| 4145 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4146 | |
| 4147 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: ""); |
| 4148 | } |
| 4149 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4150 | static ssize_t rbd_name_show(struct device *dev, |
| 4151 | struct device_attribute *attr, char *buf) |
| 4152 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4153 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4154 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4155 | if (rbd_dev->spec->image_name) |
| 4156 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4157 | |
| 4158 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4159 | } |
| 4160 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4161 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4162 | struct device_attribute *attr, char *buf) |
| 4163 | { |
| 4164 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4165 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4166 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4167 | } |
| 4168 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4169 | /* |
| 4170 | * Shows the name of the currently-mapped snapshot (or |
| 4171 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4172 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4173 | static ssize_t rbd_snap_show(struct device *dev, |
| 4174 | struct device_attribute *attr, |
| 4175 | char *buf) |
| 4176 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4177 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4178 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4179 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4180 | } |
| 4181 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4182 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4183 | struct device_attribute *attr, char *buf) |
| 4184 | { |
| 4185 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4186 | |
| 4187 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4188 | } |
| 4189 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4190 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4191 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4192 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4193 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4194 | */ |
| 4195 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4196 | struct device_attribute *attr, |
| 4197 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4198 | { |
| 4199 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4200 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4201 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4202 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4203 | return sprintf(buf, "(no parent image)\n"); |
| 4204 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4205 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4206 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4207 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4208 | count += sprintf(&buf[count], "%s" |
| 4209 | "pool_id %llu\npool_name %s\n" |
| 4210 | "image_id %s\nimage_name %s\n" |
| 4211 | "snap_id %llu\nsnap_name %s\n" |
| 4212 | "overlap %llu\n", |
| 4213 | !count ? "" : "\n", /* first? */ |
| 4214 | spec->pool_id, spec->pool_name, |
| 4215 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4216 | spec->snap_id, spec->snap_name, |
| 4217 | rbd_dev->parent_overlap); |
| 4218 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4219 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4220 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4221 | } |
| 4222 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4223 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4224 | struct device_attribute *attr, |
| 4225 | const char *buf, |
| 4226 | size_t size) |
| 4227 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4228 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4229 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4230 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4231 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4232 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4233 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4234 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4235 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4236 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4237 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4238 | static DEVICE_ATTR(size, 0444, rbd_size_show, NULL); |
| 4239 | static DEVICE_ATTR(features, 0444, rbd_features_show, NULL); |
| 4240 | static DEVICE_ATTR(major, 0444, rbd_major_show, NULL); |
| 4241 | static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL); |
| 4242 | static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL); |
| 4243 | static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL); |
| 4244 | static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL); |
| 4245 | static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL); |
| 4246 | static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL); |
| 4247 | static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4248 | static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL); |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4249 | static DEVICE_ATTR(name, 0444, rbd_name_show, NULL); |
| 4250 | static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL); |
| 4251 | static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh); |
| 4252 | static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL); |
| 4253 | static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL); |
| 4254 | static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4255 | |
| 4256 | static struct attribute *rbd_attrs[] = { |
| 4257 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4258 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4259 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4260 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4261 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4262 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4263 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4264 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4265 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4266 | &dev_attr_pool_id.attr, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4267 | &dev_attr_pool_ns.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4268 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4269 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4270 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4271 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4272 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4273 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4274 | NULL |
| 4275 | }; |
| 4276 | |
| 4277 | static struct attribute_group rbd_attr_group = { |
| 4278 | .attrs = rbd_attrs, |
| 4279 | }; |
| 4280 | |
| 4281 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4282 | &rbd_attr_group, |
| 4283 | NULL |
| 4284 | }; |
| 4285 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4286 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4287 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4288 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4289 | .name = "rbd", |
| 4290 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4291 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4292 | }; |
| 4293 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4294 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4295 | { |
| 4296 | kref_get(&spec->kref); |
| 4297 | |
| 4298 | return spec; |
| 4299 | } |
| 4300 | |
| 4301 | static void rbd_spec_free(struct kref *kref); |
| 4302 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4303 | { |
| 4304 | if (spec) |
| 4305 | kref_put(&spec->kref, rbd_spec_free); |
| 4306 | } |
| 4307 | |
| 4308 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4309 | { |
| 4310 | struct rbd_spec *spec; |
| 4311 | |
| 4312 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4313 | if (!spec) |
| 4314 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4315 | |
| 4316 | spec->pool_id = CEPH_NOPOOL; |
| 4317 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4318 | kref_init(&spec->kref); |
| 4319 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4320 | return spec; |
| 4321 | } |
| 4322 | |
| 4323 | static void rbd_spec_free(struct kref *kref) |
| 4324 | { |
| 4325 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4326 | |
| 4327 | kfree(spec->pool_name); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4328 | kfree(spec->pool_ns); |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4329 | kfree(spec->image_id); |
| 4330 | kfree(spec->image_name); |
| 4331 | kfree(spec->snap_name); |
| 4332 | kfree(spec); |
| 4333 | } |
| 4334 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4335 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4336 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4337 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4338 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4339 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4340 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4341 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4342 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4343 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4344 | rbd_put_client(rbd_dev->rbd_client); |
| 4345 | rbd_spec_put(rbd_dev->spec); |
| 4346 | kfree(rbd_dev->opts); |
| 4347 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static void rbd_dev_release(struct device *dev) |
| 4351 | { |
| 4352 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4353 | bool need_put = !!rbd_dev->opts; |
| 4354 | |
| 4355 | if (need_put) { |
| 4356 | destroy_workqueue(rbd_dev->task_wq); |
| 4357 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4358 | } |
| 4359 | |
| 4360 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4361 | |
| 4362 | /* |
| 4363 | * This is racy, but way better than putting module outside of |
| 4364 | * the release callback. The race window is pretty small, so |
| 4365 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4366 | */ |
| 4367 | if (need_put) |
| 4368 | module_put(THIS_MODULE); |
| 4369 | } |
| 4370 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4371 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4372 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4373 | { |
| 4374 | struct rbd_device *rbd_dev; |
| 4375 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4376 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4377 | if (!rbd_dev) |
| 4378 | return NULL; |
| 4379 | |
| 4380 | spin_lock_init(&rbd_dev->lock); |
| 4381 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4382 | init_rwsem(&rbd_dev->header_rwsem); |
| 4383 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4384 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4385 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4386 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4387 | if (spec->pool_ns) { |
| 4388 | WARN_ON(!*spec->pool_ns); |
| 4389 | rbd_dev->header_oloc.pool_ns = |
| 4390 | ceph_find_or_create_string(spec->pool_ns, |
| 4391 | strlen(spec->pool_ns)); |
| 4392 | } |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4393 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4394 | mutex_init(&rbd_dev->watch_mutex); |
| 4395 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4396 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4397 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4398 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4399 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4400 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4401 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4402 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4403 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4404 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4405 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4406 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4407 | rbd_dev->dev.type = &rbd_device_type; |
| 4408 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4409 | device_initialize(&rbd_dev->dev); |
| 4410 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4411 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4412 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4413 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4414 | return rbd_dev; |
| 4415 | } |
| 4416 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4417 | /* |
| 4418 | * Create a mapping rbd_dev. |
| 4419 | */ |
| 4420 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4421 | struct rbd_spec *spec, |
| 4422 | struct rbd_options *opts) |
| 4423 | { |
| 4424 | struct rbd_device *rbd_dev; |
| 4425 | |
| 4426 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4427 | if (!rbd_dev) |
| 4428 | return NULL; |
| 4429 | |
| 4430 | rbd_dev->opts = opts; |
| 4431 | |
| 4432 | /* get an id and fill in device name */ |
| 4433 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4434 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4435 | GFP_KERNEL); |
| 4436 | if (rbd_dev->dev_id < 0) |
| 4437 | goto fail_rbd_dev; |
| 4438 | |
| 4439 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4440 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4441 | rbd_dev->name); |
| 4442 | if (!rbd_dev->task_wq) |
| 4443 | goto fail_dev_id; |
| 4444 | |
| 4445 | /* we have a ref from do_rbd_add() */ |
| 4446 | __module_get(THIS_MODULE); |
| 4447 | |
| 4448 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4449 | return rbd_dev; |
| 4450 | |
| 4451 | fail_dev_id: |
| 4452 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4453 | fail_rbd_dev: |
| 4454 | rbd_dev_free(rbd_dev); |
| 4455 | return NULL; |
| 4456 | } |
| 4457 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4458 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4459 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4460 | if (rbd_dev) |
| 4461 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4462 | } |
| 4463 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4464 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4465 | * Get the size and object order for an image snapshot, or if |
| 4466 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4467 | * image. |
| 4468 | */ |
| 4469 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4470 | u8 *order, u64 *snap_size) |
| 4471 | { |
| 4472 | __le64 snapid = cpu_to_le64(snap_id); |
| 4473 | int ret; |
| 4474 | struct { |
| 4475 | u8 order; |
| 4476 | __le64 size; |
| 4477 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4478 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4479 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4480 | &rbd_dev->header_oloc, "get_size", |
| 4481 | &snapid, sizeof(snapid), |
| 4482 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4483 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4484 | if (ret < 0) |
| 4485 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4486 | if (ret < sizeof (size_buf)) |
| 4487 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4488 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4489 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4490 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4491 | dout(" order %u", (unsigned int)*order); |
| 4492 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4493 | *snap_size = le64_to_cpu(size_buf.size); |
| 4494 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4495 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4496 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4497 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4498 | |
| 4499 | return 0; |
| 4500 | } |
| 4501 | |
| 4502 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4503 | { |
| 4504 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4505 | &rbd_dev->header.obj_order, |
| 4506 | &rbd_dev->header.image_size); |
| 4507 | } |
| 4508 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4509 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4510 | { |
| 4511 | void *reply_buf; |
| 4512 | int ret; |
| 4513 | void *p; |
| 4514 | |
| 4515 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4516 | if (!reply_buf) |
| 4517 | return -ENOMEM; |
| 4518 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4519 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4520 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4521 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4522 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4523 | if (ret < 0) |
| 4524 | goto out; |
| 4525 | |
| 4526 | p = reply_buf; |
| 4527 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4528 | p + ret, NULL, GFP_NOIO); |
| 4529 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4530 | |
| 4531 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4532 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4533 | rbd_dev->header.object_prefix = NULL; |
| 4534 | } else { |
| 4535 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4536 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4537 | out: |
| 4538 | kfree(reply_buf); |
| 4539 | |
| 4540 | return ret; |
| 4541 | } |
| 4542 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4543 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4544 | u64 *snap_features) |
| 4545 | { |
| 4546 | __le64 snapid = cpu_to_le64(snap_id); |
| 4547 | struct { |
| 4548 | __le64 features; |
| 4549 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4550 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4551 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4552 | int ret; |
| 4553 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4554 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4555 | &rbd_dev->header_oloc, "get_features", |
| 4556 | &snapid, sizeof(snapid), |
| 4557 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4558 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4559 | if (ret < 0) |
| 4560 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4561 | if (ret < sizeof (features_buf)) |
| 4562 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4563 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4564 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4565 | if (unsup) { |
| 4566 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4567 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4568 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4569 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4570 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4571 | *snap_features = le64_to_cpu(features_buf.features); |
| 4572 | |
| 4573 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4574 | (unsigned long long)snap_id, |
| 4575 | (unsigned long long)*snap_features, |
| 4576 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4577 | |
| 4578 | return 0; |
| 4579 | } |
| 4580 | |
| 4581 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4582 | { |
| 4583 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4584 | &rbd_dev->header.features); |
| 4585 | } |
| 4586 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4587 | struct parent_image_info { |
| 4588 | u64 pool_id; |
| 4589 | const char *image_id; |
| 4590 | u64 snap_id; |
| 4591 | |
| 4592 | u64 overlap; |
| 4593 | }; |
| 4594 | |
| 4595 | /* |
| 4596 | * The caller is responsible for @pii. |
| 4597 | */ |
| 4598 | static int __get_parent_info_legacy(struct rbd_device *rbd_dev, |
| 4599 | struct page *req_page, |
| 4600 | struct page *reply_page, |
| 4601 | struct parent_image_info *pii) |
| 4602 | { |
| 4603 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4604 | size_t reply_len = PAGE_SIZE; |
| 4605 | void *p, *end; |
| 4606 | int ret; |
| 4607 | |
| 4608 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4609 | "rbd", "get_parent", CEPH_OSD_FLAG_READ, |
| 4610 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4611 | if (ret) |
| 4612 | return ret; |
| 4613 | |
| 4614 | p = page_address(reply_page); |
| 4615 | end = p + reply_len; |
| 4616 | ceph_decode_64_safe(&p, end, pii->pool_id, e_inval); |
| 4617 | pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
| 4618 | if (IS_ERR(pii->image_id)) { |
| 4619 | ret = PTR_ERR(pii->image_id); |
| 4620 | pii->image_id = NULL; |
| 4621 | return ret; |
| 4622 | } |
| 4623 | ceph_decode_64_safe(&p, end, pii->snap_id, e_inval); |
| 4624 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 4625 | |
| 4626 | return 0; |
| 4627 | |
| 4628 | e_inval: |
| 4629 | return -EINVAL; |
| 4630 | } |
| 4631 | |
| 4632 | static int get_parent_info(struct rbd_device *rbd_dev, |
| 4633 | struct parent_image_info *pii) |
| 4634 | { |
| 4635 | struct page *req_page, *reply_page; |
| 4636 | void *p; |
| 4637 | int ret; |
| 4638 | |
| 4639 | req_page = alloc_page(GFP_KERNEL); |
| 4640 | if (!req_page) |
| 4641 | return -ENOMEM; |
| 4642 | |
| 4643 | reply_page = alloc_page(GFP_KERNEL); |
| 4644 | if (!reply_page) { |
| 4645 | __free_page(req_page); |
| 4646 | return -ENOMEM; |
| 4647 | } |
| 4648 | |
| 4649 | p = page_address(req_page); |
| 4650 | ceph_encode_64(&p, rbd_dev->spec->snap_id); |
| 4651 | ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page, pii); |
| 4652 | |
| 4653 | __free_page(req_page); |
| 4654 | __free_page(reply_page); |
| 4655 | return ret; |
| 4656 | } |
| 4657 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4658 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4659 | { |
| 4660 | struct rbd_spec *parent_spec; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4661 | struct parent_image_info pii = { 0 }; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4662 | int ret; |
| 4663 | |
| 4664 | parent_spec = rbd_spec_alloc(); |
| 4665 | if (!parent_spec) |
| 4666 | return -ENOMEM; |
| 4667 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4668 | ret = get_parent_info(rbd_dev, &pii); |
| 4669 | if (ret) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4670 | goto out_err; |
| 4671 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4672 | dout("%s pool_id %llu image_id %s snap_id %llu overlap %llu\n", |
| 4673 | __func__, pii.pool_id, pii.image_id, pii.snap_id, pii.overlap); |
| 4674 | |
| 4675 | if (pii.pool_id == CEPH_NOPOOL) { |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4676 | /* |
| 4677 | * Either the parent never existed, or we have |
| 4678 | * record of it but the image got flattened so it no |
| 4679 | * longer has a parent. When the parent of a |
| 4680 | * layered image disappears we immediately set the |
| 4681 | * overlap to 0. The effect of this is that all new |
| 4682 | * requests will be treated as if the image had no |
| 4683 | * parent. |
| 4684 | */ |
| 4685 | if (rbd_dev->parent_overlap) { |
| 4686 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4687 | rbd_dev_parent_put(rbd_dev); |
| 4688 | pr_info("%s: clone image has been flattened\n", |
| 4689 | rbd_dev->disk->disk_name); |
| 4690 | } |
| 4691 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4692 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4693 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4694 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4695 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4696 | |
| 4697 | ret = -EIO; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4698 | if (pii.pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4699 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4700 | (unsigned long long)pii.pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4701 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4702 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4703 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4704 | /* |
| 4705 | * The parent won't change (except when the clone is |
| 4706 | * flattened, already handled that). So we only need to |
| 4707 | * record the parent spec we have not already done so. |
| 4708 | */ |
| 4709 | if (!rbd_dev->parent_spec) { |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4710 | parent_spec->pool_id = pii.pool_id; |
| 4711 | parent_spec->image_id = pii.image_id; |
| 4712 | pii.image_id = NULL; |
| 4713 | parent_spec->snap_id = pii.snap_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4714 | |
| 4715 | /* TODO: support cloning across namespaces */ |
| 4716 | if (rbd_dev->spec->pool_ns) { |
| 4717 | parent_spec->pool_ns = kstrdup(rbd_dev->spec->pool_ns, |
| 4718 | GFP_KERNEL); |
| 4719 | if (!parent_spec->pool_ns) { |
| 4720 | ret = -ENOMEM; |
| 4721 | goto out_err; |
| 4722 | } |
| 4723 | } |
| 4724 | |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4725 | rbd_dev->parent_spec = parent_spec; |
| 4726 | parent_spec = NULL; /* rbd_dev now owns this */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4727 | } |
| 4728 | |
| 4729 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4730 | * We always update the parent overlap. If it's zero we issue |
| 4731 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4732 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4733 | if (!pii.overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4734 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4735 | /* refresh, careful to warn just once */ |
| 4736 | if (rbd_dev->parent_overlap) |
| 4737 | rbd_warn(rbd_dev, |
| 4738 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4739 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4740 | /* initial probe */ |
| 4741 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4742 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4743 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4744 | rbd_dev->parent_overlap = pii.overlap; |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4745 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4746 | out: |
| 4747 | ret = 0; |
| 4748 | out_err: |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame^] | 4749 | kfree(pii.image_id); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4750 | rbd_spec_put(parent_spec); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4751 | return ret; |
| 4752 | } |
| 4753 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4754 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 4755 | { |
| 4756 | struct { |
| 4757 | __le64 stripe_unit; |
| 4758 | __le64 stripe_count; |
| 4759 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 4760 | size_t size = sizeof (striping_info_buf); |
| 4761 | void *p; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4762 | int ret; |
| 4763 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4764 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4765 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 4766 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4767 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 4768 | if (ret < 0) |
| 4769 | return ret; |
| 4770 | if (ret < size) |
| 4771 | return -ERANGE; |
| 4772 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4773 | p = &striping_info_buf; |
Ilya Dryomov | b133185 | 2018-02-07 12:09:12 +0100 | [diff] [blame] | 4774 | rbd_dev->header.stripe_unit = ceph_decode_64(&p); |
| 4775 | rbd_dev->header.stripe_count = ceph_decode_64(&p); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4776 | return 0; |
| 4777 | } |
| 4778 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4779 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 4780 | { |
| 4781 | __le64 data_pool_id; |
| 4782 | int ret; |
| 4783 | |
| 4784 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4785 | &rbd_dev->header_oloc, "get_data_pool", |
| 4786 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 4787 | if (ret < 0) |
| 4788 | return ret; |
| 4789 | if (ret < sizeof(data_pool_id)) |
| 4790 | return -EBADMSG; |
| 4791 | |
| 4792 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 4793 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 4794 | return 0; |
| 4795 | } |
| 4796 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4797 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 4798 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4799 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4800 | size_t image_id_size; |
| 4801 | char *image_id; |
| 4802 | void *p; |
| 4803 | void *end; |
| 4804 | size_t size; |
| 4805 | void *reply_buf = NULL; |
| 4806 | size_t len = 0; |
| 4807 | char *image_name = NULL; |
| 4808 | int ret; |
| 4809 | |
| 4810 | rbd_assert(!rbd_dev->spec->image_name); |
| 4811 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4812 | len = strlen(rbd_dev->spec->image_id); |
| 4813 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4814 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 4815 | if (!image_id) |
| 4816 | return NULL; |
| 4817 | |
| 4818 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4819 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4820 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4821 | |
| 4822 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 4823 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4824 | if (!reply_buf) |
| 4825 | goto out; |
| 4826 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4827 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 4828 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 4829 | "dir_get_name", image_id, image_id_size, |
| 4830 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4831 | if (ret < 0) |
| 4832 | goto out; |
| 4833 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 4834 | end = reply_buf + ret; |
| 4835 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4836 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 4837 | if (IS_ERR(image_name)) |
| 4838 | image_name = NULL; |
| 4839 | else |
| 4840 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 4841 | out: |
| 4842 | kfree(reply_buf); |
| 4843 | kfree(image_id); |
| 4844 | |
| 4845 | return image_name; |
| 4846 | } |
| 4847 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4848 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4849 | { |
| 4850 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4851 | const char *snap_name; |
| 4852 | u32 which = 0; |
| 4853 | |
| 4854 | /* Skip over names until we find the one we are looking for */ |
| 4855 | |
| 4856 | snap_name = rbd_dev->header.snap_names; |
| 4857 | while (which < snapc->num_snaps) { |
| 4858 | if (!strcmp(name, snap_name)) |
| 4859 | return snapc->snaps[which]; |
| 4860 | snap_name += strlen(snap_name) + 1; |
| 4861 | which++; |
| 4862 | } |
| 4863 | return CEPH_NOSNAP; |
| 4864 | } |
| 4865 | |
| 4866 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4867 | { |
| 4868 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 4869 | u32 which; |
| 4870 | bool found = false; |
| 4871 | u64 snap_id; |
| 4872 | |
| 4873 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 4874 | const char *snap_name; |
| 4875 | |
| 4876 | snap_id = snapc->snaps[which]; |
| 4877 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 4878 | if (IS_ERR(snap_name)) { |
| 4879 | /* ignore no-longer existing snapshots */ |
| 4880 | if (PTR_ERR(snap_name) == -ENOENT) |
| 4881 | continue; |
| 4882 | else |
| 4883 | break; |
| 4884 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4885 | found = !strcmp(name, snap_name); |
| 4886 | kfree(snap_name); |
| 4887 | } |
| 4888 | return found ? snap_id : CEPH_NOSNAP; |
| 4889 | } |
| 4890 | |
| 4891 | /* |
| 4892 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 4893 | * no snapshot by that name is found, or if an error occurs. |
| 4894 | */ |
| 4895 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 4896 | { |
| 4897 | if (rbd_dev->image_format == 1) |
| 4898 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 4899 | |
| 4900 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 4901 | } |
| 4902 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4903 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4904 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4905 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4906 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 4907 | { |
| 4908 | struct rbd_spec *spec = rbd_dev->spec; |
| 4909 | |
| 4910 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 4911 | rbd_assert(spec->image_id && spec->image_name); |
| 4912 | rbd_assert(spec->snap_name); |
| 4913 | |
| 4914 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 4915 | u64 snap_id; |
| 4916 | |
| 4917 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 4918 | if (snap_id == CEPH_NOSNAP) |
| 4919 | return -ENOENT; |
| 4920 | |
| 4921 | spec->snap_id = snap_id; |
| 4922 | } else { |
| 4923 | spec->snap_id = CEPH_NOSNAP; |
| 4924 | } |
| 4925 | |
| 4926 | return 0; |
| 4927 | } |
| 4928 | |
| 4929 | /* |
| 4930 | * A parent image will have all ids but none of the names. |
| 4931 | * |
| 4932 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 4933 | * can't figure out the name for an image id. |
| 4934 | */ |
| 4935 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4936 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4937 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4938 | struct rbd_spec *spec = rbd_dev->spec; |
| 4939 | const char *pool_name; |
| 4940 | const char *image_name; |
| 4941 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4942 | int ret; |
| 4943 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4944 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 4945 | rbd_assert(spec->image_id); |
| 4946 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4947 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4948 | /* 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] | 4949 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4950 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 4951 | if (!pool_name) { |
| 4952 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4953 | return -EIO; |
| 4954 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4955 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 4956 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4957 | return -ENOMEM; |
| 4958 | |
| 4959 | /* Fetch the image name; tolerate failure here */ |
| 4960 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4961 | image_name = rbd_dev_image_name(rbd_dev); |
| 4962 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4963 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4964 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4965 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4966 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4967 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 4968 | if (IS_ERR(snap_name)) { |
| 4969 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4970 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4971 | } |
| 4972 | |
| 4973 | spec->pool_name = pool_name; |
| 4974 | spec->image_name = image_name; |
| 4975 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4976 | |
| 4977 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4978 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4979 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 4980 | kfree(image_name); |
| 4981 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4982 | return ret; |
| 4983 | } |
| 4984 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4985 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4986 | { |
| 4987 | size_t size; |
| 4988 | int ret; |
| 4989 | void *reply_buf; |
| 4990 | void *p; |
| 4991 | void *end; |
| 4992 | u64 seq; |
| 4993 | u32 snap_count; |
| 4994 | struct ceph_snap_context *snapc; |
| 4995 | u32 i; |
| 4996 | |
| 4997 | /* |
| 4998 | * We'll need room for the seq value (maximum snapshot id), |
| 4999 | * snapshot count, and array of that many snapshot ids. |
| 5000 | * For now we have a fixed upper limit on the number we're |
| 5001 | * prepared to receive. |
| 5002 | */ |
| 5003 | size = sizeof (__le64) + sizeof (__le32) + |
| 5004 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5005 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5006 | if (!reply_buf) |
| 5007 | return -ENOMEM; |
| 5008 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5009 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5010 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5011 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5012 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5013 | if (ret < 0) |
| 5014 | goto out; |
| 5015 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5016 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5017 | end = reply_buf + ret; |
| 5018 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5019 | ceph_decode_64_safe(&p, end, seq, out); |
| 5020 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5021 | |
| 5022 | /* |
| 5023 | * Make sure the reported number of snapshot ids wouldn't go |
| 5024 | * beyond the end of our buffer. But before checking that, |
| 5025 | * make sure the computed size of the snapshot context we |
| 5026 | * allocate is representable in a size_t. |
| 5027 | */ |
| 5028 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5029 | / sizeof (u64)) { |
| 5030 | ret = -EINVAL; |
| 5031 | goto out; |
| 5032 | } |
| 5033 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5034 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5035 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5036 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5037 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5038 | if (!snapc) { |
| 5039 | ret = -ENOMEM; |
| 5040 | goto out; |
| 5041 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5042 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5043 | for (i = 0; i < snap_count; i++) |
| 5044 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5045 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5046 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5047 | rbd_dev->header.snapc = snapc; |
| 5048 | |
| 5049 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5050 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5051 | out: |
| 5052 | kfree(reply_buf); |
| 5053 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5054 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5055 | } |
| 5056 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5057 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5058 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5059 | { |
| 5060 | size_t size; |
| 5061 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5062 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5063 | int ret; |
| 5064 | void *p; |
| 5065 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5066 | char *snap_name; |
| 5067 | |
| 5068 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5069 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5070 | if (!reply_buf) |
| 5071 | return ERR_PTR(-ENOMEM); |
| 5072 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5073 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5074 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5075 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5076 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5077 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5078 | if (ret < 0) { |
| 5079 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5080 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5081 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5082 | |
| 5083 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5084 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5085 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5086 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5087 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5088 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5089 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5090 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5091 | out: |
| 5092 | kfree(reply_buf); |
| 5093 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5094 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5095 | } |
| 5096 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5097 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5098 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5099 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5100 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5101 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5102 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5103 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5104 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5105 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5106 | if (first_time) { |
| 5107 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5108 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5109 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5110 | } |
| 5111 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5112 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5113 | if (ret && first_time) { |
| 5114 | kfree(rbd_dev->header.object_prefix); |
| 5115 | rbd_dev->header.object_prefix = NULL; |
| 5116 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5117 | |
| 5118 | return ret; |
| 5119 | } |
| 5120 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5121 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5122 | { |
| 5123 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5124 | |
| 5125 | if (rbd_dev->image_format == 1) |
| 5126 | return rbd_dev_v1_header_info(rbd_dev); |
| 5127 | |
| 5128 | return rbd_dev_v2_header_info(rbd_dev); |
| 5129 | } |
| 5130 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5131 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5132 | * Skips over white space at *buf, and updates *buf to point to the |
| 5133 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5134 | * the token (string of non-white space characters) found. Note |
| 5135 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5136 | */ |
| 5137 | static inline size_t next_token(const char **buf) |
| 5138 | { |
| 5139 | /* |
| 5140 | * These are the characters that produce nonzero for |
| 5141 | * isspace() in the "C" and "POSIX" locales. |
| 5142 | */ |
| 5143 | const char *spaces = " \f\n\r\t\v"; |
| 5144 | |
| 5145 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5146 | |
| 5147 | return strcspn(*buf, spaces); /* Return token length */ |
| 5148 | } |
| 5149 | |
| 5150 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5151 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5152 | * enough to hold a copy of it, and copies the token into the new |
| 5153 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5154 | * that a duplicate buffer is created even for a zero-length token. |
| 5155 | * |
| 5156 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5157 | * pointer if memory for the duplicate was not available. If |
| 5158 | * the lenp argument is a non-null pointer, the length of the token |
| 5159 | * (not including the '\0') is returned in *lenp. |
| 5160 | * |
| 5161 | * If successful, the *buf pointer will be updated to point beyond |
| 5162 | * the end of the found token. |
| 5163 | * |
| 5164 | * Note: uses GFP_KERNEL for allocation. |
| 5165 | */ |
| 5166 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5167 | { |
| 5168 | char *dup; |
| 5169 | size_t len; |
| 5170 | |
| 5171 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5172 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5173 | if (!dup) |
| 5174 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5175 | *(dup + len) = '\0'; |
| 5176 | *buf += len; |
| 5177 | |
| 5178 | if (lenp) |
| 5179 | *lenp = len; |
| 5180 | |
| 5181 | return dup; |
| 5182 | } |
| 5183 | |
| 5184 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5185 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5186 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5187 | * and the data written is passed here via a NUL-terminated buffer. |
| 5188 | * Returns 0 if successful or an error code otherwise. |
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 | * The information extracted from these options is recorded in |
| 5191 | * the other parameters which return dynamically-allocated |
| 5192 | * structures: |
| 5193 | * ceph_opts |
| 5194 | * The address of a pointer that will refer to a ceph options |
| 5195 | * structure. Caller must release the returned pointer using |
| 5196 | * ceph_destroy_options() when it is no longer needed. |
| 5197 | * rbd_opts |
| 5198 | * Address of an rbd options pointer. Fully initialized by |
| 5199 | * this function; caller must release with kfree(). |
| 5200 | * spec |
| 5201 | * Address of an rbd image specification pointer. Fully |
| 5202 | * initialized by this function based on parsed options. |
| 5203 | * Caller must release with rbd_spec_put(). |
| 5204 | * |
| 5205 | * The options passed take this form: |
| 5206 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5207 | * where: |
| 5208 | * <mon_addrs> |
| 5209 | * A comma-separated list of one or more monitor addresses. |
| 5210 | * A monitor address is an ip address, optionally followed |
| 5211 | * by a port number (separated by a colon). |
| 5212 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5213 | * <options> |
| 5214 | * A comma-separated list of ceph and/or rbd options. |
| 5215 | * <pool_name> |
| 5216 | * The name of the rados pool containing the rbd image. |
| 5217 | * <image_name> |
| 5218 | * The name of the image in that pool to map. |
| 5219 | * <snap_id> |
| 5220 | * An optional snapshot id. If provided, the mapping will |
| 5221 | * present data from the image at the time that snapshot was |
| 5222 | * created. The image head is used if no snapshot id is |
| 5223 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5224 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5225 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5226 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5227 | struct rbd_options **opts, |
| 5228 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5229 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5230 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5231 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5232 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5233 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5234 | size_t mon_addrs_size; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5235 | struct parse_rbd_opts_ctx pctx = { 0 }; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5236 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5237 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5238 | |
| 5239 | /* The first four tokens are required */ |
| 5240 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5241 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5242 | if (!len) { |
| 5243 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5244 | return -EINVAL; |
| 5245 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5246 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5247 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5248 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5249 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5250 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5251 | options = dup_token(&buf, NULL); |
| 5252 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5253 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5254 | if (!*options) { |
| 5255 | rbd_warn(NULL, "no options provided"); |
| 5256 | goto out_err; |
| 5257 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5258 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5259 | pctx.spec = rbd_spec_alloc(); |
| 5260 | if (!pctx.spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5261 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5262 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5263 | pctx.spec->pool_name = dup_token(&buf, NULL); |
| 5264 | if (!pctx.spec->pool_name) |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5265 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5266 | if (!*pctx.spec->pool_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5267 | rbd_warn(NULL, "no pool name provided"); |
| 5268 | goto out_err; |
| 5269 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5270 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5271 | pctx.spec->image_name = dup_token(&buf, NULL); |
| 5272 | if (!pctx.spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5273 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5274 | if (!*pctx.spec->image_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5275 | rbd_warn(NULL, "no image name provided"); |
| 5276 | goto out_err; |
| 5277 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5278 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5279 | /* |
| 5280 | * Snapshot name is optional; default is to use "-" |
| 5281 | * (indicating the head/no snapshot). |
| 5282 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5283 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5284 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5285 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5286 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5287 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5288 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5289 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5290 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5291 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5292 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5293 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5294 | *(snap_name + len) = '\0'; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5295 | pctx.spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5296 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5297 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5298 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5299 | pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL); |
| 5300 | if (!pctx.opts) |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5301 | goto out_mem; |
| 5302 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5303 | pctx.opts->read_only = RBD_READ_ONLY_DEFAULT; |
| 5304 | pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
| 5305 | pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT; |
| 5306 | pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
| 5307 | pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
| 5308 | pctx.opts->trim = RBD_TRIM_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5309 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5310 | copts = ceph_parse_options(options, mon_addrs, |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5311 | mon_addrs + mon_addrs_size - 1, |
| 5312 | parse_rbd_opts_token, &pctx); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5313 | if (IS_ERR(copts)) { |
| 5314 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5315 | goto out_err; |
| 5316 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5317 | kfree(options); |
| 5318 | |
| 5319 | *ceph_opts = copts; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5320 | *opts = pctx.opts; |
| 5321 | *rbd_spec = pctx.spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5322 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5323 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5324 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5325 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5326 | out_err: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5327 | kfree(pctx.opts); |
| 5328 | rbd_spec_put(pctx.spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5329 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5330 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5331 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5332 | } |
| 5333 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5334 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5335 | { |
| 5336 | down_write(&rbd_dev->lock_rwsem); |
| 5337 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5338 | rbd_unlock(rbd_dev); |
| 5339 | up_write(&rbd_dev->lock_rwsem); |
| 5340 | } |
| 5341 | |
| 5342 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5343 | { |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5344 | int ret; |
| 5345 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5346 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5347 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5348 | return -EINVAL; |
| 5349 | } |
| 5350 | |
| 5351 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5352 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5353 | ret = rbd_wait_state_locked(rbd_dev, true); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5354 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5355 | if (ret) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5356 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5357 | return -EROFS; |
| 5358 | } |
| 5359 | |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5363 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5364 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5365 | * name given to it by the user. Internally, that identifier is |
| 5366 | * what's used to specify the names of objects related to the image. |
| 5367 | * |
| 5368 | * A special "rbd id" object is used to map an rbd image name to its |
| 5369 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5370 | * with the supplied name. |
| 5371 | * |
| 5372 | * This function will record the given rbd_dev's image_id field if |
| 5373 | * it can be determined, and in that case will return 0. If any |
| 5374 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5375 | * image_id field will be unchanged (and should be NULL). |
| 5376 | */ |
| 5377 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5378 | { |
| 5379 | int ret; |
| 5380 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5381 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5382 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5383 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5384 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5385 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5386 | * When probing a parent image, the image id is already |
| 5387 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5388 | * need to fetch the image id again in this case. We |
| 5389 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5390 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5391 | if (rbd_dev->spec->image_id) { |
| 5392 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5393 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5394 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5395 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5396 | |
| 5397 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5398 | * First, see if the format 2 image id file exists, and if |
| 5399 | * so, get the image's persistent id from it. |
| 5400 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5401 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5402 | rbd_dev->spec->image_name); |
| 5403 | if (ret) |
| 5404 | return ret; |
| 5405 | |
| 5406 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5407 | |
| 5408 | /* Response will be an encoded string, which includes a length */ |
| 5409 | |
| 5410 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5411 | response = kzalloc(size, GFP_NOIO); |
| 5412 | if (!response) { |
| 5413 | ret = -ENOMEM; |
| 5414 | goto out; |
| 5415 | } |
| 5416 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5417 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5418 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5419 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5420 | "get_id", NULL, 0, |
| 5421 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5422 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5423 | if (ret == -ENOENT) { |
| 5424 | image_id = kstrdup("", GFP_KERNEL); |
| 5425 | ret = image_id ? 0 : -ENOMEM; |
| 5426 | if (!ret) |
| 5427 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5428 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5429 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5430 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5431 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5432 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5433 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5434 | if (!ret) |
| 5435 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | if (!ret) { |
| 5439 | rbd_dev->spec->image_id = image_id; |
| 5440 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5441 | } |
| 5442 | out: |
| 5443 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5444 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5445 | return ret; |
| 5446 | } |
| 5447 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5448 | /* |
| 5449 | * Undo whatever state changes are made by v1 or v2 header info |
| 5450 | * call. |
| 5451 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5452 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5453 | { |
| 5454 | struct rbd_image_header *header; |
| 5455 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5456 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5457 | |
| 5458 | /* Free dynamic fields from the header, then zero it out */ |
| 5459 | |
| 5460 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5461 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5462 | kfree(header->snap_sizes); |
| 5463 | kfree(header->snap_names); |
| 5464 | kfree(header->object_prefix); |
| 5465 | memset(header, 0, sizeof (*header)); |
| 5466 | } |
| 5467 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5468 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5469 | { |
| 5470 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5471 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5472 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5473 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5474 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5475 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5476 | /* |
| 5477 | * Get the and check features for the image. Currently the |
| 5478 | * features are assumed to never change. |
| 5479 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5480 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5481 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5482 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5483 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5484 | /* If the image supports fancy striping, get its parameters */ |
| 5485 | |
| 5486 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5487 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5488 | if (ret < 0) |
| 5489 | goto out_err; |
| 5490 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5491 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5492 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5493 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5494 | if (ret) |
| 5495 | goto out_err; |
| 5496 | } |
| 5497 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5498 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5499 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5500 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5501 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5502 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5503 | kfree(rbd_dev->header.object_prefix); |
| 5504 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5505 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5506 | } |
| 5507 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5508 | /* |
| 5509 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5510 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5511 | * length of the already discovered part of the parent chain. |
| 5512 | */ |
| 5513 | 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] | 5514 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5515 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5516 | int ret; |
| 5517 | |
| 5518 | if (!rbd_dev->parent_spec) |
| 5519 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5520 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5521 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5522 | pr_info("parent chain is too long (%d)\n", depth); |
| 5523 | ret = -EINVAL; |
| 5524 | goto out_err; |
| 5525 | } |
| 5526 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5527 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5528 | if (!parent) { |
| 5529 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5530 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | /* |
| 5534 | * Images related by parent/child relationships always share |
| 5535 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5536 | */ |
| 5537 | __rbd_get_client(rbd_dev->rbd_client); |
| 5538 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5539 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5540 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5541 | if (ret < 0) |
| 5542 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5543 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5544 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5545 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5546 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5547 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5548 | out_err: |
| 5549 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5550 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5551 | return ret; |
| 5552 | } |
| 5553 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5554 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5555 | { |
| 5556 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5557 | rbd_dev_mapping_clear(rbd_dev); |
| 5558 | rbd_free_disk(rbd_dev); |
| 5559 | if (!single_major) |
| 5560 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5561 | } |
| 5562 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5563 | /* |
| 5564 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5565 | * upon return. |
| 5566 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5567 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5568 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5569 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5570 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5571 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5572 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5573 | if (!single_major) { |
| 5574 | ret = register_blkdev(0, rbd_dev->name); |
| 5575 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5576 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5577 | |
| 5578 | rbd_dev->major = ret; |
| 5579 | rbd_dev->minor = 0; |
| 5580 | } else { |
| 5581 | rbd_dev->major = rbd_major; |
| 5582 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5583 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5584 | |
| 5585 | /* Set up the blkdev mapping. */ |
| 5586 | |
| 5587 | ret = rbd_init_disk(rbd_dev); |
| 5588 | if (ret) |
| 5589 | goto err_out_blkdev; |
| 5590 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5591 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5592 | if (ret) |
| 5593 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5594 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5595 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5596 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5597 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5598 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5599 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5600 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5601 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5602 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5603 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5604 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5605 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5606 | err_out_mapping: |
| 5607 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5608 | err_out_disk: |
| 5609 | rbd_free_disk(rbd_dev); |
| 5610 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5611 | if (!single_major) |
| 5612 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5613 | err_out_unlock: |
| 5614 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5615 | return ret; |
| 5616 | } |
| 5617 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5618 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5619 | { |
| 5620 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5621 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5622 | |
| 5623 | /* Record the header object name for this rbd image. */ |
| 5624 | |
| 5625 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5626 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5627 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5628 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5629 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5630 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5631 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5632 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5633 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5634 | } |
| 5635 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5636 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5637 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5638 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5639 | if (rbd_dev->opts) |
| 5640 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5641 | rbd_dev->image_format = 0; |
| 5642 | kfree(rbd_dev->spec->image_id); |
| 5643 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5644 | } |
| 5645 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5646 | /* |
| 5647 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5648 | * device. If this image is the one being mapped (i.e., not a |
| 5649 | * parent), initiate a watch on its header object before using that |
| 5650 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5651 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5652 | 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] | 5653 | { |
| 5654 | int ret; |
| 5655 | |
| 5656 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5657 | * Get the id from the image id object. Unless there's an |
| 5658 | * error, rbd_dev->spec->image_id will be filled in with |
| 5659 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5660 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5661 | */ |
| 5662 | ret = rbd_dev_image_id(rbd_dev); |
| 5663 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5664 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5665 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5666 | ret = rbd_dev_header_name(rbd_dev); |
| 5667 | if (ret) |
| 5668 | goto err_out_format; |
| 5669 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5670 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5671 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5672 | if (ret) { |
| 5673 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5674 | pr_info("image %s/%s%s%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5675 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5676 | rbd_dev->spec->pool_ns ?: "", |
| 5677 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5678 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5679 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5680 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5681 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5682 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5683 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5684 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5685 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5686 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5687 | /* |
| 5688 | * If this image is the one being mapped, we have pool name and |
| 5689 | * id, image name and id, and snap name - need to fill snap id. |
| 5690 | * Otherwise this is a parent image, identified by pool, image |
| 5691 | * and snap ids - need to fill in names for those ids. |
| 5692 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5693 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5694 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5695 | else |
| 5696 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5697 | if (ret) { |
| 5698 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5699 | pr_info("snap %s/%s%s%s@%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5700 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5701 | rbd_dev->spec->pool_ns ?: "", |
| 5702 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5703 | rbd_dev->spec->image_name, |
| 5704 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5705 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5706 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5707 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5708 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5709 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5710 | if (ret) |
| 5711 | goto err_out_probe; |
| 5712 | |
| 5713 | /* |
| 5714 | * Need to warn users if this image is the one being |
| 5715 | * mapped and has a parent. |
| 5716 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5717 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5718 | rbd_warn(rbd_dev, |
| 5719 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 5720 | } |
| 5721 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5722 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5723 | if (ret) |
| 5724 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5725 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5726 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5727 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5728 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5729 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5730 | err_out_probe: |
| 5731 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5732 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5733 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5734 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5735 | err_out_format: |
| 5736 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5737 | kfree(rbd_dev->spec->image_id); |
| 5738 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5739 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5740 | } |
| 5741 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5742 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 5743 | const char *buf, |
| 5744 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5745 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5746 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5747 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5748 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5749 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5750 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5751 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5752 | |
| 5753 | if (!try_module_get(THIS_MODULE)) |
| 5754 | return -ENODEV; |
| 5755 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5756 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5757 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5758 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5759 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5760 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5761 | rbdc = rbd_get_client(ceph_opts); |
| 5762 | if (IS_ERR(rbdc)) { |
| 5763 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5764 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5765 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5766 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5767 | /* pick the pool */ |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 5768 | rc = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5769 | if (rc < 0) { |
| 5770 | if (rc == -ENOENT) |
| 5771 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5772 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5773 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5774 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5775 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5776 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5777 | if (!rbd_dev) { |
| 5778 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5779 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5780 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5781 | rbdc = NULL; /* rbd_dev now owns this */ |
| 5782 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5783 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5784 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5785 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 5786 | if (!rbd_dev->config_info) { |
| 5787 | rc = -ENOMEM; |
| 5788 | goto err_out_rbd_dev; |
| 5789 | } |
| 5790 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5791 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5792 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5793 | if (rc < 0) { |
| 5794 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5795 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5796 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 5797 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5798 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5799 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5800 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5801 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5802 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5803 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5804 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5805 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5806 | if (rbd_dev->opts->exclusive) { |
| 5807 | rc = rbd_add_acquire_lock(rbd_dev); |
| 5808 | if (rc) |
| 5809 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5810 | } |
| 5811 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5812 | /* Everything's ready. Announce the disk to the world. */ |
| 5813 | |
| 5814 | rc = device_add(&rbd_dev->dev); |
| 5815 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5816 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5817 | |
| 5818 | add_disk(rbd_dev->disk); |
| 5819 | /* see rbd_init_disk() */ |
| 5820 | blk_put_queue(rbd_dev->disk->queue); |
| 5821 | |
| 5822 | spin_lock(&rbd_dev_list_lock); |
| 5823 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5824 | spin_unlock(&rbd_dev_list_lock); |
| 5825 | |
| 5826 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5827 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5828 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5829 | rc = count; |
| 5830 | out: |
| 5831 | module_put(THIS_MODULE); |
| 5832 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5833 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5834 | err_out_image_lock: |
| 5835 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5836 | err_out_device_setup: |
| 5837 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5838 | err_out_image_probe: |
| 5839 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5840 | err_out_rbd_dev: |
| 5841 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5842 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5843 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5844 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5845 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5846 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5847 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5848 | } |
| 5849 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5850 | static ssize_t rbd_add(struct bus_type *bus, |
| 5851 | const char *buf, |
| 5852 | size_t count) |
| 5853 | { |
| 5854 | if (single_major) |
| 5855 | return -EINVAL; |
| 5856 | |
| 5857 | return do_rbd_add(bus, buf, count); |
| 5858 | } |
| 5859 | |
| 5860 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 5861 | const char *buf, |
| 5862 | size_t count) |
| 5863 | { |
| 5864 | return do_rbd_add(bus, buf, count); |
| 5865 | } |
| 5866 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5867 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 5868 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5869 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5870 | struct rbd_device *first = rbd_dev; |
| 5871 | struct rbd_device *second = first->parent; |
| 5872 | struct rbd_device *third; |
| 5873 | |
| 5874 | /* |
| 5875 | * Follow to the parent with no grandparent and |
| 5876 | * remove it. |
| 5877 | */ |
| 5878 | while (second && (third = second->parent)) { |
| 5879 | first = second; |
| 5880 | second = third; |
| 5881 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5882 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5883 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5884 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5885 | first->parent = NULL; |
| 5886 | first->parent_overlap = 0; |
| 5887 | |
| 5888 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5889 | rbd_spec_put(first->parent_spec); |
| 5890 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5891 | } |
| 5892 | } |
| 5893 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5894 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 5895 | const char *buf, |
| 5896 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5897 | { |
| 5898 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5899 | struct list_head *tmp; |
| 5900 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5901 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5902 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5903 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5904 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5905 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5906 | dev_id = -1; |
| 5907 | opt_buf[0] = '\0'; |
| 5908 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 5909 | if (dev_id < 0) { |
| 5910 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5911 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5912 | } |
| 5913 | if (opt_buf[0] != '\0') { |
| 5914 | if (!strcmp(opt_buf, "force")) { |
| 5915 | force = true; |
| 5916 | } else { |
| 5917 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 5918 | return -EINVAL; |
| 5919 | } |
| 5920 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5921 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5922 | ret = -ENOENT; |
| 5923 | spin_lock(&rbd_dev_list_lock); |
| 5924 | list_for_each(tmp, &rbd_dev_list) { |
| 5925 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 5926 | if (rbd_dev->dev_id == dev_id) { |
| 5927 | ret = 0; |
| 5928 | break; |
| 5929 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5930 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5931 | if (!ret) { |
| 5932 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5933 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5934 | ret = -EBUSY; |
| 5935 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5936 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 5937 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5938 | spin_unlock_irq(&rbd_dev->lock); |
| 5939 | } |
| 5940 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 5941 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5942 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5943 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5944 | if (force) { |
| 5945 | /* |
| 5946 | * Prevent new IO from being queued and wait for existing |
| 5947 | * IO to complete/fail. |
| 5948 | */ |
| 5949 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 5950 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 5951 | } |
| 5952 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5953 | del_gendisk(rbd_dev->disk); |
| 5954 | spin_lock(&rbd_dev_list_lock); |
| 5955 | list_del_init(&rbd_dev->node); |
| 5956 | spin_unlock(&rbd_dev_list_lock); |
| 5957 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 5958 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5959 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5960 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5961 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5962 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 5963 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5966 | static ssize_t rbd_remove(struct bus_type *bus, |
| 5967 | const char *buf, |
| 5968 | size_t count) |
| 5969 | { |
| 5970 | if (single_major) |
| 5971 | return -EINVAL; |
| 5972 | |
| 5973 | return do_rbd_remove(bus, buf, count); |
| 5974 | } |
| 5975 | |
| 5976 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 5977 | const char *buf, |
| 5978 | size_t count) |
| 5979 | { |
| 5980 | return do_rbd_remove(bus, buf, count); |
| 5981 | } |
| 5982 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5983 | /* |
| 5984 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5985 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5986 | */ |
| 5987 | static int rbd_sysfs_init(void) |
| 5988 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5989 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5990 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5991 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 5992 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 5993 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5994 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 5995 | ret = bus_register(&rbd_bus_type); |
| 5996 | if (ret < 0) |
| 5997 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5998 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5999 | return ret; |
| 6000 | } |
| 6001 | |
| 6002 | static void rbd_sysfs_cleanup(void) |
| 6003 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6004 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6005 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6006 | } |
| 6007 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6008 | static int rbd_slab_init(void) |
| 6009 | { |
| 6010 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6011 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6012 | if (!rbd_img_request_cache) |
| 6013 | return -ENOMEM; |
| 6014 | |
| 6015 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6016 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6017 | if (!rbd_obj_request_cache) |
| 6018 | goto out_err; |
| 6019 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6020 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6021 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6022 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6023 | kmem_cache_destroy(rbd_img_request_cache); |
| 6024 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6025 | return -ENOMEM; |
| 6026 | } |
| 6027 | |
| 6028 | static void rbd_slab_exit(void) |
| 6029 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6030 | rbd_assert(rbd_obj_request_cache); |
| 6031 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6032 | rbd_obj_request_cache = NULL; |
| 6033 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6034 | rbd_assert(rbd_img_request_cache); |
| 6035 | kmem_cache_destroy(rbd_img_request_cache); |
| 6036 | rbd_img_request_cache = NULL; |
| 6037 | } |
| 6038 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6039 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6040 | { |
| 6041 | int rc; |
| 6042 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6043 | if (!libceph_compatible(NULL)) { |
| 6044 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6045 | return -EINVAL; |
| 6046 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6047 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6048 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6049 | if (rc) |
| 6050 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6051 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6052 | /* |
| 6053 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6054 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6055 | */ |
| 6056 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6057 | if (!rbd_wq) { |
| 6058 | rc = -ENOMEM; |
| 6059 | goto err_out_slab; |
| 6060 | } |
| 6061 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6062 | if (single_major) { |
| 6063 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6064 | if (rbd_major < 0) { |
| 6065 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6066 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6067 | } |
| 6068 | } |
| 6069 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6070 | rc = rbd_sysfs_init(); |
| 6071 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6072 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6073 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6074 | if (single_major) |
| 6075 | pr_info("loaded (major %d)\n", rbd_major); |
| 6076 | else |
| 6077 | pr_info("loaded\n"); |
| 6078 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6079 | return 0; |
| 6080 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6081 | err_out_blkdev: |
| 6082 | if (single_major) |
| 6083 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6084 | err_out_wq: |
| 6085 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6086 | err_out_slab: |
| 6087 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6088 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6089 | } |
| 6090 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6091 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6092 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6093 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6094 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6095 | if (single_major) |
| 6096 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6097 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6098 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | module_init(rbd_init); |
| 6102 | module_exit(rbd_exit); |
| 6103 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6104 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6105 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6106 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6107 | /* following authorship retained from original osdblk.c */ |
| 6108 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6109 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6110 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6111 | MODULE_LICENSE("GPL"); |