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, |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 217 | OBJ_OP_ZEROOUT, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 218 | }; |
| 219 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 220 | /* |
| 221 | * Writes go through the following state machine to deal with |
| 222 | * layering: |
| 223 | * |
| 224 | * need copyup |
| 225 | * RBD_OBJ_WRITE_GUARD ---------------> RBD_OBJ_WRITE_COPYUP |
| 226 | * | ^ | |
| 227 | * v \------------------------------/ |
| 228 | * done |
| 229 | * ^ |
| 230 | * | |
| 231 | * RBD_OBJ_WRITE_FLAT |
| 232 | * |
| 233 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
| 234 | * there is a parent or not. |
| 235 | */ |
| 236 | enum rbd_obj_write_state { |
| 237 | RBD_OBJ_WRITE_FLAT = 1, |
| 238 | RBD_OBJ_WRITE_GUARD, |
| 239 | RBD_OBJ_WRITE_COPYUP, |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 240 | }; |
| 241 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 242 | struct rbd_obj_request { |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 243 | struct ceph_object_extent ex; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 244 | union { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 245 | bool tried_parent; /* for reads */ |
| 246 | enum rbd_obj_write_state write_state; /* for writes */ |
| 247 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 248 | |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 249 | struct rbd_img_request *img_request; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 250 | struct ceph_file_extent *img_extents; |
| 251 | u32 num_img_extents; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 252 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 253 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 254 | struct ceph_bio_iter bio_pos; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 255 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 256 | struct ceph_bvec_iter bvec_pos; |
| 257 | u32 bvec_count; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 258 | u32 bvec_idx; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 259 | }; |
| 260 | }; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 261 | struct bio_vec *copyup_bvecs; |
| 262 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 263 | |
| 264 | struct ceph_osd_request *osd_req; |
| 265 | |
| 266 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 267 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 268 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 269 | struct kref kref; |
| 270 | }; |
| 271 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 272 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 273 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 274 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 275 | }; |
| 276 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 277 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 278 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 279 | enum obj_operation_type op_type; |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 280 | enum obj_request_type data_type; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 281 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 282 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 283 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 284 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 285 | }; |
| 286 | union { |
| 287 | struct request *rq; /* block request */ |
| 288 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 289 | }; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 290 | spinlock_t completion_lock; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 291 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 292 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 293 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 294 | struct list_head object_extents; /* obj_req.ex structs */ |
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, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 736 | Opt_alloc_size, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 737 | Opt_lock_timeout, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 738 | Opt_last_int, |
| 739 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 740 | Opt_pool_ns, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 741 | Opt_last_string, |
| 742 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 743 | Opt_read_only, |
| 744 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 745 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 746 | Opt_exclusive, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 747 | Opt_notrim, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 748 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 749 | }; |
| 750 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 751 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 752 | {Opt_queue_depth, "queue_depth=%d"}, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 753 | {Opt_alloc_size, "alloc_size=%d"}, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 754 | {Opt_lock_timeout, "lock_timeout=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 755 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 756 | {Opt_pool_ns, "_pool_ns=%s"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 757 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 758 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 759 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 760 | {Opt_read_write, "read_write"}, |
| 761 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 762 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 763 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 764 | {Opt_notrim, "notrim"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 765 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 766 | }; |
| 767 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 768 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 769 | int queue_depth; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 770 | int alloc_size; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 771 | unsigned long lock_timeout; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 772 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 773 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 774 | bool exclusive; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 775 | bool trim; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 776 | }; |
| 777 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 778 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 779 | #define RBD_ALLOC_SIZE_DEFAULT (64 * 1024) |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 780 | #define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 781 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 782 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 783 | #define RBD_EXCLUSIVE_DEFAULT false |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 784 | #define RBD_TRIM_DEFAULT true |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 785 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 786 | struct parse_rbd_opts_ctx { |
| 787 | struct rbd_spec *spec; |
| 788 | struct rbd_options *opts; |
| 789 | }; |
| 790 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 791 | static int parse_rbd_opts_token(char *c, void *private) |
| 792 | { |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 793 | struct parse_rbd_opts_ctx *pctx = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 794 | substring_t argstr[MAX_OPT_ARGS]; |
| 795 | int token, intval, ret; |
| 796 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 797 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 798 | if (token < Opt_last_int) { |
| 799 | ret = match_int(&argstr[0], &intval); |
| 800 | if (ret < 0) { |
Ilya Dryomov | 2f56b6b | 2018-06-27 16:38:13 +0200 | [diff] [blame] | 801 | pr_err("bad option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 802 | return ret; |
| 803 | } |
| 804 | dout("got int token %d val %d\n", token, intval); |
| 805 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 806 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 807 | } else { |
| 808 | dout("got token %d\n", token); |
| 809 | } |
| 810 | |
| 811 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 812 | case Opt_queue_depth: |
| 813 | if (intval < 1) { |
| 814 | pr_err("queue_depth out of range\n"); |
| 815 | return -EINVAL; |
| 816 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 817 | pctx->opts->queue_depth = intval; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 818 | break; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 819 | case Opt_alloc_size: |
| 820 | if (intval < 1) { |
| 821 | pr_err("alloc_size out of range\n"); |
| 822 | return -EINVAL; |
| 823 | } |
| 824 | if (!is_power_of_2(intval)) { |
| 825 | pr_err("alloc_size must be a power of 2\n"); |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | pctx->opts->alloc_size = intval; |
| 829 | break; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 830 | case Opt_lock_timeout: |
| 831 | /* 0 is "wait forever" (i.e. infinite timeout) */ |
| 832 | if (intval < 0 || intval > INT_MAX / 1000) { |
| 833 | pr_err("lock_timeout out of range\n"); |
| 834 | return -EINVAL; |
| 835 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 836 | pctx->opts->lock_timeout = msecs_to_jiffies(intval * 1000); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 837 | break; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 838 | case Opt_pool_ns: |
| 839 | kfree(pctx->spec->pool_ns); |
| 840 | pctx->spec->pool_ns = match_strdup(argstr); |
| 841 | if (!pctx->spec->pool_ns) |
| 842 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 843 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 844 | case Opt_read_only: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 845 | pctx->opts->read_only = true; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 846 | break; |
| 847 | case Opt_read_write: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 848 | pctx->opts->read_only = false; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 849 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 850 | case Opt_lock_on_read: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 851 | pctx->opts->lock_on_read = true; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 852 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 853 | case Opt_exclusive: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 854 | pctx->opts->exclusive = true; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 855 | break; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 856 | case Opt_notrim: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 857 | pctx->opts->trim = false; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 858 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 859 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 860 | /* libceph prints "bad option" msg */ |
| 861 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 862 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 863 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 867 | static char* obj_op_name(enum obj_operation_type op_type) |
| 868 | { |
| 869 | switch (op_type) { |
| 870 | case OBJ_OP_READ: |
| 871 | return "read"; |
| 872 | case OBJ_OP_WRITE: |
| 873 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 874 | case OBJ_OP_DISCARD: |
| 875 | return "discard"; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 876 | case OBJ_OP_ZEROOUT: |
| 877 | return "zeroout"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 878 | default: |
| 879 | return "???"; |
| 880 | } |
| 881 | } |
| 882 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 883 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 884 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 885 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 886 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 887 | */ |
| 888 | static void rbd_client_release(struct kref *kref) |
| 889 | { |
| 890 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 891 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 892 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 893 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 894 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 895 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 896 | |
| 897 | ceph_destroy_client(rbdc->client); |
| 898 | kfree(rbdc); |
| 899 | } |
| 900 | |
| 901 | /* |
| 902 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 903 | * it. |
| 904 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 905 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 906 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 907 | if (rbdc) |
| 908 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 911 | static int wait_for_latest_osdmap(struct ceph_client *client) |
| 912 | { |
| 913 | u64 newest_epoch; |
| 914 | int ret; |
| 915 | |
| 916 | ret = ceph_monc_get_version(&client->monc, "osdmap", &newest_epoch); |
| 917 | if (ret) |
| 918 | return ret; |
| 919 | |
| 920 | if (client->osdc.osdmap->epoch >= newest_epoch) |
| 921 | return 0; |
| 922 | |
| 923 | ceph_osdc_maybe_request_map(&client->osdc); |
| 924 | return ceph_monc_wait_osdmap(&client->monc, newest_epoch, |
| 925 | client->options->mount_timeout); |
| 926 | } |
| 927 | |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 928 | /* |
| 929 | * Get a ceph client with specific addr and configuration, if one does |
| 930 | * not exist create it. Either way, ceph_opts is consumed by this |
| 931 | * function. |
| 932 | */ |
| 933 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
| 934 | { |
| 935 | struct rbd_client *rbdc; |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 936 | int ret; |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 937 | |
| 938 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
| 939 | rbdc = rbd_client_find(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 940 | if (rbdc) { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 941 | ceph_destroy_options(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 942 | |
| 943 | /* |
| 944 | * Using an existing client. Make sure ->pg_pools is up to |
| 945 | * date before we look up the pool id in do_rbd_add(). |
| 946 | */ |
| 947 | ret = wait_for_latest_osdmap(rbdc->client); |
| 948 | if (ret) { |
| 949 | rbd_warn(NULL, "failed to get latest osdmap: %d", ret); |
| 950 | rbd_put_client(rbdc); |
| 951 | rbdc = ERR_PTR(ret); |
| 952 | } |
| 953 | } else { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 954 | rbdc = rbd_client_create(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 955 | } |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 956 | mutex_unlock(&client_mutex); |
| 957 | |
| 958 | return rbdc; |
| 959 | } |
| 960 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 961 | static bool rbd_image_format_valid(u32 image_format) |
| 962 | { |
| 963 | return image_format == 1 || image_format == 2; |
| 964 | } |
| 965 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 966 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 967 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 968 | size_t size; |
| 969 | u32 snap_count; |
| 970 | |
| 971 | /* The header has to start with the magic rbd header text */ |
| 972 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 973 | return false; |
| 974 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 975 | /* The bio layer requires at least sector-sized I/O */ |
| 976 | |
| 977 | if (ondisk->options.order < SECTOR_SHIFT) |
| 978 | return false; |
| 979 | |
| 980 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 981 | |
| 982 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 983 | return false; |
| 984 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 985 | /* |
| 986 | * The size of a snapshot header has to fit in a size_t, and |
| 987 | * that limits the number of snapshots. |
| 988 | */ |
| 989 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 990 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 991 | if (snap_count > size / sizeof (__le64)) |
| 992 | return false; |
| 993 | |
| 994 | /* |
| 995 | * Not only that, but the size of the entire the snapshot |
| 996 | * header must also be representable in a size_t. |
| 997 | */ |
| 998 | size -= snap_count * sizeof (__le64); |
| 999 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 1000 | return false; |
| 1001 | |
| 1002 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1005 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1006 | * returns the size of an object in the image |
| 1007 | */ |
| 1008 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 1009 | { |
| 1010 | return 1U << header->obj_order; |
| 1011 | } |
| 1012 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1013 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 1014 | { |
| 1015 | if (rbd_dev->header.stripe_unit == 0 || |
| 1016 | rbd_dev->header.stripe_count == 0) { |
| 1017 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 1018 | rbd_dev->header.stripe_count = 1; |
| 1019 | } |
| 1020 | |
| 1021 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 1022 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 1023 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1024 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 1025 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1026 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 1027 | } |
| 1028 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1029 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1030 | * Fill an rbd image header with information from the given format 1 |
| 1031 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1032 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1033 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1034 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1035 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1036 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | bool first_time = header->object_prefix == NULL; |
| 1038 | struct ceph_snap_context *snapc; |
| 1039 | char *object_prefix = NULL; |
| 1040 | char *snap_names = NULL; |
| 1041 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1042 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1043 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1044 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1045 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | /* Allocate this now to avoid having to handle failure below */ |
| 1047 | |
| 1048 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1049 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1050 | sizeof(ondisk->object_prefix), |
| 1051 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1052 | if (!object_prefix) |
| 1053 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1057 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1058 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1060 | if (!snapc) |
| 1061 | goto out_err; |
| 1062 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1063 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1064 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1065 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1066 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1067 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1068 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | if (snap_names_len > (u64)SIZE_MAX) |
| 1070 | goto out_2big; |
| 1071 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1072 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1073 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | |
| 1075 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1076 | snap_sizes = kmalloc_array(snap_count, |
| 1077 | sizeof(*header->snap_sizes), |
| 1078 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1079 | if (!snap_sizes) |
| 1080 | goto out_err; |
| 1081 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1082 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1083 | * Copy the names, and fill in each snapshot's id |
| 1084 | * and size. |
| 1085 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1086 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1087 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1088 | * snap_names_len bytes beyond the end of the |
| 1089 | * snapshot id array, this memcpy() is safe. |
| 1090 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1091 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1092 | snaps = ondisk->snaps; |
| 1093 | for (i = 0; i < snap_count; i++) { |
| 1094 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1095 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1096 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1097 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1098 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1099 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1100 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1101 | if (first_time) { |
| 1102 | header->object_prefix = object_prefix; |
| 1103 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1104 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1105 | } else { |
| 1106 | ceph_put_snap_context(header->snapc); |
| 1107 | kfree(header->snap_names); |
| 1108 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1112 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1113 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1114 | header->snapc = snapc; |
| 1115 | header->snap_names = snap_names; |
| 1116 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1117 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1118 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1119 | out_2big: |
| 1120 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1121 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1122 | kfree(snap_sizes); |
| 1123 | kfree(snap_names); |
| 1124 | ceph_put_snap_context(snapc); |
| 1125 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1126 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1127 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1130 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1131 | { |
| 1132 | const char *snap_name; |
| 1133 | |
| 1134 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1135 | |
| 1136 | /* Skip over names until we find the one we are looking for */ |
| 1137 | |
| 1138 | snap_name = rbd_dev->header.snap_names; |
| 1139 | while (which--) |
| 1140 | snap_name += strlen(snap_name) + 1; |
| 1141 | |
| 1142 | return kstrdup(snap_name, GFP_KERNEL); |
| 1143 | } |
| 1144 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1145 | /* |
| 1146 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1147 | * Note that result is for snapshots in *descending* order. |
| 1148 | */ |
| 1149 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1150 | { |
| 1151 | u64 snap_id1 = *(u64 *)s1; |
| 1152 | u64 snap_id2 = *(u64 *)s2; |
| 1153 | |
| 1154 | if (snap_id1 < snap_id2) |
| 1155 | return 1; |
| 1156 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Search a snapshot context to see if the given snapshot id is |
| 1161 | * present. |
| 1162 | * |
| 1163 | * Returns the position of the snapshot id in the array if it's found, |
| 1164 | * or BAD_SNAP_INDEX otherwise. |
| 1165 | * |
| 1166 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1167 | * reverse order, highest snapshot id first. |
| 1168 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1169 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1170 | { |
| 1171 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1172 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1173 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1174 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1175 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1176 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1177 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1180 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1181 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1182 | { |
| 1183 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1184 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1185 | |
| 1186 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1187 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1188 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1189 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1190 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1191 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1194 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1195 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1196 | if (snap_id == CEPH_NOSNAP) |
| 1197 | return RBD_SNAP_HEAD_NAME; |
| 1198 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1199 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1200 | if (rbd_dev->image_format == 1) |
| 1201 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1202 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1203 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1204 | } |
| 1205 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1206 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1207 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1208 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1209 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1210 | if (snap_id == CEPH_NOSNAP) { |
| 1211 | *snap_size = rbd_dev->header.image_size; |
| 1212 | } else if (rbd_dev->image_format == 1) { |
| 1213 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1214 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1215 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1216 | if (which == BAD_SNAP_INDEX) |
| 1217 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1218 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1219 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1220 | } else { |
| 1221 | u64 size = 0; |
| 1222 | int ret; |
| 1223 | |
| 1224 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
| 1228 | *snap_size = size; |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1234 | u64 *snap_features) |
| 1235 | { |
| 1236 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1237 | if (snap_id == CEPH_NOSNAP) { |
| 1238 | *snap_features = rbd_dev->header.features; |
| 1239 | } else if (rbd_dev->image_format == 1) { |
| 1240 | *snap_features = 0; /* No features for format 1 */ |
| 1241 | } else { |
| 1242 | u64 features = 0; |
| 1243 | int ret; |
| 1244 | |
| 1245 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1246 | if (ret) |
| 1247 | return ret; |
| 1248 | |
| 1249 | *snap_features = features; |
| 1250 | } |
| 1251 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1254 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1255 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1256 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1257 | u64 size = 0; |
| 1258 | u64 features = 0; |
| 1259 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1260 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1261 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1262 | if (ret) |
| 1263 | return ret; |
| 1264 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | |
| 1268 | rbd_dev->mapping.size = size; |
| 1269 | rbd_dev->mapping.features = features; |
| 1270 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1271 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1274 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1275 | { |
| 1276 | rbd_dev->mapping.size = 0; |
| 1277 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1278 | } |
| 1279 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1280 | static void zero_bvec(struct bio_vec *bv) |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1281 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1282 | void *buf; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1283 | unsigned long flags; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1284 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1285 | buf = bvec_kmap_irq(bv, &flags); |
| 1286 | memset(buf, 0, bv->bv_len); |
| 1287 | flush_dcache_page(bv->bv_page); |
| 1288 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1291 | 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] | 1292 | { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1293 | struct ceph_bio_iter it = *bio_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1294 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1295 | ceph_bio_iter_advance(&it, off); |
| 1296 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1297 | zero_bvec(&bv); |
| 1298 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1299 | } |
| 1300 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1301 | 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] | 1302 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1303 | struct ceph_bvec_iter it = *bvec_pos; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1304 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1305 | ceph_bvec_iter_advance(&it, off); |
| 1306 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1307 | zero_bvec(&bv); |
| 1308 | })); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1309 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1310 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1311 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1312 | * 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] | 1313 | * (private) bio_vec array. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1314 | * |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1315 | * @off is relative to the start of the data buffer. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1316 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1317 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1318 | u32 bytes) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1319 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1320 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1321 | case OBJ_REQUEST_BIO: |
| 1322 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1323 | break; |
| 1324 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1325 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1326 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1327 | break; |
| 1328 | default: |
| 1329 | rbd_assert(0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1330 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1334 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1335 | { |
| 1336 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1337 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1338 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1339 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1340 | } |
| 1341 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1342 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1343 | { |
| 1344 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1345 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1346 | kref_get(&img_request->kref); |
| 1347 | } |
| 1348 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1349 | static void rbd_img_request_destroy(struct kref *kref); |
| 1350 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1351 | { |
| 1352 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1353 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1354 | kref_read(&img_request->kref)); |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1355 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1359 | struct rbd_obj_request *obj_request) |
| 1360 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1361 | rbd_assert(obj_request->img_request == NULL); |
| 1362 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1363 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1364 | obj_request->img_request = img_request; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1365 | img_request->pending_count++; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1366 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1370 | struct rbd_obj_request *obj_request) |
| 1371 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1372 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1373 | list_del(&obj_request->ex.oe_item); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1374 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1375 | rbd_obj_request_put(obj_request); |
| 1376 | } |
| 1377 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1378 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1379 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1380 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1381 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1382 | 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] | 1383 | obj_request, obj_request->ex.oe_objno, obj_request->ex.oe_off, |
| 1384 | obj_request->ex.oe_len, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1385 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1386 | } |
| 1387 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1388 | /* |
| 1389 | * The default/initial value for all image request flags is 0. Each |
| 1390 | * is conditionally set to 1 at image request initialization time |
| 1391 | * and currently never change thereafter. |
| 1392 | */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1393 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1394 | { |
| 1395 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1396 | smp_mb(); |
| 1397 | } |
| 1398 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1399 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1400 | { |
| 1401 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1402 | smp_mb(); |
| 1403 | } |
| 1404 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1405 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1406 | { |
| 1407 | smp_mb(); |
| 1408 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1409 | } |
| 1410 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1411 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1412 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1413 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1414 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1415 | return !obj_req->ex.oe_off && |
| 1416 | obj_req->ex.oe_len == rbd_dev->layout.object_size; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1419 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1420 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1421 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1422 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1423 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1424 | rbd_dev->layout.object_size; |
| 1425 | } |
| 1426 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1427 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
| 1428 | { |
| 1429 | return ceph_file_extents_bytes(obj_req->img_extents, |
| 1430 | obj_req->num_img_extents); |
| 1431 | } |
| 1432 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1433 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1434 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1435 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1436 | case OBJ_OP_READ: |
| 1437 | return false; |
| 1438 | case OBJ_OP_WRITE: |
| 1439 | case OBJ_OP_DISCARD: |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1440 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1441 | return true; |
| 1442 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 1443 | BUG(); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1444 | } |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1445 | } |
| 1446 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1447 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1448 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1449 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1450 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1451 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
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 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1454 | osd_req->r_result, obj_req); |
| 1455 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1456 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1457 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1458 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1459 | obj_req->xferred = osd_req->r_result; |
| 1460 | else |
| 1461 | /* |
| 1462 | * Writes aren't allowed to return a data payload. In some |
| 1463 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1464 | * a stat response makes it through, but we don't care. |
| 1465 | */ |
| 1466 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1467 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1468 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1469 | } |
| 1470 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1471 | 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] | 1472 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1473 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1474 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1475 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1476 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1480 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1481 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1482 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1483 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Arnd Bergmann | fac02dd | 2018-07-13 22:18:37 +0200 | [diff] [blame] | 1484 | ktime_get_real_ts64(&osd_req->r_mtime); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1485 | osd_req->r_data_offset = obj_request->ex.oe_off; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1488 | static struct ceph_osd_request * |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame^] | 1489 | __rbd_osd_req_create(struct rbd_obj_request *obj_req, |
| 1490 | struct ceph_snap_context *snapc, unsigned int num_ops) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1491 | { |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame^] | 1492 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1493 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1494 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1495 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1496 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1497 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame^] | 1498 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1499 | if (!req) |
| 1500 | return NULL; |
| 1501 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1502 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1503 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1504 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1505 | /* |
| 1506 | * Data objects may be stored in a separate pool, but always in |
| 1507 | * the same namespace in that pool as the header in its pool. |
| 1508 | */ |
| 1509 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1510 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1511 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1512 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1513 | rbd_dev->header.object_prefix, obj_req->ex.oe_objno)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1514 | goto err_req; |
| 1515 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1516 | return req; |
| 1517 | |
| 1518 | err_req: |
| 1519 | ceph_osdc_put_request(req); |
| 1520 | return NULL; |
| 1521 | } |
| 1522 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame^] | 1523 | static struct ceph_osd_request * |
| 1524 | rbd_osd_req_create(struct rbd_obj_request *obj_req, unsigned int num_ops) |
| 1525 | { |
| 1526 | return __rbd_osd_req_create(obj_req, obj_req->img_request->snapc, |
| 1527 | num_ops); |
| 1528 | } |
| 1529 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1530 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1531 | { |
| 1532 | ceph_osdc_put_request(osd_req); |
| 1533 | } |
| 1534 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1535 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1536 | { |
| 1537 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1538 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1539 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1540 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1541 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1542 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1543 | ceph_object_extent_init(&obj_request->ex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1544 | kref_init(&obj_request->kref); |
| 1545 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1546 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1547 | return obj_request; |
| 1548 | } |
| 1549 | |
| 1550 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1551 | { |
| 1552 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1553 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1554 | |
| 1555 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1556 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1557 | dout("%s: obj %p\n", __func__, obj_request); |
| 1558 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1559 | if (obj_request->osd_req) |
| 1560 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1561 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1562 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1563 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1564 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1565 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1566 | break; /* Nothing to do */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1567 | case OBJ_REQUEST_OWN_BVECS: |
| 1568 | kfree(obj_request->bvec_pos.bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1569 | break; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1570 | default: |
| 1571 | rbd_assert(0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1572 | } |
| 1573 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1574 | kfree(obj_request->img_extents); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1575 | if (obj_request->copyup_bvecs) { |
| 1576 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1577 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1578 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1579 | } |
| 1580 | kfree(obj_request->copyup_bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1581 | } |
| 1582 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1583 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1584 | } |
| 1585 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1586 | /* It's OK to call this for a device with no parent */ |
| 1587 | |
| 1588 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1589 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1590 | { |
| 1591 | rbd_dev_remove_parent(rbd_dev); |
| 1592 | rbd_spec_put(rbd_dev->parent_spec); |
| 1593 | rbd_dev->parent_spec = NULL; |
| 1594 | rbd_dev->parent_overlap = 0; |
| 1595 | } |
| 1596 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1597 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1598 | * Parent image reference counting is used to determine when an |
| 1599 | * image's parent fields can be safely torn down--after there are no |
| 1600 | * more in-flight requests to the parent image. When the last |
| 1601 | * reference is dropped, cleaning them up is safe. |
| 1602 | */ |
| 1603 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1604 | { |
| 1605 | int counter; |
| 1606 | |
| 1607 | if (!rbd_dev->parent_spec) |
| 1608 | return; |
| 1609 | |
| 1610 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1611 | if (counter > 0) |
| 1612 | return; |
| 1613 | |
| 1614 | /* Last reference; clean up parent data structures */ |
| 1615 | |
| 1616 | if (!counter) |
| 1617 | rbd_dev_unparent(rbd_dev); |
| 1618 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1619 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * If an image has a non-zero parent overlap, get a reference to its |
| 1624 | * parent. |
| 1625 | * |
| 1626 | * Returns true if the rbd device has a parent with a non-zero |
| 1627 | * overlap and a reference for it was successfully taken, or |
| 1628 | * false otherwise. |
| 1629 | */ |
| 1630 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1631 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1632 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1633 | |
| 1634 | if (!rbd_dev->parent_spec) |
| 1635 | return false; |
| 1636 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1637 | down_read(&rbd_dev->header_rwsem); |
| 1638 | if (rbd_dev->parent_overlap) |
| 1639 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1640 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1641 | |
| 1642 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1643 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1644 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1645 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1646 | } |
| 1647 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1648 | /* |
| 1649 | * Caller is responsible for filling in the list of object requests |
| 1650 | * that comprises the image request, and the Linux request pointer |
| 1651 | * (if there is one). |
| 1652 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1653 | static struct rbd_img_request *rbd_img_request_create( |
| 1654 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1655 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1656 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1657 | { |
| 1658 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1659 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1660 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1661 | if (!img_request) |
| 1662 | return NULL; |
| 1663 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1664 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1665 | img_request->op_type = op_type; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1666 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1667 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1668 | else |
| 1669 | img_request->snapc = snapc; |
| 1670 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1671 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1672 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1673 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1674 | spin_lock_init(&img_request->completion_lock); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1675 | INIT_LIST_HEAD(&img_request->object_extents); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1676 | kref_init(&img_request->kref); |
| 1677 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1678 | dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, |
| 1679 | obj_op_name(op_type), img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1680 | return img_request; |
| 1681 | } |
| 1682 | |
| 1683 | static void rbd_img_request_destroy(struct kref *kref) |
| 1684 | { |
| 1685 | struct rbd_img_request *img_request; |
| 1686 | struct rbd_obj_request *obj_request; |
| 1687 | struct rbd_obj_request *next_obj_request; |
| 1688 | |
| 1689 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1690 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1691 | dout("%s: img %p\n", __func__, img_request); |
| 1692 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1693 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1694 | rbd_img_obj_request_del(img_request, obj_request); |
| 1695 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1696 | if (img_request_layered_test(img_request)) { |
| 1697 | img_request_layered_clear(img_request); |
| 1698 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1699 | } |
| 1700 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1701 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1702 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1703 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1704 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1705 | } |
| 1706 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1707 | static void prune_extents(struct ceph_file_extent *img_extents, |
| 1708 | u32 *num_img_extents, u64 overlap) |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1709 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1710 | u32 cnt = *num_img_extents; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1711 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1712 | /* drop extents completely beyond the overlap */ |
| 1713 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) |
| 1714 | cnt--; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1715 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1716 | if (cnt) { |
| 1717 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1718 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1719 | /* trim final overlapping extent */ |
| 1720 | if (ex->fe_off + ex->fe_len > overlap) |
| 1721 | ex->fe_len = overlap - ex->fe_off; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1722 | } |
| 1723 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1724 | *num_img_extents = cnt; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1725 | } |
| 1726 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1727 | /* |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1728 | * Determine the byte range(s) covered by either just the object extent |
| 1729 | * or the entire object in the parent image. |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1730 | */ |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1731 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, |
| 1732 | bool entire) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1733 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1734 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1735 | int ret; |
| 1736 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1737 | if (!rbd_dev->parent_overlap) |
| 1738 | return 0; |
| 1739 | |
| 1740 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
| 1741 | entire ? 0 : obj_req->ex.oe_off, |
| 1742 | entire ? rbd_dev->layout.object_size : |
| 1743 | obj_req->ex.oe_len, |
| 1744 | &obj_req->img_extents, |
| 1745 | &obj_req->num_img_extents); |
| 1746 | if (ret) |
| 1747 | return ret; |
| 1748 | |
| 1749 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 1750 | rbd_dev->parent_overlap); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1754 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1755 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1756 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1757 | case OBJ_REQUEST_BIO: |
| 1758 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1759 | &obj_req->bio_pos, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1760 | obj_req->ex.oe_len); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1761 | break; |
| 1762 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1763 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1764 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1765 | obj_req->ex.oe_len); |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1766 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1767 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1768 | &obj_req->bvec_pos); |
| 1769 | break; |
| 1770 | default: |
| 1771 | rbd_assert(0); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1776 | { |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame^] | 1777 | obj_req->osd_req = __rbd_osd_req_create(obj_req, NULL, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1778 | if (!obj_req->osd_req) |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1779 | return -ENOMEM; |
| 1780 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1781 | 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] | 1782 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1783 | rbd_osd_req_setup_data(obj_req, 0); |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1784 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1785 | rbd_osd_req_format_read(obj_req); |
| 1786 | return 0; |
| 1787 | } |
| 1788 | |
| 1789 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1790 | unsigned int which) |
| 1791 | { |
| 1792 | struct page **pages; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1793 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1794 | /* |
| 1795 | * The response data for a STAT call consists of: |
| 1796 | * le64 length; |
| 1797 | * struct { |
| 1798 | * le32 tv_sec; |
| 1799 | * le32 tv_nsec; |
| 1800 | * } mtime; |
| 1801 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1802 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1803 | if (IS_ERR(pages)) |
| 1804 | return PTR_ERR(pages); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1805 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1806 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1807 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1808 | 8 + sizeof(struct ceph_timespec), |
| 1809 | 0, false, true); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1810 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1811 | } |
| 1812 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1813 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 1814 | unsigned int which) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1815 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1816 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1817 | u16 opcode; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1818 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1819 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 1820 | rbd_dev->layout.object_size, |
| 1821 | rbd_dev->layout.object_size); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1822 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1823 | if (rbd_obj_is_entire(obj_req)) |
| 1824 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1825 | else |
| 1826 | opcode = CEPH_OSD_OP_WRITE; |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1827 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1828 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1829 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1830 | rbd_osd_req_setup_data(obj_req, which++); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1831 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1832 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1833 | rbd_osd_req_format_write(obj_req); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1834 | } |
| 1835 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1836 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1837 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1838 | unsigned int num_osd_ops, which = 0; |
| 1839 | int ret; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 1840 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1841 | /* reverse map the entire object onto the parent */ |
| 1842 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1843 | if (ret) |
| 1844 | return ret; |
| 1845 | |
| 1846 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1847 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1848 | num_osd_ops = 3; /* stat + setallochint + write/writefull */ |
| 1849 | } else { |
| 1850 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1851 | num_osd_ops = 2; /* setallochint + write/writefull */ |
| 1852 | } |
| 1853 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1854 | 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] | 1855 | if (!obj_req->osd_req) |
| 1856 | return -ENOMEM; |
| 1857 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1858 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1859 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1860 | if (ret) |
| 1861 | return ret; |
| 1862 | } |
| 1863 | |
| 1864 | __rbd_obj_setup_write(obj_req, which); |
| 1865 | return 0; |
| 1866 | } |
| 1867 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1868 | static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req) |
| 1869 | { |
| 1870 | return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE : |
| 1871 | CEPH_OSD_OP_ZERO; |
| 1872 | } |
| 1873 | |
| 1874 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 1875 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1876 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1877 | u64 off = obj_req->ex.oe_off; |
| 1878 | u64 next_off = obj_req->ex.oe_off + obj_req->ex.oe_len; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1879 | int ret; |
| 1880 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1881 | /* |
| 1882 | * Align the range to alloc_size boundary and punt on discards |
| 1883 | * that are too small to free up any space. |
| 1884 | * |
| 1885 | * alloc_size == object_size && is_tail() is a special case for |
| 1886 | * filestore with filestore_punch_hole = false, needed to allow |
| 1887 | * truncate (in addition to delete). |
| 1888 | */ |
| 1889 | if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size || |
| 1890 | !rbd_obj_is_tail(obj_req)) { |
| 1891 | off = round_up(off, rbd_dev->opts->alloc_size); |
| 1892 | next_off = round_down(next_off, rbd_dev->opts->alloc_size); |
| 1893 | if (off >= next_off) |
| 1894 | return 1; |
| 1895 | } |
| 1896 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1897 | /* reverse map the entire object onto the parent */ |
| 1898 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1899 | if (ret) |
| 1900 | return ret; |
| 1901 | |
| 1902 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
| 1903 | if (!obj_req->osd_req) |
| 1904 | return -ENOMEM; |
| 1905 | |
| 1906 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) { |
| 1907 | osd_req_op_init(obj_req->osd_req, 0, CEPH_OSD_OP_DELETE, 0); |
| 1908 | } else { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1909 | dout("%s %p %llu~%llu -> %llu~%llu\n", __func__, |
| 1910 | obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len, |
| 1911 | off, next_off - off); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1912 | osd_req_op_extent_init(obj_req->osd_req, 0, |
| 1913 | truncate_or_zero_opcode(obj_req), |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1914 | off, next_off - off, 0, 0); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1915 | } |
| 1916 | |
| 1917 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1918 | rbd_osd_req_format_write(obj_req); |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | static void __rbd_obj_setup_zeroout(struct rbd_obj_request *obj_req, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1923 | unsigned int which) |
| 1924 | { |
| 1925 | u16 opcode; |
| 1926 | |
| 1927 | if (rbd_obj_is_entire(obj_req)) { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1928 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1929 | osd_req_op_init(obj_req->osd_req, which++, |
| 1930 | CEPH_OSD_OP_CREATE, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1931 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1932 | } else { |
| 1933 | osd_req_op_init(obj_req->osd_req, which++, |
| 1934 | CEPH_OSD_OP_DELETE, 0); |
| 1935 | opcode = 0; |
| 1936 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1937 | } else { |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1938 | opcode = truncate_or_zero_opcode(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | if (opcode) |
| 1942 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1943 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1944 | 0, 0); |
| 1945 | |
| 1946 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1947 | rbd_osd_req_format_write(obj_req); |
| 1948 | } |
| 1949 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1950 | static int rbd_obj_setup_zeroout(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1951 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1952 | unsigned int num_osd_ops, which = 0; |
| 1953 | int ret; |
| 1954 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1955 | /* reverse map the entire object onto the parent */ |
| 1956 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1957 | if (ret) |
| 1958 | return ret; |
| 1959 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1960 | if (rbd_obj_is_entire(obj_req)) { |
| 1961 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
Ilya Dryomov | 2bb1e56 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 1962 | if (obj_req->num_img_extents) |
| 1963 | num_osd_ops = 2; /* create + truncate */ |
| 1964 | else |
| 1965 | num_osd_ops = 1; /* delete */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1966 | } else { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1967 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1968 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1969 | num_osd_ops = 2; /* stat + truncate/zero */ |
| 1970 | } else { |
| 1971 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1972 | num_osd_ops = 1; /* truncate/zero */ |
| 1973 | } |
| 1974 | } |
| 1975 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1976 | 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] | 1977 | if (!obj_req->osd_req) |
| 1978 | return -ENOMEM; |
| 1979 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1980 | if (!rbd_obj_is_entire(obj_req) && obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1981 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1982 | if (ret) |
| 1983 | return ret; |
| 1984 | } |
| 1985 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1986 | __rbd_obj_setup_zeroout(obj_req, which); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1987 | return 0; |
| 1988 | } |
| 1989 | |
| 1990 | /* |
| 1991 | * For each object request in @img_req, allocate an OSD request, add |
| 1992 | * individual OSD ops and prepare them for submission. The number of |
| 1993 | * OSD ops depends on op_type and the overlap point (if any). |
| 1994 | */ |
| 1995 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 1996 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1997 | struct rbd_obj_request *obj_req, *next_obj_req; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1998 | int ret; |
| 1999 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2000 | for_each_obj_request_safe(img_req, obj_req, next_obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2001 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2002 | case OBJ_OP_READ: |
| 2003 | ret = rbd_obj_setup_read(obj_req); |
| 2004 | break; |
| 2005 | case OBJ_OP_WRITE: |
| 2006 | ret = rbd_obj_setup_write(obj_req); |
| 2007 | break; |
| 2008 | case OBJ_OP_DISCARD: |
| 2009 | ret = rbd_obj_setup_discard(obj_req); |
| 2010 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2011 | case OBJ_OP_ZEROOUT: |
| 2012 | ret = rbd_obj_setup_zeroout(obj_req); |
| 2013 | break; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2014 | default: |
| 2015 | rbd_assert(0); |
| 2016 | } |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2017 | if (ret < 0) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2018 | return ret; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2019 | if (ret > 0) { |
| 2020 | img_req->xferred += obj_req->ex.oe_len; |
| 2021 | img_req->pending_count--; |
| 2022 | rbd_img_obj_request_del(img_req, obj_req); |
| 2023 | continue; |
| 2024 | } |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 2025 | |
| 2026 | ret = ceph_osdc_alloc_messages(obj_req->osd_req, GFP_NOIO); |
| 2027 | if (ret) |
| 2028 | return ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2034 | union rbd_img_fill_iter { |
| 2035 | struct ceph_bio_iter bio_iter; |
| 2036 | struct ceph_bvec_iter bvec_iter; |
| 2037 | }; |
| 2038 | |
| 2039 | struct rbd_img_fill_ctx { |
| 2040 | enum obj_request_type pos_type; |
| 2041 | union rbd_img_fill_iter *pos; |
| 2042 | union rbd_img_fill_iter iter; |
| 2043 | ceph_object_extent_fn_t set_pos_fn; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2044 | ceph_object_extent_fn_t count_fn; |
| 2045 | ceph_object_extent_fn_t copy_fn; |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2046 | }; |
| 2047 | |
| 2048 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
| 2049 | { |
| 2050 | struct rbd_img_request *img_req = arg; |
| 2051 | struct rbd_obj_request *obj_req; |
| 2052 | |
| 2053 | obj_req = rbd_obj_request_create(); |
| 2054 | if (!obj_req) |
| 2055 | return NULL; |
| 2056 | |
| 2057 | rbd_img_obj_request_add(img_req, obj_req); |
| 2058 | return &obj_req->ex; |
| 2059 | } |
| 2060 | |
| 2061 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2062 | * While su != os && sc == 1 is technically not fancy (it's the same |
| 2063 | * layout as su == os && sc == 1), we can't use the nocopy path for it |
| 2064 | * because ->set_pos_fn() should be called only once per object. |
| 2065 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so |
| 2066 | * treat su != os && sc == 1 as fancy. |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2067 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2068 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) |
| 2069 | { |
| 2070 | return l->stripe_unit != l->object_size; |
| 2071 | } |
| 2072 | |
| 2073 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
| 2074 | struct ceph_file_extent *img_extents, |
| 2075 | u32 num_img_extents, |
| 2076 | struct rbd_img_fill_ctx *fctx) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2077 | { |
| 2078 | u32 i; |
| 2079 | int ret; |
| 2080 | |
| 2081 | img_req->data_type = fctx->pos_type; |
| 2082 | |
| 2083 | /* |
| 2084 | * Create object requests and set each object request's starting |
| 2085 | * position in the provided bio (list) or bio_vec array. |
| 2086 | */ |
| 2087 | fctx->iter = *fctx->pos; |
| 2088 | for (i = 0; i < num_img_extents; i++) { |
| 2089 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, |
| 2090 | img_extents[i].fe_off, |
| 2091 | img_extents[i].fe_len, |
| 2092 | &img_req->object_extents, |
| 2093 | alloc_object_extent, img_req, |
| 2094 | fctx->set_pos_fn, &fctx->iter); |
| 2095 | if (ret) |
| 2096 | return ret; |
| 2097 | } |
| 2098 | |
| 2099 | return __rbd_img_fill_request(img_req); |
| 2100 | } |
| 2101 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2102 | /* |
| 2103 | * Map a list of image extents to a list of object extents, create the |
| 2104 | * corresponding object requests (normally each to a different object, |
| 2105 | * but not always) and add them to @img_req. For each object request, |
| 2106 | * set up its data descriptor to point to the corresponding chunk(s) of |
| 2107 | * @fctx->pos data buffer. |
| 2108 | * |
| 2109 | * Because ceph_file_to_extents() will merge adjacent object extents |
| 2110 | * together, each object request's data descriptor may point to multiple |
| 2111 | * different chunks of @fctx->pos data buffer. |
| 2112 | * |
| 2113 | * @fctx->pos data buffer is assumed to be large enough. |
| 2114 | */ |
| 2115 | static int rbd_img_fill_request(struct rbd_img_request *img_req, |
| 2116 | struct ceph_file_extent *img_extents, |
| 2117 | u32 num_img_extents, |
| 2118 | struct rbd_img_fill_ctx *fctx) |
| 2119 | { |
| 2120 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2121 | struct rbd_obj_request *obj_req; |
| 2122 | u32 i; |
| 2123 | int ret; |
| 2124 | |
| 2125 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
| 2126 | !rbd_layout_is_fancy(&rbd_dev->layout)) |
| 2127 | return rbd_img_fill_request_nocopy(img_req, img_extents, |
| 2128 | num_img_extents, fctx); |
| 2129 | |
| 2130 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
| 2131 | |
| 2132 | /* |
| 2133 | * Create object requests and determine ->bvec_count for each object |
| 2134 | * request. Note that ->bvec_count sum over all object requests may |
| 2135 | * be greater than the number of bio_vecs in the provided bio (list) |
| 2136 | * or bio_vec array because when mapped, those bio_vecs can straddle |
| 2137 | * stripe unit boundaries. |
| 2138 | */ |
| 2139 | fctx->iter = *fctx->pos; |
| 2140 | for (i = 0; i < num_img_extents; i++) { |
| 2141 | ret = ceph_file_to_extents(&rbd_dev->layout, |
| 2142 | img_extents[i].fe_off, |
| 2143 | img_extents[i].fe_len, |
| 2144 | &img_req->object_extents, |
| 2145 | alloc_object_extent, img_req, |
| 2146 | fctx->count_fn, &fctx->iter); |
| 2147 | if (ret) |
| 2148 | return ret; |
| 2149 | } |
| 2150 | |
| 2151 | for_each_obj_request(img_req, obj_req) { |
| 2152 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, |
| 2153 | sizeof(*obj_req->bvec_pos.bvecs), |
| 2154 | GFP_NOIO); |
| 2155 | if (!obj_req->bvec_pos.bvecs) |
| 2156 | return -ENOMEM; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2160 | * Fill in each object request's private bio_vec array, splitting and |
| 2161 | * rearranging the provided bio_vecs in stripe unit chunks as needed. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2162 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2163 | fctx->iter = *fctx->pos; |
| 2164 | for (i = 0; i < num_img_extents; i++) { |
| 2165 | ret = ceph_iterate_extents(&rbd_dev->layout, |
| 2166 | img_extents[i].fe_off, |
| 2167 | img_extents[i].fe_len, |
| 2168 | &img_req->object_extents, |
| 2169 | fctx->copy_fn, &fctx->iter); |
| 2170 | if (ret) |
| 2171 | return ret; |
| 2172 | } |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2173 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2174 | return __rbd_img_fill_request(img_req); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2175 | } |
| 2176 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2177 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, |
| 2178 | u64 off, u64 len) |
| 2179 | { |
| 2180 | struct ceph_file_extent ex = { off, len }; |
| 2181 | union rbd_img_fill_iter dummy; |
| 2182 | struct rbd_img_fill_ctx fctx = { |
| 2183 | .pos_type = OBJ_REQUEST_NODATA, |
| 2184 | .pos = &dummy, |
| 2185 | }; |
| 2186 | |
| 2187 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); |
| 2188 | } |
| 2189 | |
| 2190 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2191 | { |
| 2192 | struct rbd_obj_request *obj_req = |
| 2193 | container_of(ex, struct rbd_obj_request, ex); |
| 2194 | struct ceph_bio_iter *it = arg; |
| 2195 | |
| 2196 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2197 | obj_req->bio_pos = *it; |
| 2198 | ceph_bio_iter_advance(it, bytes); |
| 2199 | } |
| 2200 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2201 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2202 | { |
| 2203 | struct rbd_obj_request *obj_req = |
| 2204 | container_of(ex, struct rbd_obj_request, ex); |
| 2205 | struct ceph_bio_iter *it = arg; |
| 2206 | |
| 2207 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2208 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2209 | obj_req->bvec_count++; |
| 2210 | })); |
| 2211 | |
| 2212 | } |
| 2213 | |
| 2214 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2215 | { |
| 2216 | struct rbd_obj_request *obj_req = |
| 2217 | container_of(ex, struct rbd_obj_request, ex); |
| 2218 | struct ceph_bio_iter *it = arg; |
| 2219 | |
| 2220 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2221 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2222 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2223 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2224 | })); |
| 2225 | } |
| 2226 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2227 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2228 | struct ceph_file_extent *img_extents, |
| 2229 | u32 num_img_extents, |
| 2230 | struct ceph_bio_iter *bio_pos) |
| 2231 | { |
| 2232 | struct rbd_img_fill_ctx fctx = { |
| 2233 | .pos_type = OBJ_REQUEST_BIO, |
| 2234 | .pos = (union rbd_img_fill_iter *)bio_pos, |
| 2235 | .set_pos_fn = set_bio_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2236 | .count_fn = count_bio_bvecs, |
| 2237 | .copy_fn = copy_bio_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2238 | }; |
| 2239 | |
| 2240 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2241 | &fctx); |
| 2242 | } |
| 2243 | |
| 2244 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2245 | u64 off, u64 len, struct bio *bio) |
| 2246 | { |
| 2247 | struct ceph_file_extent ex = { off, len }; |
| 2248 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; |
| 2249 | |
| 2250 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
| 2251 | } |
| 2252 | |
| 2253 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2254 | { |
| 2255 | struct rbd_obj_request *obj_req = |
| 2256 | container_of(ex, struct rbd_obj_request, ex); |
| 2257 | struct ceph_bvec_iter *it = arg; |
| 2258 | |
| 2259 | obj_req->bvec_pos = *it; |
| 2260 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); |
| 2261 | ceph_bvec_iter_advance(it, bytes); |
| 2262 | } |
| 2263 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2264 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2265 | { |
| 2266 | struct rbd_obj_request *obj_req = |
| 2267 | container_of(ex, struct rbd_obj_request, ex); |
| 2268 | struct ceph_bvec_iter *it = arg; |
| 2269 | |
| 2270 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2271 | obj_req->bvec_count++; |
| 2272 | })); |
| 2273 | } |
| 2274 | |
| 2275 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2276 | { |
| 2277 | struct rbd_obj_request *obj_req = |
| 2278 | container_of(ex, struct rbd_obj_request, ex); |
| 2279 | struct ceph_bvec_iter *it = arg; |
| 2280 | |
| 2281 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2282 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2283 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2284 | })); |
| 2285 | } |
| 2286 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2287 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2288 | struct ceph_file_extent *img_extents, |
| 2289 | u32 num_img_extents, |
| 2290 | struct ceph_bvec_iter *bvec_pos) |
| 2291 | { |
| 2292 | struct rbd_img_fill_ctx fctx = { |
| 2293 | .pos_type = OBJ_REQUEST_BVECS, |
| 2294 | .pos = (union rbd_img_fill_iter *)bvec_pos, |
| 2295 | .set_pos_fn = set_bvec_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2296 | .count_fn = count_bvecs, |
| 2297 | .copy_fn = copy_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2298 | }; |
| 2299 | |
| 2300 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2301 | &fctx); |
| 2302 | } |
| 2303 | |
| 2304 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2305 | struct ceph_file_extent *img_extents, |
| 2306 | u32 num_img_extents, |
| 2307 | struct bio_vec *bvecs) |
| 2308 | { |
| 2309 | struct ceph_bvec_iter it = { |
| 2310 | .bvecs = bvecs, |
| 2311 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, |
| 2312 | num_img_extents) }, |
| 2313 | }; |
| 2314 | |
| 2315 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
| 2316 | &it); |
| 2317 | } |
| 2318 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2319 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2320 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2321 | struct rbd_obj_request *obj_request; |
| 2322 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2323 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2324 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2325 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2326 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2327 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2328 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2329 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2330 | } |
| 2331 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2332 | 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] | 2333 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2334 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2335 | struct rbd_img_request *child_img_req; |
| 2336 | int ret; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2337 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2338 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
| 2339 | OBJ_OP_READ, NULL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2340 | if (!child_img_req) |
| 2341 | return -ENOMEM; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2342 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2343 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
| 2344 | child_img_req->obj_request = obj_req; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2345 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2346 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2347 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2348 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2349 | ret = __rbd_img_fill_from_bio(child_img_req, |
| 2350 | obj_req->img_extents, |
| 2351 | obj_req->num_img_extents, |
| 2352 | &obj_req->bio_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2353 | break; |
| 2354 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2355 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2356 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
| 2357 | obj_req->img_extents, |
| 2358 | obj_req->num_img_extents, |
| 2359 | &obj_req->bvec_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2360 | break; |
| 2361 | default: |
| 2362 | rbd_assert(0); |
| 2363 | } |
| 2364 | } else { |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2365 | ret = rbd_img_fill_from_bvecs(child_img_req, |
| 2366 | obj_req->img_extents, |
| 2367 | obj_req->num_img_extents, |
| 2368 | obj_req->copyup_bvecs); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2369 | } |
| 2370 | if (ret) { |
| 2371 | rbd_img_request_put(child_img_req); |
| 2372 | return ret; |
| 2373 | } |
| 2374 | |
| 2375 | rbd_img_request_submit(child_img_req); |
| 2376 | return 0; |
| 2377 | } |
| 2378 | |
| 2379 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2380 | { |
| 2381 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2382 | int ret; |
| 2383 | |
| 2384 | if (obj_req->result == -ENOENT && |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2385 | rbd_dev->parent_overlap && !obj_req->tried_parent) { |
| 2386 | /* reverse map this object extent onto the parent */ |
| 2387 | ret = rbd_obj_calc_img_extents(obj_req, false); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2388 | if (ret) { |
| 2389 | obj_req->result = ret; |
| 2390 | return true; |
| 2391 | } |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2392 | |
| 2393 | if (obj_req->num_img_extents) { |
| 2394 | obj_req->tried_parent = true; |
| 2395 | ret = rbd_obj_read_from_parent(obj_req); |
| 2396 | if (ret) { |
| 2397 | obj_req->result = ret; |
| 2398 | return true; |
| 2399 | } |
| 2400 | return false; |
| 2401 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2402 | } |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2403 | |
| 2404 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2405 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2406 | * length of the request. A short read also implies zero-fill |
| 2407 | * to the end of the request. In both cases we update xferred |
| 2408 | * count to indicate the whole request was satisfied. |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2409 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2410 | if (obj_req->result == -ENOENT || |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2411 | (!obj_req->result && obj_req->xferred < obj_req->ex.oe_len)) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2412 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2413 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2414 | obj_req->ex.oe_len - obj_req->xferred); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2415 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2416 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | return true; |
| 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * copyup_bvecs pages are never highmem pages |
| 2424 | */ |
| 2425 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2426 | { |
| 2427 | struct ceph_bvec_iter it = { |
| 2428 | .bvecs = bvecs, |
| 2429 | .iter = { .bi_size = bytes }, |
| 2430 | }; |
| 2431 | |
| 2432 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2433 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2434 | bv.bv_len)) |
| 2435 | return false; |
| 2436 | })); |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
| 2440 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2441 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2442 | unsigned int num_osd_ops = obj_req->osd_req->r_num_ops; |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2443 | int ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2444 | |
| 2445 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2446 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
| 2447 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2448 | |
| 2449 | /* |
| 2450 | * Create a copyup request with the same number of OSD ops as |
| 2451 | * the original request. The original request was stat + op(s), |
| 2452 | * the new copyup request will be copyup + the same op(s). |
| 2453 | */ |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2454 | 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] | 2455 | if (!obj_req->osd_req) |
| 2456 | return -ENOMEM; |
| 2457 | |
Ilya Dryomov | 24639ce56 | 2018-09-26 19:12:07 +0200 | [diff] [blame] | 2458 | ret = osd_req_op_cls_init(obj_req->osd_req, 0, "rbd", "copyup"); |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2459 | if (ret) |
| 2460 | return ret; |
| 2461 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2462 | /* |
| 2463 | * Only send non-zero copyup data to save some I/O and network |
| 2464 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2465 | * existing. |
| 2466 | */ |
| 2467 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2468 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2469 | bytes = 0; |
| 2470 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2471 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
Ilya Dryomov | 0010f70 | 2018-05-04 16:57:30 +0200 | [diff] [blame] | 2472 | obj_req->copyup_bvecs, |
| 2473 | obj_req->copyup_bvec_count, |
| 2474 | bytes); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2475 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2476 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2477 | case OBJ_OP_WRITE: |
| 2478 | __rbd_obj_setup_write(obj_req, 1); |
| 2479 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2480 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2481 | rbd_assert(!rbd_obj_is_entire(obj_req)); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2482 | __rbd_obj_setup_zeroout(obj_req, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2483 | break; |
| 2484 | default: |
| 2485 | rbd_assert(0); |
| 2486 | } |
| 2487 | |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 2488 | ret = ceph_osdc_alloc_messages(obj_req->osd_req, GFP_NOIO); |
| 2489 | if (ret) |
| 2490 | return ret; |
| 2491 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2492 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2493 | return 0; |
| 2494 | } |
| 2495 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2496 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2497 | { |
| 2498 | u32 i; |
| 2499 | |
| 2500 | rbd_assert(!obj_req->copyup_bvecs); |
| 2501 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2502 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2503 | sizeof(*obj_req->copyup_bvecs), |
| 2504 | GFP_NOIO); |
| 2505 | if (!obj_req->copyup_bvecs) |
| 2506 | return -ENOMEM; |
| 2507 | |
| 2508 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2509 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2510 | |
| 2511 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2512 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2513 | return -ENOMEM; |
| 2514 | |
| 2515 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2516 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2517 | obj_overlap -= len; |
| 2518 | } |
| 2519 | |
| 2520 | rbd_assert(!obj_overlap); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2524 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2525 | { |
| 2526 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2527 | int ret; |
| 2528 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2529 | rbd_assert(obj_req->num_img_extents); |
| 2530 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 2531 | rbd_dev->parent_overlap); |
| 2532 | if (!obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2533 | /* |
| 2534 | * The overlap has become 0 (most likely because the |
| 2535 | * image has been flattened). Use rbd_obj_issue_copyup() |
| 2536 | * to re-submit the original write request -- the copyup |
| 2537 | * operation itself will be a no-op, since someone must |
| 2538 | * have populated the child object while we weren't |
| 2539 | * looking. Move to WRITE_FLAT state as we'll be done |
| 2540 | * with the operation once the null copyup completes. |
| 2541 | */ |
| 2542 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 2543 | return rbd_obj_issue_copyup(obj_req, 0); |
| 2544 | } |
| 2545 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2546 | 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] | 2547 | if (ret) |
| 2548 | return ret; |
| 2549 | |
| 2550 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2551 | return rbd_obj_read_from_parent(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2555 | { |
| 2556 | int ret; |
| 2557 | |
| 2558 | again: |
| 2559 | switch (obj_req->write_state) { |
| 2560 | case RBD_OBJ_WRITE_GUARD: |
| 2561 | rbd_assert(!obj_req->xferred); |
| 2562 | if (obj_req->result == -ENOENT) { |
| 2563 | /* |
| 2564 | * The target object doesn't exist. Read the data for |
| 2565 | * the entire target object up to the overlap point (if |
| 2566 | * any) from the parent, so we can use it for a copyup. |
| 2567 | */ |
| 2568 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2569 | if (ret) { |
| 2570 | obj_req->result = ret; |
| 2571 | return true; |
| 2572 | } |
| 2573 | return false; |
| 2574 | } |
| 2575 | /* fall through */ |
| 2576 | case RBD_OBJ_WRITE_FLAT: |
| 2577 | if (!obj_req->result) |
| 2578 | /* |
| 2579 | * There is no such thing as a successful short |
| 2580 | * write -- indicate the whole request was satisfied. |
| 2581 | */ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2582 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2583 | return true; |
| 2584 | case RBD_OBJ_WRITE_COPYUP: |
| 2585 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2586 | if (obj_req->result) |
| 2587 | goto again; |
| 2588 | |
| 2589 | rbd_assert(obj_req->xferred); |
| 2590 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2591 | if (ret) { |
| 2592 | obj_req->result = ret; |
Ilya Dryomov | 356889c | 2019-03-01 12:06:24 +0100 | [diff] [blame] | 2593 | obj_req->xferred = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2594 | return true; |
| 2595 | } |
| 2596 | return false; |
| 2597 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2598 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /* |
| 2603 | * Returns true if @obj_req is completed, or false otherwise. |
| 2604 | */ |
| 2605 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2606 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2607 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2608 | case OBJ_OP_READ: |
| 2609 | return rbd_obj_handle_read(obj_req); |
| 2610 | case OBJ_OP_WRITE: |
| 2611 | return rbd_obj_handle_write(obj_req); |
| 2612 | case OBJ_OP_DISCARD: |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2613 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2614 | if (rbd_obj_handle_write(obj_req)) { |
| 2615 | /* |
| 2616 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2617 | * a non-existent object is not a problem. |
| 2618 | */ |
| 2619 | if (obj_req->result == -ENOENT) { |
| 2620 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2621 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2622 | } |
| 2623 | return true; |
| 2624 | } |
| 2625 | return false; |
| 2626 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2627 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2628 | } |
| 2629 | } |
| 2630 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2631 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2632 | { |
| 2633 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2634 | |
| 2635 | rbd_assert((!obj_req->result && |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2636 | obj_req->xferred == obj_req->ex.oe_len) || |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2637 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2638 | if (!obj_req->result) { |
| 2639 | img_req->xferred += obj_req->xferred; |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2640 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2641 | } |
| 2642 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2643 | rbd_warn(img_req->rbd_dev, |
| 2644 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2645 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, |
| 2646 | 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] | 2647 | obj_req->xferred); |
| 2648 | if (!img_req->result) { |
| 2649 | img_req->result = obj_req->result; |
| 2650 | img_req->xferred = 0; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2651 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2652 | } |
| 2653 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2654 | 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] | 2655 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2656 | struct rbd_obj_request *obj_req = img_req->obj_request; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2657 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2658 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2659 | rbd_assert((!img_req->result && |
| 2660 | img_req->xferred == rbd_obj_img_extents_bytes(obj_req)) || |
| 2661 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2662 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2663 | obj_req->result = img_req->result; |
| 2664 | obj_req->xferred = img_req->xferred; |
| 2665 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2666 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2667 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2668 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2669 | { |
| 2670 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2671 | rbd_assert((!img_req->result && |
| 2672 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2673 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2674 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2675 | blk_mq_end_request(img_req->rq, |
| 2676 | errno_to_blk_status(img_req->result)); |
| 2677 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2678 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2679 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2680 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2681 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2682 | struct rbd_img_request *img_req; |
| 2683 | |
| 2684 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2685 | if (!__rbd_obj_handle_request(obj_req)) |
| 2686 | return; |
| 2687 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2688 | img_req = obj_req->img_request; |
| 2689 | spin_lock(&img_req->completion_lock); |
| 2690 | rbd_obj_end_request(obj_req); |
| 2691 | rbd_assert(img_req->pending_count); |
| 2692 | if (--img_req->pending_count) { |
| 2693 | spin_unlock(&img_req->completion_lock); |
| 2694 | return; |
| 2695 | } |
| 2696 | |
| 2697 | spin_unlock(&img_req->completion_lock); |
| 2698 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2699 | obj_req = img_req->obj_request; |
| 2700 | rbd_img_end_child_request(img_req); |
| 2701 | goto again; |
| 2702 | } |
| 2703 | rbd_img_end_request(img_req); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2704 | } |
| 2705 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2706 | static const struct rbd_client_id rbd_empty_cid; |
| 2707 | |
| 2708 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2709 | const struct rbd_client_id *rhs) |
| 2710 | { |
| 2711 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2712 | } |
| 2713 | |
| 2714 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2715 | { |
| 2716 | struct rbd_client_id cid; |
| 2717 | |
| 2718 | mutex_lock(&rbd_dev->watch_mutex); |
| 2719 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2720 | cid.handle = rbd_dev->watch_cookie; |
| 2721 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2722 | return cid; |
| 2723 | } |
| 2724 | |
| 2725 | /* |
| 2726 | * lock_rwsem must be held for write |
| 2727 | */ |
| 2728 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2729 | const struct rbd_client_id *cid) |
| 2730 | { |
| 2731 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2732 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2733 | cid->gid, cid->handle); |
| 2734 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2735 | } |
| 2736 | |
| 2737 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2738 | { |
| 2739 | mutex_lock(&rbd_dev->watch_mutex); |
| 2740 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2741 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2742 | } |
| 2743 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2744 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2745 | { |
| 2746 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2747 | |
| 2748 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2749 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2750 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2751 | } |
| 2752 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2753 | /* |
| 2754 | * lock_rwsem must be held for write |
| 2755 | */ |
| 2756 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2757 | { |
| 2758 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2759 | char cookie[32]; |
| 2760 | int ret; |
| 2761 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2762 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2763 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2764 | |
| 2765 | format_lock_cookie(rbd_dev, cookie); |
| 2766 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2767 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2768 | RBD_LOCK_TAG, "", 0); |
| 2769 | if (ret) |
| 2770 | return ret; |
| 2771 | |
| 2772 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2773 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2774 | return 0; |
| 2775 | } |
| 2776 | |
| 2777 | /* |
| 2778 | * lock_rwsem must be held for write |
| 2779 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2780 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2781 | { |
| 2782 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2783 | int ret; |
| 2784 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2785 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2786 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2787 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2788 | 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] | 2789 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2790 | if (ret && ret != -ENOENT) |
| 2791 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2792 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2793 | /* treat errors as the image is unlocked */ |
| 2794 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2795 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2796 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2797 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2801 | enum rbd_notify_op notify_op, |
| 2802 | struct page ***preply_pages, |
| 2803 | size_t *preply_len) |
| 2804 | { |
| 2805 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2806 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 2807 | char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN]; |
| 2808 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2809 | void *p = buf; |
| 2810 | |
| 2811 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2812 | |
| 2813 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2814 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2815 | ceph_encode_32(&p, notify_op); |
| 2816 | ceph_encode_64(&p, cid.gid); |
| 2817 | ceph_encode_64(&p, cid.handle); |
| 2818 | |
| 2819 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2820 | &rbd_dev->header_oloc, buf, buf_size, |
| 2821 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2822 | } |
| 2823 | |
| 2824 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2825 | enum rbd_notify_op notify_op) |
| 2826 | { |
| 2827 | struct page **reply_pages; |
| 2828 | size_t reply_len; |
| 2829 | |
| 2830 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2831 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2832 | } |
| 2833 | |
| 2834 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2835 | { |
| 2836 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2837 | acquired_lock_work); |
| 2838 | |
| 2839 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2840 | } |
| 2841 | |
| 2842 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2843 | { |
| 2844 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2845 | released_lock_work); |
| 2846 | |
| 2847 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2848 | } |
| 2849 | |
| 2850 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2851 | { |
| 2852 | struct page **reply_pages; |
| 2853 | size_t reply_len; |
| 2854 | bool lock_owner_responded = false; |
| 2855 | int ret; |
| 2856 | |
| 2857 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2858 | |
| 2859 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2860 | &reply_pages, &reply_len); |
| 2861 | if (ret && ret != -ETIMEDOUT) { |
| 2862 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2863 | goto out; |
| 2864 | } |
| 2865 | |
| 2866 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2867 | void *p = page_address(reply_pages[0]); |
| 2868 | void *const end = p + reply_len; |
| 2869 | u32 n; |
| 2870 | |
| 2871 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2872 | while (n--) { |
| 2873 | u8 struct_v; |
| 2874 | u32 len; |
| 2875 | |
| 2876 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2877 | p += 8 + 8; /* skip gid and cookie */ |
| 2878 | |
| 2879 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2880 | if (!len) |
| 2881 | continue; |
| 2882 | |
| 2883 | if (lock_owner_responded) { |
| 2884 | rbd_warn(rbd_dev, |
| 2885 | "duplicate lock owners detected"); |
| 2886 | ret = -EIO; |
| 2887 | goto out; |
| 2888 | } |
| 2889 | |
| 2890 | lock_owner_responded = true; |
| 2891 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2892 | &struct_v, &len); |
| 2893 | if (ret) { |
| 2894 | rbd_warn(rbd_dev, |
| 2895 | "failed to decode ResponseMessage: %d", |
| 2896 | ret); |
| 2897 | goto e_inval; |
| 2898 | } |
| 2899 | |
| 2900 | ret = ceph_decode_32(&p); |
| 2901 | } |
| 2902 | } |
| 2903 | |
| 2904 | if (!lock_owner_responded) { |
| 2905 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 2906 | ret = -ETIMEDOUT; |
| 2907 | } |
| 2908 | |
| 2909 | out: |
| 2910 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2911 | return ret; |
| 2912 | |
| 2913 | e_inval: |
| 2914 | ret = -EINVAL; |
| 2915 | goto out; |
| 2916 | } |
| 2917 | |
| 2918 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 2919 | { |
| 2920 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 2921 | |
| 2922 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 2923 | if (wake_all) |
| 2924 | wake_up_all(&rbd_dev->lock_waitq); |
| 2925 | else |
| 2926 | wake_up(&rbd_dev->lock_waitq); |
| 2927 | } |
| 2928 | |
| 2929 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 2930 | struct ceph_locker **lockers, u32 *num_lockers) |
| 2931 | { |
| 2932 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2933 | u8 lock_type; |
| 2934 | char *lock_tag; |
| 2935 | int ret; |
| 2936 | |
| 2937 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2938 | |
| 2939 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 2940 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 2941 | &lock_type, &lock_tag, lockers, num_lockers); |
| 2942 | if (ret) |
| 2943 | return ret; |
| 2944 | |
| 2945 | if (*num_lockers == 0) { |
| 2946 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 2947 | goto out; |
| 2948 | } |
| 2949 | |
| 2950 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 2951 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 2952 | lock_tag); |
| 2953 | ret = -EBUSY; |
| 2954 | goto out; |
| 2955 | } |
| 2956 | |
| 2957 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 2958 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 2959 | ret = -EBUSY; |
| 2960 | goto out; |
| 2961 | } |
| 2962 | |
| 2963 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 2964 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 2965 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 2966 | (*lockers)[0].id.cookie); |
| 2967 | ret = -EBUSY; |
| 2968 | goto out; |
| 2969 | } |
| 2970 | |
| 2971 | out: |
| 2972 | kfree(lock_tag); |
| 2973 | return ret; |
| 2974 | } |
| 2975 | |
| 2976 | static int find_watcher(struct rbd_device *rbd_dev, |
| 2977 | const struct ceph_locker *locker) |
| 2978 | { |
| 2979 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2980 | struct ceph_watch_item *watchers; |
| 2981 | u32 num_watchers; |
| 2982 | u64 cookie; |
| 2983 | int i; |
| 2984 | int ret; |
| 2985 | |
| 2986 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 2987 | &rbd_dev->header_oloc, &watchers, |
| 2988 | &num_watchers); |
| 2989 | if (ret) |
| 2990 | return ret; |
| 2991 | |
| 2992 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 2993 | for (i = 0; i < num_watchers; i++) { |
| 2994 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 2995 | sizeof(locker->info.addr)) && |
| 2996 | watchers[i].cookie == cookie) { |
| 2997 | struct rbd_client_id cid = { |
| 2998 | .gid = le64_to_cpu(watchers[i].name.num), |
| 2999 | .handle = cookie, |
| 3000 | }; |
| 3001 | |
| 3002 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3003 | rbd_dev, cid.gid, cid.handle); |
| 3004 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3005 | ret = 1; |
| 3006 | goto out; |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3011 | ret = 0; |
| 3012 | out: |
| 3013 | kfree(watchers); |
| 3014 | return ret; |
| 3015 | } |
| 3016 | |
| 3017 | /* |
| 3018 | * lock_rwsem must be held for write |
| 3019 | */ |
| 3020 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3021 | { |
| 3022 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3023 | struct ceph_locker *lockers; |
| 3024 | u32 num_lockers; |
| 3025 | int ret; |
| 3026 | |
| 3027 | for (;;) { |
| 3028 | ret = rbd_lock(rbd_dev); |
| 3029 | if (ret != -EBUSY) |
| 3030 | return ret; |
| 3031 | |
| 3032 | /* determine if the current lock holder is still alive */ |
| 3033 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3034 | if (ret) |
| 3035 | return ret; |
| 3036 | |
| 3037 | if (num_lockers == 0) |
| 3038 | goto again; |
| 3039 | |
| 3040 | ret = find_watcher(rbd_dev, lockers); |
| 3041 | if (ret) { |
| 3042 | if (ret > 0) |
| 3043 | ret = 0; /* have to request lock */ |
| 3044 | goto out; |
| 3045 | } |
| 3046 | |
| 3047 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3048 | ENTITY_NAME(lockers[0].id.name)); |
| 3049 | |
| 3050 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3051 | &lockers[0].info.addr); |
| 3052 | if (ret) { |
| 3053 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3054 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3055 | goto out; |
| 3056 | } |
| 3057 | |
| 3058 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3059 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3060 | lockers[0].id.cookie, |
| 3061 | &lockers[0].id.name); |
| 3062 | if (ret && ret != -ENOENT) |
| 3063 | goto out; |
| 3064 | |
| 3065 | again: |
| 3066 | ceph_free_lockers(lockers, num_lockers); |
| 3067 | } |
| 3068 | |
| 3069 | out: |
| 3070 | ceph_free_lockers(lockers, num_lockers); |
| 3071 | return ret; |
| 3072 | } |
| 3073 | |
| 3074 | /* |
| 3075 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3076 | */ |
| 3077 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3078 | int *pret) |
| 3079 | { |
| 3080 | enum rbd_lock_state lock_state; |
| 3081 | |
| 3082 | down_read(&rbd_dev->lock_rwsem); |
| 3083 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3084 | rbd_dev->lock_state); |
| 3085 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3086 | lock_state = rbd_dev->lock_state; |
| 3087 | up_read(&rbd_dev->lock_rwsem); |
| 3088 | return lock_state; |
| 3089 | } |
| 3090 | |
| 3091 | up_read(&rbd_dev->lock_rwsem); |
| 3092 | down_write(&rbd_dev->lock_rwsem); |
| 3093 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3094 | rbd_dev->lock_state); |
| 3095 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3096 | *pret = rbd_try_lock(rbd_dev); |
| 3097 | if (*pret) |
| 3098 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3099 | } |
| 3100 | |
| 3101 | lock_state = rbd_dev->lock_state; |
| 3102 | up_write(&rbd_dev->lock_rwsem); |
| 3103 | return lock_state; |
| 3104 | } |
| 3105 | |
| 3106 | static void rbd_acquire_lock(struct work_struct *work) |
| 3107 | { |
| 3108 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3109 | struct rbd_device, lock_dwork); |
| 3110 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3111 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3112 | |
| 3113 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3114 | again: |
| 3115 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3116 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3117 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3118 | wake_requests(rbd_dev, true); |
| 3119 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3120 | rbd_dev, lock_state, ret); |
| 3121 | return; |
| 3122 | } |
| 3123 | |
| 3124 | ret = rbd_request_lock(rbd_dev); |
| 3125 | if (ret == -ETIMEDOUT) { |
| 3126 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3127 | } else if (ret == -EROFS) { |
| 3128 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3129 | /* |
| 3130 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3131 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3132 | * want to block. |
| 3133 | */ |
| 3134 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3135 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3136 | /* wake "rbd map --exclusive" process */ |
| 3137 | wake_requests(rbd_dev, false); |
| 3138 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3139 | } else if (ret < 0) { |
| 3140 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3141 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3142 | RBD_RETRY_DELAY); |
| 3143 | } else { |
| 3144 | /* |
| 3145 | * lock owner acked, but resend if we don't see them |
| 3146 | * release the lock |
| 3147 | */ |
| 3148 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3149 | rbd_dev); |
| 3150 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3151 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3152 | } |
| 3153 | } |
| 3154 | |
| 3155 | /* |
| 3156 | * lock_rwsem must be held for write |
| 3157 | */ |
| 3158 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3159 | { |
| 3160 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3161 | rbd_dev->lock_state); |
| 3162 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3163 | return false; |
| 3164 | |
| 3165 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3166 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3167 | /* |
| 3168 | * Ensure that all in-flight IO is flushed. |
| 3169 | * |
| 3170 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3171 | * may be shared with other devices. |
| 3172 | */ |
| 3173 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3174 | up_read(&rbd_dev->lock_rwsem); |
| 3175 | |
| 3176 | down_write(&rbd_dev->lock_rwsem); |
| 3177 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3178 | rbd_dev->lock_state); |
| 3179 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3180 | return false; |
| 3181 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3182 | rbd_unlock(rbd_dev); |
| 3183 | /* |
| 3184 | * Give others a chance to grab the lock - we would re-acquire |
| 3185 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3186 | * otherwise. We need to ack our own notifications, so this |
| 3187 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3188 | * after wake_requests() in rbd_handle_released_lock(). |
| 3189 | */ |
| 3190 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3191 | return true; |
| 3192 | } |
| 3193 | |
| 3194 | static void rbd_release_lock_work(struct work_struct *work) |
| 3195 | { |
| 3196 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3197 | unlock_work); |
| 3198 | |
| 3199 | down_write(&rbd_dev->lock_rwsem); |
| 3200 | rbd_release_lock(rbd_dev); |
| 3201 | up_write(&rbd_dev->lock_rwsem); |
| 3202 | } |
| 3203 | |
| 3204 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3205 | void **p) |
| 3206 | { |
| 3207 | struct rbd_client_id cid = { 0 }; |
| 3208 | |
| 3209 | if (struct_v >= 2) { |
| 3210 | cid.gid = ceph_decode_64(p); |
| 3211 | cid.handle = ceph_decode_64(p); |
| 3212 | } |
| 3213 | |
| 3214 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3215 | cid.handle); |
| 3216 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3217 | down_write(&rbd_dev->lock_rwsem); |
| 3218 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3219 | /* |
| 3220 | * we already know that the remote client is |
| 3221 | * the owner |
| 3222 | */ |
| 3223 | up_write(&rbd_dev->lock_rwsem); |
| 3224 | return; |
| 3225 | } |
| 3226 | |
| 3227 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3228 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3229 | } else { |
| 3230 | down_read(&rbd_dev->lock_rwsem); |
| 3231 | } |
| 3232 | |
| 3233 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3234 | wake_requests(rbd_dev, false); |
| 3235 | up_read(&rbd_dev->lock_rwsem); |
| 3236 | } |
| 3237 | |
| 3238 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3239 | void **p) |
| 3240 | { |
| 3241 | struct rbd_client_id cid = { 0 }; |
| 3242 | |
| 3243 | if (struct_v >= 2) { |
| 3244 | cid.gid = ceph_decode_64(p); |
| 3245 | cid.handle = ceph_decode_64(p); |
| 3246 | } |
| 3247 | |
| 3248 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3249 | cid.handle); |
| 3250 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3251 | down_write(&rbd_dev->lock_rwsem); |
| 3252 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3253 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3254 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3255 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3256 | up_write(&rbd_dev->lock_rwsem); |
| 3257 | return; |
| 3258 | } |
| 3259 | |
| 3260 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3261 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3262 | } else { |
| 3263 | down_read(&rbd_dev->lock_rwsem); |
| 3264 | } |
| 3265 | |
| 3266 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3267 | wake_requests(rbd_dev, false); |
| 3268 | up_read(&rbd_dev->lock_rwsem); |
| 3269 | } |
| 3270 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3271 | /* |
| 3272 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3273 | * ResponseMessage is needed. |
| 3274 | */ |
| 3275 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3276 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3277 | { |
| 3278 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3279 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3280 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3281 | |
| 3282 | if (struct_v >= 2) { |
| 3283 | cid.gid = ceph_decode_64(p); |
| 3284 | cid.handle = ceph_decode_64(p); |
| 3285 | } |
| 3286 | |
| 3287 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3288 | cid.handle); |
| 3289 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3290 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3291 | |
| 3292 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3293 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3294 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3295 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3296 | goto out_unlock; |
| 3297 | |
| 3298 | /* |
| 3299 | * encode ResponseMessage(0) so the peer can detect |
| 3300 | * a missing owner |
| 3301 | */ |
| 3302 | result = 0; |
| 3303 | |
| 3304 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3305 | if (!rbd_dev->opts->exclusive) { |
| 3306 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3307 | __func__, rbd_dev); |
| 3308 | queue_work(rbd_dev->task_wq, |
| 3309 | &rbd_dev->unlock_work); |
| 3310 | } else { |
| 3311 | /* refuse to release the lock */ |
| 3312 | result = -EROFS; |
| 3313 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3314 | } |
| 3315 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3316 | |
| 3317 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3318 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3319 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3323 | u64 notify_id, u64 cookie, s32 *result) |
| 3324 | { |
| 3325 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 3326 | char buf[4 + CEPH_ENCODING_START_BLK_LEN]; |
| 3327 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3328 | int ret; |
| 3329 | |
| 3330 | if (result) { |
| 3331 | void *p = buf; |
| 3332 | |
| 3333 | /* encode ResponseMessage */ |
| 3334 | ceph_start_encoding(&p, 1, 1, |
| 3335 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3336 | ceph_encode_32(&p, *result); |
| 3337 | } else { |
| 3338 | buf_size = 0; |
| 3339 | } |
| 3340 | |
| 3341 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3342 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3343 | buf, buf_size); |
| 3344 | if (ret) |
| 3345 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3346 | } |
| 3347 | |
| 3348 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3349 | u64 cookie) |
| 3350 | { |
| 3351 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3352 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3353 | } |
| 3354 | |
| 3355 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3356 | u64 notify_id, u64 cookie, s32 result) |
| 3357 | { |
| 3358 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3359 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3360 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3361 | |
| 3362 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3363 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3364 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3365 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3366 | void *p = data; |
| 3367 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3368 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3369 | u32 len; |
| 3370 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3371 | int ret; |
| 3372 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3373 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3374 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3375 | if (data_len) { |
| 3376 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3377 | &struct_v, &len); |
| 3378 | if (ret) { |
| 3379 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3380 | ret); |
| 3381 | return; |
| 3382 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3383 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3384 | notify_op = ceph_decode_32(&p); |
| 3385 | } else { |
| 3386 | /* legacy notification for header updates */ |
| 3387 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3388 | len = 0; |
| 3389 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3390 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3391 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3392 | switch (notify_op) { |
| 3393 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3394 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3395 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3396 | break; |
| 3397 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3398 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3399 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3400 | break; |
| 3401 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3402 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3403 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3404 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3405 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3406 | else |
| 3407 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3408 | break; |
| 3409 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3410 | ret = rbd_dev_refresh(rbd_dev); |
| 3411 | if (ret) |
| 3412 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3413 | |
| 3414 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3415 | break; |
| 3416 | default: |
| 3417 | if (rbd_is_lock_owner(rbd_dev)) |
| 3418 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3419 | cookie, -EOPNOTSUPP); |
| 3420 | else |
| 3421 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3422 | break; |
| 3423 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3424 | } |
| 3425 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3426 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3427 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3428 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3429 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3430 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3431 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3432 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3433 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3434 | down_write(&rbd_dev->lock_rwsem); |
| 3435 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3436 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3437 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3438 | mutex_lock(&rbd_dev->watch_mutex); |
| 3439 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3440 | __rbd_unregister_watch(rbd_dev); |
| 3441 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3442 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3443 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3444 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3445 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3449 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3450 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3451 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3452 | { |
| 3453 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3454 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3455 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3456 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3457 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3458 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3459 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3460 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3461 | rbd_watch_errcb, rbd_dev); |
| 3462 | if (IS_ERR(handle)) |
| 3463 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3464 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3465 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3466 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3467 | } |
| 3468 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3469 | /* |
| 3470 | * watch_mutex must be locked |
| 3471 | */ |
| 3472 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3473 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3474 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3475 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3476 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3477 | rbd_assert(rbd_dev->watch_handle); |
| 3478 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3479 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3480 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3481 | if (ret) |
| 3482 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3483 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3484 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3485 | } |
| 3486 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3487 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3488 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3489 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3490 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3491 | mutex_lock(&rbd_dev->watch_mutex); |
| 3492 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3493 | ret = __rbd_register_watch(rbd_dev); |
| 3494 | if (ret) |
| 3495 | goto out; |
| 3496 | |
| 3497 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3498 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3499 | |
| 3500 | out: |
| 3501 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3502 | return ret; |
| 3503 | } |
| 3504 | |
| 3505 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3506 | { |
| 3507 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3508 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3509 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3510 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3511 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3512 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3516 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3517 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3518 | cancel_tasks_sync(rbd_dev); |
| 3519 | |
| 3520 | mutex_lock(&rbd_dev->watch_mutex); |
| 3521 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3522 | __rbd_unregister_watch(rbd_dev); |
| 3523 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3524 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3525 | |
Dongsheng Yang | 23edca8 | 2018-06-04 06:24:37 -0400 | [diff] [blame] | 3526 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3527 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3530 | /* |
| 3531 | * lock_rwsem must be held for write |
| 3532 | */ |
| 3533 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3534 | { |
| 3535 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3536 | char cookie[32]; |
| 3537 | int ret; |
| 3538 | |
| 3539 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3540 | |
| 3541 | format_lock_cookie(rbd_dev, cookie); |
| 3542 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3543 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3544 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3545 | RBD_LOCK_TAG, cookie); |
| 3546 | if (ret) { |
| 3547 | if (ret != -EOPNOTSUPP) |
| 3548 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3549 | ret); |
| 3550 | |
| 3551 | /* |
| 3552 | * Lock cookie cannot be updated on older OSDs, so do |
| 3553 | * a manual release and queue an acquire. |
| 3554 | */ |
| 3555 | if (rbd_release_lock(rbd_dev)) |
| 3556 | queue_delayed_work(rbd_dev->task_wq, |
| 3557 | &rbd_dev->lock_dwork, 0); |
| 3558 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3559 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3560 | } |
| 3561 | } |
| 3562 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3563 | static void rbd_reregister_watch(struct work_struct *work) |
| 3564 | { |
| 3565 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3566 | struct rbd_device, watch_dwork); |
| 3567 | int ret; |
| 3568 | |
| 3569 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3570 | |
| 3571 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3572 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3573 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3574 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3575 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3576 | |
| 3577 | ret = __rbd_register_watch(rbd_dev); |
| 3578 | if (ret) { |
| 3579 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3580 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3581 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3582 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3583 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3584 | queue_delayed_work(rbd_dev->task_wq, |
| 3585 | &rbd_dev->watch_dwork, |
| 3586 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3587 | } |
| 3588 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3589 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3593 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3594 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3595 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3596 | down_write(&rbd_dev->lock_rwsem); |
| 3597 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3598 | rbd_reacquire_lock(rbd_dev); |
| 3599 | up_write(&rbd_dev->lock_rwsem); |
| 3600 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3601 | ret = rbd_dev_refresh(rbd_dev); |
| 3602 | if (ret) |
Colin Ian King | f6870cc | 2018-03-19 13:33:10 +0000 | [diff] [blame] | 3603 | rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3604 | } |
| 3605 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3606 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3607 | * Synchronous osd object method call. Returns the number of bytes |
| 3608 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3609 | */ |
| 3610 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3611 | struct ceph_object_id *oid, |
| 3612 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3613 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3614 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3615 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3616 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3617 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3618 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3619 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3620 | struct page *req_page = NULL; |
| 3621 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3622 | int ret; |
| 3623 | |
| 3624 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3625 | * Method calls are ultimately read operations. The result |
| 3626 | * should placed into the inbound buffer provided. They |
| 3627 | * also supply outbound data--parameters for the object |
| 3628 | * method. Currently if this is present it will be a |
| 3629 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3630 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3631 | if (outbound) { |
| 3632 | if (outbound_size > PAGE_SIZE) |
| 3633 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3634 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3635 | req_page = alloc_page(GFP_KERNEL); |
| 3636 | if (!req_page) |
| 3637 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3638 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3639 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3640 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3641 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3642 | reply_page = alloc_page(GFP_KERNEL); |
| 3643 | if (!reply_page) { |
| 3644 | if (req_page) |
| 3645 | __free_page(req_page); |
| 3646 | return -ENOMEM; |
| 3647 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3648 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3649 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3650 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3651 | reply_page, &inbound_size); |
| 3652 | if (!ret) { |
| 3653 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3654 | ret = inbound_size; |
| 3655 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3656 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3657 | if (req_page) |
| 3658 | __free_page(req_page); |
| 3659 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3660 | return ret; |
| 3661 | } |
| 3662 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3663 | /* |
| 3664 | * lock_rwsem must be held for read |
| 3665 | */ |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3666 | 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] | 3667 | { |
| 3668 | DEFINE_WAIT(wait); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3669 | unsigned long timeout; |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3670 | int ret = 0; |
| 3671 | |
| 3672 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
| 3673 | return -EBLACKLISTED; |
| 3674 | |
| 3675 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3676 | return 0; |
| 3677 | |
| 3678 | if (!may_acquire) { |
| 3679 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3680 | return -EROFS; |
| 3681 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3682 | |
| 3683 | do { |
| 3684 | /* |
| 3685 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3686 | * and cancel_delayed_work() in wake_requests(). |
| 3687 | */ |
| 3688 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3689 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3690 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3691 | TASK_UNINTERRUPTIBLE); |
| 3692 | up_read(&rbd_dev->lock_rwsem); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3693 | timeout = schedule_timeout(ceph_timeout_jiffies( |
| 3694 | rbd_dev->opts->lock_timeout)); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3695 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3696 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3697 | ret = -EBLACKLISTED; |
| 3698 | break; |
| 3699 | } |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3700 | if (!timeout) { |
| 3701 | rbd_warn(rbd_dev, "timed out waiting for lock"); |
| 3702 | ret = -ETIMEDOUT; |
| 3703 | break; |
| 3704 | } |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3705 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3706 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3707 | finish_wait(&rbd_dev->lock_waitq, &wait); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3708 | return ret; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3709 | } |
| 3710 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3711 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3712 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3713 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3714 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3715 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3716 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3717 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3718 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3719 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3720 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3721 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3722 | int result; |
| 3723 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3724 | switch (req_op(rq)) { |
| 3725 | case REQ_OP_DISCARD: |
| 3726 | op_type = OBJ_OP_DISCARD; |
| 3727 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 3728 | case REQ_OP_WRITE_ZEROES: |
| 3729 | op_type = OBJ_OP_ZEROOUT; |
| 3730 | break; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3731 | case REQ_OP_WRITE: |
| 3732 | op_type = OBJ_OP_WRITE; |
| 3733 | break; |
| 3734 | case REQ_OP_READ: |
| 3735 | op_type = OBJ_OP_READ; |
| 3736 | break; |
| 3737 | default: |
| 3738 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3739 | result = -EIO; |
| 3740 | goto err; |
| 3741 | } |
| 3742 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3743 | /* Ignore/skip any zero-length requests */ |
| 3744 | |
| 3745 | if (!length) { |
| 3746 | dout("%s: zero-length request\n", __func__); |
| 3747 | result = 0; |
| 3748 | goto err_rq; |
| 3749 | } |
| 3750 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3751 | rbd_assert(op_type == OBJ_OP_READ || |
| 3752 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3753 | |
| 3754 | /* |
| 3755 | * Quit early if the mapped snapshot no longer exists. It's |
| 3756 | * still possible the snapshot will have disappeared by the |
| 3757 | * time our request arrives at the osd, but there's no sense in |
| 3758 | * sending it if we already know. |
| 3759 | */ |
| 3760 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3761 | dout("request for non-existent snapshot"); |
| 3762 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3763 | result = -ENXIO; |
| 3764 | goto err_rq; |
| 3765 | } |
| 3766 | |
| 3767 | if (offset && length > U64_MAX - offset + 1) { |
| 3768 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3769 | length); |
| 3770 | result = -EINVAL; |
| 3771 | goto err_rq; /* Shouldn't happen */ |
| 3772 | } |
| 3773 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3774 | blk_mq_start_request(rq); |
| 3775 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3776 | down_read(&rbd_dev->header_rwsem); |
| 3777 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3778 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3779 | snapc = rbd_dev->header.snapc; |
| 3780 | ceph_get_snap_context(snapc); |
| 3781 | } |
| 3782 | up_read(&rbd_dev->header_rwsem); |
| 3783 | |
| 3784 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3785 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3786 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3787 | result = -EIO; |
| 3788 | goto err_rq; |
| 3789 | } |
| 3790 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3791 | must_be_locked = |
| 3792 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3793 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3794 | if (must_be_locked) { |
| 3795 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3796 | result = rbd_wait_state_locked(rbd_dev, |
| 3797 | !rbd_dev->opts->exclusive); |
| 3798 | if (result) |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3799 | goto err_unlock; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3800 | } |
| 3801 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 3802 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3803 | if (!img_request) { |
| 3804 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3805 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3806 | } |
| 3807 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3808 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3809 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 3810 | if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3811 | result = rbd_img_fill_nodata(img_request, offset, length); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3812 | else |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3813 | result = rbd_img_fill_from_bio(img_request, offset, length, |
| 3814 | rq->bio); |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 3815 | if (result || !img_request->pending_count) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3816 | goto err_img_request; |
| 3817 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3818 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3819 | if (must_be_locked) |
| 3820 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3821 | return; |
| 3822 | |
| 3823 | err_img_request: |
| 3824 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3825 | err_unlock: |
| 3826 | if (must_be_locked) |
| 3827 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3828 | err_rq: |
| 3829 | if (result) |
| 3830 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3831 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3832 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3833 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3834 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3835 | } |
| 3836 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3837 | 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] | 3838 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3839 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3840 | struct request *rq = bd->rq; |
| 3841 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3842 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3843 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3844 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3845 | } |
| 3846 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3847 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3848 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3849 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3850 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3851 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3852 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3853 | } |
| 3854 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3855 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3856 | struct ceph_object_id *oid, |
| 3857 | struct ceph_object_locator *oloc, |
| 3858 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3859 | |
| 3860 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3861 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3862 | struct ceph_osd_request *req; |
| 3863 | struct page **pages; |
| 3864 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3865 | int ret; |
| 3866 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3867 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3868 | if (!req) |
| 3869 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3870 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3871 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3872 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3873 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3874 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3875 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3876 | if (IS_ERR(pages)) { |
| 3877 | ret = PTR_ERR(pages); |
| 3878 | goto out_req; |
| 3879 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3880 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3881 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3882 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3883 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3884 | |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 3885 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
| 3886 | if (ret) |
| 3887 | goto out_req; |
| 3888 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3889 | ceph_osdc_start_request(osdc, req, false); |
| 3890 | ret = ceph_osdc_wait_request(osdc, req); |
| 3891 | if (ret >= 0) |
| 3892 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3893 | |
| 3894 | out_req: |
| 3895 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3896 | return ret; |
| 3897 | } |
| 3898 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3899 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3900 | * Read the complete header for the given rbd device. On successful |
| 3901 | * return, the rbd_dev->header field will contain up-to-date |
| 3902 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3903 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3904 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3905 | { |
| 3906 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 3907 | u32 snap_count = 0; |
| 3908 | u64 names_size = 0; |
| 3909 | u32 want_count; |
| 3910 | int ret; |
| 3911 | |
| 3912 | /* |
| 3913 | * The complete header will include an array of its 64-bit |
| 3914 | * snapshot ids, followed by the names of those snapshots as |
| 3915 | * a contiguous block of NUL-terminated strings. Note that |
| 3916 | * the number of snapshots could change by the time we read |
| 3917 | * it in, in which case we re-read it. |
| 3918 | */ |
| 3919 | do { |
| 3920 | size_t size; |
| 3921 | |
| 3922 | kfree(ondisk); |
| 3923 | |
| 3924 | size = sizeof (*ondisk); |
| 3925 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 3926 | size += names_size; |
| 3927 | ondisk = kmalloc(size, GFP_KERNEL); |
| 3928 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3929 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3930 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3931 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 3932 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3933 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3934 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 3935 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3936 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3937 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 3938 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3939 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3940 | } |
| 3941 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 3942 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 3943 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3944 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3945 | } |
| 3946 | |
| 3947 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 3948 | want_count = snap_count; |
| 3949 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 3950 | } while (snap_count != want_count); |
| 3951 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3952 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 3953 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3954 | kfree(ondisk); |
| 3955 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 3956 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3957 | } |
| 3958 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 3959 | /* |
| 3960 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 3961 | * has disappeared from the (just updated) snapshot context. |
| 3962 | */ |
| 3963 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 3964 | { |
| 3965 | u64 snap_id; |
| 3966 | |
| 3967 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 3968 | return; |
| 3969 | |
| 3970 | snap_id = rbd_dev->spec->snap_id; |
| 3971 | if (snap_id == CEPH_NOSNAP) |
| 3972 | return; |
| 3973 | |
| 3974 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 3975 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 3976 | } |
| 3977 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3978 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 3979 | { |
| 3980 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3981 | |
| 3982 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3983 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 3984 | * try to update its size. If REMOVING is set, updating size |
| 3985 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3986 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3987 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 3988 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 3989 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 3990 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 3991 | set_capacity(rbd_dev->disk, size); |
| 3992 | revalidate_disk(rbd_dev->disk); |
| 3993 | } |
| 3994 | } |
| 3995 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3996 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3997 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 3998 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 3999 | int ret; |
| 4000 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4001 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4002 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4003 | |
| 4004 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4005 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4006 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4007 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4008 | /* |
| 4009 | * If there is a parent, see if it has disappeared due to the |
| 4010 | * mapped image getting flattened. |
| 4011 | */ |
| 4012 | if (rbd_dev->parent) { |
| 4013 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4014 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4015 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4016 | } |
| 4017 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4018 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4019 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4020 | } else { |
| 4021 | /* validate mapped snapshot's EXISTS flag */ |
| 4022 | rbd_exists_validate(rbd_dev); |
| 4023 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4024 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4025 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4026 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4027 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4028 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4029 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4030 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4031 | } |
| 4032 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 4033 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 4034 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4035 | { |
| 4036 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4037 | |
| 4038 | INIT_WORK(work, rbd_queue_workfn); |
| 4039 | return 0; |
| 4040 | } |
| 4041 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 4042 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4043 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4044 | .init_request = rbd_init_request, |
| 4045 | }; |
| 4046 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4047 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4048 | { |
| 4049 | struct gendisk *disk; |
| 4050 | struct request_queue *q; |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4051 | unsigned int objset_bytes = |
| 4052 | rbd_dev->layout.object_size * rbd_dev->layout.stripe_count; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4053 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4054 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4055 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4056 | disk = alloc_disk(single_major ? |
| 4057 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4058 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4059 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4060 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4061 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4062 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4063 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4064 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4065 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4066 | if (single_major) |
| 4067 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4068 | disk->fops = &rbd_bd_ops; |
| 4069 | disk->private_data = rbd_dev; |
| 4070 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4071 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4072 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4073 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4074 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4075 | 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] | 4076 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4077 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4078 | |
| 4079 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4080 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4081 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4082 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4083 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4084 | if (IS_ERR(q)) { |
| 4085 | err = PTR_ERR(q); |
| 4086 | goto out_tag_set; |
| 4087 | } |
| 4088 | |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 4089 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4090 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4091 | |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4092 | blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4093 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 4094 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 4095 | blk_queue_max_segment_size(q, UINT_MAX); |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4096 | blk_queue_io_min(q, objset_bytes); |
| 4097 | blk_queue_io_opt(q, objset_bytes); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4098 | |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4099 | if (rbd_dev->opts->trim) { |
| 4100 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
| 4101 | q->limits.discard_granularity = objset_bytes; |
| 4102 | blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4103 | blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4104 | } |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4105 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4106 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4107 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4108 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4109 | /* |
| 4110 | * disk_release() expects a queue ref from add_disk() and will |
| 4111 | * put it. Hold an extra ref until add_disk() is called. |
| 4112 | */ |
| 4113 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4114 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4115 | q->queuedata = rbd_dev; |
| 4116 | |
| 4117 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4118 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4119 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4120 | out_tag_set: |
| 4121 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4122 | out_disk: |
| 4123 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4124 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4125 | } |
| 4126 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4127 | /* |
| 4128 | sysfs |
| 4129 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4130 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4131 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4132 | { |
| 4133 | return container_of(dev, struct rbd_device, dev); |
| 4134 | } |
| 4135 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4136 | static ssize_t rbd_size_show(struct device *dev, |
| 4137 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4138 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4139 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4140 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4141 | return sprintf(buf, "%llu\n", |
| 4142 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4143 | } |
| 4144 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4145 | /* |
| 4146 | * Note this shows the features for whatever's mapped, which is not |
| 4147 | * necessarily the base image. |
| 4148 | */ |
| 4149 | static ssize_t rbd_features_show(struct device *dev, |
| 4150 | struct device_attribute *attr, char *buf) |
| 4151 | { |
| 4152 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4153 | |
| 4154 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4155 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4156 | } |
| 4157 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4158 | static ssize_t rbd_major_show(struct device *dev, |
| 4159 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4160 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4161 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4162 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4163 | if (rbd_dev->major) |
| 4164 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4165 | |
| 4166 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4167 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4168 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4169 | static ssize_t rbd_minor_show(struct device *dev, |
| 4170 | struct device_attribute *attr, char *buf) |
| 4171 | { |
| 4172 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4173 | |
| 4174 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4175 | } |
| 4176 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4177 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4178 | struct device_attribute *attr, char *buf) |
| 4179 | { |
| 4180 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4181 | struct ceph_entity_addr *client_addr = |
| 4182 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4183 | |
| 4184 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4185 | le32_to_cpu(client_addr->nonce)); |
| 4186 | } |
| 4187 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4188 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4189 | struct device_attribute *attr, char *buf) |
| 4190 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4191 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4192 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4193 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4194 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4195 | } |
| 4196 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4197 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4198 | struct device_attribute *attr, char *buf) |
| 4199 | { |
| 4200 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4201 | |
| 4202 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4203 | } |
| 4204 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4205 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4206 | struct device_attribute *attr, char *buf) |
| 4207 | { |
| 4208 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4209 | |
| 4210 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | static ssize_t rbd_pool_show(struct device *dev, |
| 4214 | struct device_attribute *attr, char *buf) |
| 4215 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4216 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4217 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4218 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4219 | } |
| 4220 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4221 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4222 | struct device_attribute *attr, char *buf) |
| 4223 | { |
| 4224 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4225 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4226 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4227 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4228 | } |
| 4229 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4230 | static ssize_t rbd_pool_ns_show(struct device *dev, |
| 4231 | struct device_attribute *attr, char *buf) |
| 4232 | { |
| 4233 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4234 | |
| 4235 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: ""); |
| 4236 | } |
| 4237 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4238 | static ssize_t rbd_name_show(struct device *dev, |
| 4239 | struct device_attribute *attr, char *buf) |
| 4240 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4241 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4242 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4243 | if (rbd_dev->spec->image_name) |
| 4244 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4245 | |
| 4246 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4247 | } |
| 4248 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4249 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4250 | struct device_attribute *attr, char *buf) |
| 4251 | { |
| 4252 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4253 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4254 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4255 | } |
| 4256 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4257 | /* |
| 4258 | * Shows the name of the currently-mapped snapshot (or |
| 4259 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4260 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4261 | static ssize_t rbd_snap_show(struct device *dev, |
| 4262 | struct device_attribute *attr, |
| 4263 | char *buf) |
| 4264 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4265 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4266 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4267 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4268 | } |
| 4269 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4270 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4271 | struct device_attribute *attr, char *buf) |
| 4272 | { |
| 4273 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4274 | |
| 4275 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4276 | } |
| 4277 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4278 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4279 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4280 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4281 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4282 | */ |
| 4283 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4284 | struct device_attribute *attr, |
| 4285 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4286 | { |
| 4287 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4288 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4289 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4290 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4291 | return sprintf(buf, "(no parent image)\n"); |
| 4292 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4293 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4294 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4295 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4296 | count += sprintf(&buf[count], "%s" |
| 4297 | "pool_id %llu\npool_name %s\n" |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4298 | "pool_ns %s\n" |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4299 | "image_id %s\nimage_name %s\n" |
| 4300 | "snap_id %llu\nsnap_name %s\n" |
| 4301 | "overlap %llu\n", |
| 4302 | !count ? "" : "\n", /* first? */ |
| 4303 | spec->pool_id, spec->pool_name, |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4304 | spec->pool_ns ?: "", |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4305 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4306 | spec->snap_id, spec->snap_name, |
| 4307 | rbd_dev->parent_overlap); |
| 4308 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4309 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4310 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4311 | } |
| 4312 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4313 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4314 | struct device_attribute *attr, |
| 4315 | const char *buf, |
| 4316 | size_t size) |
| 4317 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4318 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4319 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4320 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4321 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4322 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4323 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4324 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4325 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4326 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4327 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4328 | static DEVICE_ATTR(size, 0444, rbd_size_show, NULL); |
| 4329 | static DEVICE_ATTR(features, 0444, rbd_features_show, NULL); |
| 4330 | static DEVICE_ATTR(major, 0444, rbd_major_show, NULL); |
| 4331 | static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL); |
| 4332 | static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL); |
| 4333 | static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL); |
| 4334 | static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL); |
| 4335 | static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL); |
| 4336 | static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL); |
| 4337 | static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4338 | static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL); |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4339 | static DEVICE_ATTR(name, 0444, rbd_name_show, NULL); |
| 4340 | static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL); |
| 4341 | static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh); |
| 4342 | static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL); |
| 4343 | static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL); |
| 4344 | static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4345 | |
| 4346 | static struct attribute *rbd_attrs[] = { |
| 4347 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4348 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4349 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4350 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4351 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4352 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4353 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4354 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4355 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4356 | &dev_attr_pool_id.attr, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4357 | &dev_attr_pool_ns.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4358 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4359 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4360 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4361 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4362 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4363 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4364 | NULL |
| 4365 | }; |
| 4366 | |
| 4367 | static struct attribute_group rbd_attr_group = { |
| 4368 | .attrs = rbd_attrs, |
| 4369 | }; |
| 4370 | |
| 4371 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4372 | &rbd_attr_group, |
| 4373 | NULL |
| 4374 | }; |
| 4375 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4376 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4377 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4378 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4379 | .name = "rbd", |
| 4380 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4381 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4382 | }; |
| 4383 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4384 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4385 | { |
| 4386 | kref_get(&spec->kref); |
| 4387 | |
| 4388 | return spec; |
| 4389 | } |
| 4390 | |
| 4391 | static void rbd_spec_free(struct kref *kref); |
| 4392 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4393 | { |
| 4394 | if (spec) |
| 4395 | kref_put(&spec->kref, rbd_spec_free); |
| 4396 | } |
| 4397 | |
| 4398 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4399 | { |
| 4400 | struct rbd_spec *spec; |
| 4401 | |
| 4402 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4403 | if (!spec) |
| 4404 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4405 | |
| 4406 | spec->pool_id = CEPH_NOPOOL; |
| 4407 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4408 | kref_init(&spec->kref); |
| 4409 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4410 | return spec; |
| 4411 | } |
| 4412 | |
| 4413 | static void rbd_spec_free(struct kref *kref) |
| 4414 | { |
| 4415 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4416 | |
| 4417 | kfree(spec->pool_name); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4418 | kfree(spec->pool_ns); |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4419 | kfree(spec->image_id); |
| 4420 | kfree(spec->image_name); |
| 4421 | kfree(spec->snap_name); |
| 4422 | kfree(spec); |
| 4423 | } |
| 4424 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4425 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4426 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4427 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4428 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4429 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4430 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4431 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4432 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4433 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4434 | rbd_put_client(rbd_dev->rbd_client); |
| 4435 | rbd_spec_put(rbd_dev->spec); |
| 4436 | kfree(rbd_dev->opts); |
| 4437 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | static void rbd_dev_release(struct device *dev) |
| 4441 | { |
| 4442 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4443 | bool need_put = !!rbd_dev->opts; |
| 4444 | |
| 4445 | if (need_put) { |
| 4446 | destroy_workqueue(rbd_dev->task_wq); |
| 4447 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4448 | } |
| 4449 | |
| 4450 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4451 | |
| 4452 | /* |
| 4453 | * This is racy, but way better than putting module outside of |
| 4454 | * the release callback. The race window is pretty small, so |
| 4455 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4456 | */ |
| 4457 | if (need_put) |
| 4458 | module_put(THIS_MODULE); |
| 4459 | } |
| 4460 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4461 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4462 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4463 | { |
| 4464 | struct rbd_device *rbd_dev; |
| 4465 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4466 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4467 | if (!rbd_dev) |
| 4468 | return NULL; |
| 4469 | |
| 4470 | spin_lock_init(&rbd_dev->lock); |
| 4471 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4472 | init_rwsem(&rbd_dev->header_rwsem); |
| 4473 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4474 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4475 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4476 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4477 | if (spec->pool_ns) { |
| 4478 | WARN_ON(!*spec->pool_ns); |
| 4479 | rbd_dev->header_oloc.pool_ns = |
| 4480 | ceph_find_or_create_string(spec->pool_ns, |
| 4481 | strlen(spec->pool_ns)); |
| 4482 | } |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4483 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4484 | mutex_init(&rbd_dev->watch_mutex); |
| 4485 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4486 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4487 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4488 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4489 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4490 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4491 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4492 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4493 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4494 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4495 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4496 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4497 | rbd_dev->dev.type = &rbd_device_type; |
| 4498 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4499 | device_initialize(&rbd_dev->dev); |
| 4500 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4501 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4502 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4503 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4504 | return rbd_dev; |
| 4505 | } |
| 4506 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4507 | /* |
| 4508 | * Create a mapping rbd_dev. |
| 4509 | */ |
| 4510 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4511 | struct rbd_spec *spec, |
| 4512 | struct rbd_options *opts) |
| 4513 | { |
| 4514 | struct rbd_device *rbd_dev; |
| 4515 | |
| 4516 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4517 | if (!rbd_dev) |
| 4518 | return NULL; |
| 4519 | |
| 4520 | rbd_dev->opts = opts; |
| 4521 | |
| 4522 | /* get an id and fill in device name */ |
| 4523 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4524 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4525 | GFP_KERNEL); |
| 4526 | if (rbd_dev->dev_id < 0) |
| 4527 | goto fail_rbd_dev; |
| 4528 | |
| 4529 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4530 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4531 | rbd_dev->name); |
| 4532 | if (!rbd_dev->task_wq) |
| 4533 | goto fail_dev_id; |
| 4534 | |
| 4535 | /* we have a ref from do_rbd_add() */ |
| 4536 | __module_get(THIS_MODULE); |
| 4537 | |
| 4538 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4539 | return rbd_dev; |
| 4540 | |
| 4541 | fail_dev_id: |
| 4542 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4543 | fail_rbd_dev: |
| 4544 | rbd_dev_free(rbd_dev); |
| 4545 | return NULL; |
| 4546 | } |
| 4547 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4548 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4549 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4550 | if (rbd_dev) |
| 4551 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4552 | } |
| 4553 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4554 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4555 | * Get the size and object order for an image snapshot, or if |
| 4556 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4557 | * image. |
| 4558 | */ |
| 4559 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4560 | u8 *order, u64 *snap_size) |
| 4561 | { |
| 4562 | __le64 snapid = cpu_to_le64(snap_id); |
| 4563 | int ret; |
| 4564 | struct { |
| 4565 | u8 order; |
| 4566 | __le64 size; |
| 4567 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4568 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4569 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4570 | &rbd_dev->header_oloc, "get_size", |
| 4571 | &snapid, sizeof(snapid), |
| 4572 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4573 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4574 | if (ret < 0) |
| 4575 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4576 | if (ret < sizeof (size_buf)) |
| 4577 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4578 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4579 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4580 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4581 | dout(" order %u", (unsigned int)*order); |
| 4582 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4583 | *snap_size = le64_to_cpu(size_buf.size); |
| 4584 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4585 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4586 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4587 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4588 | |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
| 4592 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4593 | { |
| 4594 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4595 | &rbd_dev->header.obj_order, |
| 4596 | &rbd_dev->header.image_size); |
| 4597 | } |
| 4598 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4599 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4600 | { |
| 4601 | void *reply_buf; |
| 4602 | int ret; |
| 4603 | void *p; |
| 4604 | |
| 4605 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4606 | if (!reply_buf) |
| 4607 | return -ENOMEM; |
| 4608 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4609 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4610 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4611 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4612 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4613 | if (ret < 0) |
| 4614 | goto out; |
| 4615 | |
| 4616 | p = reply_buf; |
| 4617 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4618 | p + ret, NULL, GFP_NOIO); |
| 4619 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4620 | |
| 4621 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4622 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4623 | rbd_dev->header.object_prefix = NULL; |
| 4624 | } else { |
| 4625 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4626 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4627 | out: |
| 4628 | kfree(reply_buf); |
| 4629 | |
| 4630 | return ret; |
| 4631 | } |
| 4632 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4633 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4634 | u64 *snap_features) |
| 4635 | { |
| 4636 | __le64 snapid = cpu_to_le64(snap_id); |
| 4637 | struct { |
| 4638 | __le64 features; |
| 4639 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4640 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4641 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4642 | int ret; |
| 4643 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4644 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4645 | &rbd_dev->header_oloc, "get_features", |
| 4646 | &snapid, sizeof(snapid), |
| 4647 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4648 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4649 | if (ret < 0) |
| 4650 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4651 | if (ret < sizeof (features_buf)) |
| 4652 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4653 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4654 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4655 | if (unsup) { |
| 4656 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4657 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4658 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4659 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4660 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4661 | *snap_features = le64_to_cpu(features_buf.features); |
| 4662 | |
| 4663 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4664 | (unsigned long long)snap_id, |
| 4665 | (unsigned long long)*snap_features, |
| 4666 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
| 4671 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4672 | { |
| 4673 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4674 | &rbd_dev->header.features); |
| 4675 | } |
| 4676 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4677 | struct parent_image_info { |
| 4678 | u64 pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4679 | const char *pool_ns; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4680 | const char *image_id; |
| 4681 | u64 snap_id; |
| 4682 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4683 | bool has_overlap; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4684 | u64 overlap; |
| 4685 | }; |
| 4686 | |
| 4687 | /* |
| 4688 | * The caller is responsible for @pii. |
| 4689 | */ |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4690 | static int decode_parent_image_spec(void **p, void *end, |
| 4691 | struct parent_image_info *pii) |
| 4692 | { |
| 4693 | u8 struct_v; |
| 4694 | u32 struct_len; |
| 4695 | int ret; |
| 4696 | |
| 4697 | ret = ceph_start_decoding(p, end, 1, "ParentImageSpec", |
| 4698 | &struct_v, &struct_len); |
| 4699 | if (ret) |
| 4700 | return ret; |
| 4701 | |
| 4702 | ceph_decode_64_safe(p, end, pii->pool_id, e_inval); |
| 4703 | pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 4704 | if (IS_ERR(pii->pool_ns)) { |
| 4705 | ret = PTR_ERR(pii->pool_ns); |
| 4706 | pii->pool_ns = NULL; |
| 4707 | return ret; |
| 4708 | } |
| 4709 | pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 4710 | if (IS_ERR(pii->image_id)) { |
| 4711 | ret = PTR_ERR(pii->image_id); |
| 4712 | pii->image_id = NULL; |
| 4713 | return ret; |
| 4714 | } |
| 4715 | ceph_decode_64_safe(p, end, pii->snap_id, e_inval); |
| 4716 | return 0; |
| 4717 | |
| 4718 | e_inval: |
| 4719 | return -EINVAL; |
| 4720 | } |
| 4721 | |
| 4722 | static int __get_parent_info(struct rbd_device *rbd_dev, |
| 4723 | struct page *req_page, |
| 4724 | struct page *reply_page, |
| 4725 | struct parent_image_info *pii) |
| 4726 | { |
| 4727 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4728 | size_t reply_len = PAGE_SIZE; |
| 4729 | void *p, *end; |
| 4730 | int ret; |
| 4731 | |
| 4732 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4733 | "rbd", "parent_get", CEPH_OSD_FLAG_READ, |
| 4734 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4735 | if (ret) |
| 4736 | return ret == -EOPNOTSUPP ? 1 : ret; |
| 4737 | |
| 4738 | p = page_address(reply_page); |
| 4739 | end = p + reply_len; |
| 4740 | ret = decode_parent_image_spec(&p, end, pii); |
| 4741 | if (ret) |
| 4742 | return ret; |
| 4743 | |
| 4744 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4745 | "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ, |
| 4746 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4747 | if (ret) |
| 4748 | return ret; |
| 4749 | |
| 4750 | p = page_address(reply_page); |
| 4751 | end = p + reply_len; |
| 4752 | ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval); |
| 4753 | if (pii->has_overlap) |
| 4754 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 4755 | |
| 4756 | return 0; |
| 4757 | |
| 4758 | e_inval: |
| 4759 | return -EINVAL; |
| 4760 | } |
| 4761 | |
| 4762 | /* |
| 4763 | * The caller is responsible for @pii. |
| 4764 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4765 | static int __get_parent_info_legacy(struct rbd_device *rbd_dev, |
| 4766 | struct page *req_page, |
| 4767 | struct page *reply_page, |
| 4768 | struct parent_image_info *pii) |
| 4769 | { |
| 4770 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4771 | size_t reply_len = PAGE_SIZE; |
| 4772 | void *p, *end; |
| 4773 | int ret; |
| 4774 | |
| 4775 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4776 | "rbd", "get_parent", CEPH_OSD_FLAG_READ, |
| 4777 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4778 | if (ret) |
| 4779 | return ret; |
| 4780 | |
| 4781 | p = page_address(reply_page); |
| 4782 | end = p + reply_len; |
| 4783 | ceph_decode_64_safe(&p, end, pii->pool_id, e_inval); |
| 4784 | pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
| 4785 | if (IS_ERR(pii->image_id)) { |
| 4786 | ret = PTR_ERR(pii->image_id); |
| 4787 | pii->image_id = NULL; |
| 4788 | return ret; |
| 4789 | } |
| 4790 | ceph_decode_64_safe(&p, end, pii->snap_id, e_inval); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4791 | pii->has_overlap = true; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4792 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 4793 | |
| 4794 | return 0; |
| 4795 | |
| 4796 | e_inval: |
| 4797 | return -EINVAL; |
| 4798 | } |
| 4799 | |
| 4800 | static int get_parent_info(struct rbd_device *rbd_dev, |
| 4801 | struct parent_image_info *pii) |
| 4802 | { |
| 4803 | struct page *req_page, *reply_page; |
| 4804 | void *p; |
| 4805 | int ret; |
| 4806 | |
| 4807 | req_page = alloc_page(GFP_KERNEL); |
| 4808 | if (!req_page) |
| 4809 | return -ENOMEM; |
| 4810 | |
| 4811 | reply_page = alloc_page(GFP_KERNEL); |
| 4812 | if (!reply_page) { |
| 4813 | __free_page(req_page); |
| 4814 | return -ENOMEM; |
| 4815 | } |
| 4816 | |
| 4817 | p = page_address(req_page); |
| 4818 | ceph_encode_64(&p, rbd_dev->spec->snap_id); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4819 | ret = __get_parent_info(rbd_dev, req_page, reply_page, pii); |
| 4820 | if (ret > 0) |
| 4821 | ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page, |
| 4822 | pii); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4823 | |
| 4824 | __free_page(req_page); |
| 4825 | __free_page(reply_page); |
| 4826 | return ret; |
| 4827 | } |
| 4828 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4829 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4830 | { |
| 4831 | struct rbd_spec *parent_spec; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4832 | struct parent_image_info pii = { 0 }; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4833 | int ret; |
| 4834 | |
| 4835 | parent_spec = rbd_spec_alloc(); |
| 4836 | if (!parent_spec) |
| 4837 | return -ENOMEM; |
| 4838 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4839 | ret = get_parent_info(rbd_dev, &pii); |
| 4840 | if (ret) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4841 | goto out_err; |
| 4842 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4843 | dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n", |
| 4844 | __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id, |
| 4845 | pii.has_overlap, pii.overlap); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4846 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4847 | if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) { |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4848 | /* |
| 4849 | * Either the parent never existed, or we have |
| 4850 | * record of it but the image got flattened so it no |
| 4851 | * longer has a parent. When the parent of a |
| 4852 | * layered image disappears we immediately set the |
| 4853 | * overlap to 0. The effect of this is that all new |
| 4854 | * requests will be treated as if the image had no |
| 4855 | * parent. |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4856 | * |
| 4857 | * If !pii.has_overlap, the parent image spec is not |
| 4858 | * applicable. It's there to avoid duplication in each |
| 4859 | * snapshot record. |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4860 | */ |
| 4861 | if (rbd_dev->parent_overlap) { |
| 4862 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4863 | rbd_dev_parent_put(rbd_dev); |
| 4864 | pr_info("%s: clone image has been flattened\n", |
| 4865 | rbd_dev->disk->disk_name); |
| 4866 | } |
| 4867 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4868 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4869 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4870 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4871 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4872 | |
| 4873 | ret = -EIO; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4874 | if (pii.pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4875 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4876 | (unsigned long long)pii.pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4877 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4878 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4879 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4880 | /* |
| 4881 | * The parent won't change (except when the clone is |
| 4882 | * flattened, already handled that). So we only need to |
| 4883 | * record the parent spec we have not already done so. |
| 4884 | */ |
| 4885 | if (!rbd_dev->parent_spec) { |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4886 | parent_spec->pool_id = pii.pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4887 | if (pii.pool_ns && *pii.pool_ns) { |
| 4888 | parent_spec->pool_ns = pii.pool_ns; |
| 4889 | pii.pool_ns = NULL; |
| 4890 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4891 | parent_spec->image_id = pii.image_id; |
| 4892 | pii.image_id = NULL; |
| 4893 | parent_spec->snap_id = pii.snap_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4894 | |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4895 | rbd_dev->parent_spec = parent_spec; |
| 4896 | parent_spec = NULL; /* rbd_dev now owns this */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4900 | * We always update the parent overlap. If it's zero we issue |
| 4901 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4902 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4903 | if (!pii.overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4904 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4905 | /* refresh, careful to warn just once */ |
| 4906 | if (rbd_dev->parent_overlap) |
| 4907 | rbd_warn(rbd_dev, |
| 4908 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4909 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4910 | /* initial probe */ |
| 4911 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4912 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4913 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4914 | rbd_dev->parent_overlap = pii.overlap; |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4915 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4916 | out: |
| 4917 | ret = 0; |
| 4918 | out_err: |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4919 | kfree(pii.pool_ns); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4920 | kfree(pii.image_id); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4921 | rbd_spec_put(parent_spec); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4922 | return ret; |
| 4923 | } |
| 4924 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4925 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 4926 | { |
| 4927 | struct { |
| 4928 | __le64 stripe_unit; |
| 4929 | __le64 stripe_count; |
| 4930 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 4931 | size_t size = sizeof (striping_info_buf); |
| 4932 | void *p; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4933 | int ret; |
| 4934 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4935 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4936 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 4937 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4938 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 4939 | if (ret < 0) |
| 4940 | return ret; |
| 4941 | if (ret < size) |
| 4942 | return -ERANGE; |
| 4943 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4944 | p = &striping_info_buf; |
Ilya Dryomov | b133185 | 2018-02-07 12:09:12 +0100 | [diff] [blame] | 4945 | rbd_dev->header.stripe_unit = ceph_decode_64(&p); |
| 4946 | rbd_dev->header.stripe_count = ceph_decode_64(&p); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4947 | return 0; |
| 4948 | } |
| 4949 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4950 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 4951 | { |
| 4952 | __le64 data_pool_id; |
| 4953 | int ret; |
| 4954 | |
| 4955 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4956 | &rbd_dev->header_oloc, "get_data_pool", |
| 4957 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 4958 | if (ret < 0) |
| 4959 | return ret; |
| 4960 | if (ret < sizeof(data_pool_id)) |
| 4961 | return -EBADMSG; |
| 4962 | |
| 4963 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 4964 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 4965 | return 0; |
| 4966 | } |
| 4967 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4968 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 4969 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4970 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4971 | size_t image_id_size; |
| 4972 | char *image_id; |
| 4973 | void *p; |
| 4974 | void *end; |
| 4975 | size_t size; |
| 4976 | void *reply_buf = NULL; |
| 4977 | size_t len = 0; |
| 4978 | char *image_name = NULL; |
| 4979 | int ret; |
| 4980 | |
| 4981 | rbd_assert(!rbd_dev->spec->image_name); |
| 4982 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4983 | len = strlen(rbd_dev->spec->image_id); |
| 4984 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4985 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 4986 | if (!image_id) |
| 4987 | return NULL; |
| 4988 | |
| 4989 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4990 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4991 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4992 | |
| 4993 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 4994 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 4995 | if (!reply_buf) |
| 4996 | goto out; |
| 4997 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4998 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 4999 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5000 | "dir_get_name", image_id, image_id_size, |
| 5001 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5002 | if (ret < 0) |
| 5003 | goto out; |
| 5004 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5005 | end = reply_buf + ret; |
| 5006 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5007 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5008 | if (IS_ERR(image_name)) |
| 5009 | image_name = NULL; |
| 5010 | else |
| 5011 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5012 | out: |
| 5013 | kfree(reply_buf); |
| 5014 | kfree(image_id); |
| 5015 | |
| 5016 | return image_name; |
| 5017 | } |
| 5018 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5019 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5020 | { |
| 5021 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5022 | const char *snap_name; |
| 5023 | u32 which = 0; |
| 5024 | |
| 5025 | /* Skip over names until we find the one we are looking for */ |
| 5026 | |
| 5027 | snap_name = rbd_dev->header.snap_names; |
| 5028 | while (which < snapc->num_snaps) { |
| 5029 | if (!strcmp(name, snap_name)) |
| 5030 | return snapc->snaps[which]; |
| 5031 | snap_name += strlen(snap_name) + 1; |
| 5032 | which++; |
| 5033 | } |
| 5034 | return CEPH_NOSNAP; |
| 5035 | } |
| 5036 | |
| 5037 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5038 | { |
| 5039 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5040 | u32 which; |
| 5041 | bool found = false; |
| 5042 | u64 snap_id; |
| 5043 | |
| 5044 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5045 | const char *snap_name; |
| 5046 | |
| 5047 | snap_id = snapc->snaps[which]; |
| 5048 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5049 | if (IS_ERR(snap_name)) { |
| 5050 | /* ignore no-longer existing snapshots */ |
| 5051 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5052 | continue; |
| 5053 | else |
| 5054 | break; |
| 5055 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5056 | found = !strcmp(name, snap_name); |
| 5057 | kfree(snap_name); |
| 5058 | } |
| 5059 | return found ? snap_id : CEPH_NOSNAP; |
| 5060 | } |
| 5061 | |
| 5062 | /* |
| 5063 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5064 | * no snapshot by that name is found, or if an error occurs. |
| 5065 | */ |
| 5066 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5067 | { |
| 5068 | if (rbd_dev->image_format == 1) |
| 5069 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5070 | |
| 5071 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5072 | } |
| 5073 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5074 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5075 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5076 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5077 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5078 | { |
| 5079 | struct rbd_spec *spec = rbd_dev->spec; |
| 5080 | |
| 5081 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5082 | rbd_assert(spec->image_id && spec->image_name); |
| 5083 | rbd_assert(spec->snap_name); |
| 5084 | |
| 5085 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5086 | u64 snap_id; |
| 5087 | |
| 5088 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5089 | if (snap_id == CEPH_NOSNAP) |
| 5090 | return -ENOENT; |
| 5091 | |
| 5092 | spec->snap_id = snap_id; |
| 5093 | } else { |
| 5094 | spec->snap_id = CEPH_NOSNAP; |
| 5095 | } |
| 5096 | |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
| 5100 | /* |
| 5101 | * A parent image will have all ids but none of the names. |
| 5102 | * |
| 5103 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5104 | * can't figure out the name for an image id. |
| 5105 | */ |
| 5106 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5107 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5108 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5109 | struct rbd_spec *spec = rbd_dev->spec; |
| 5110 | const char *pool_name; |
| 5111 | const char *image_name; |
| 5112 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5113 | int ret; |
| 5114 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5115 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5116 | rbd_assert(spec->image_id); |
| 5117 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5118 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5119 | /* 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] | 5120 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5121 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5122 | if (!pool_name) { |
| 5123 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5124 | return -EIO; |
| 5125 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5126 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5127 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5128 | return -ENOMEM; |
| 5129 | |
| 5130 | /* Fetch the image name; tolerate failure here */ |
| 5131 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5132 | image_name = rbd_dev_image_name(rbd_dev); |
| 5133 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5134 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5135 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5136 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5137 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5138 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5139 | if (IS_ERR(snap_name)) { |
| 5140 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5141 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | spec->pool_name = pool_name; |
| 5145 | spec->image_name = image_name; |
| 5146 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5147 | |
| 5148 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5149 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5150 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5151 | kfree(image_name); |
| 5152 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5153 | return ret; |
| 5154 | } |
| 5155 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5156 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5157 | { |
| 5158 | size_t size; |
| 5159 | int ret; |
| 5160 | void *reply_buf; |
| 5161 | void *p; |
| 5162 | void *end; |
| 5163 | u64 seq; |
| 5164 | u32 snap_count; |
| 5165 | struct ceph_snap_context *snapc; |
| 5166 | u32 i; |
| 5167 | |
| 5168 | /* |
| 5169 | * We'll need room for the seq value (maximum snapshot id), |
| 5170 | * snapshot count, and array of that many snapshot ids. |
| 5171 | * For now we have a fixed upper limit on the number we're |
| 5172 | * prepared to receive. |
| 5173 | */ |
| 5174 | size = sizeof (__le64) + sizeof (__le32) + |
| 5175 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5176 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5177 | if (!reply_buf) |
| 5178 | return -ENOMEM; |
| 5179 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5180 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5181 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5182 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5183 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5184 | if (ret < 0) |
| 5185 | goto out; |
| 5186 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5187 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5188 | end = reply_buf + ret; |
| 5189 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5190 | ceph_decode_64_safe(&p, end, seq, out); |
| 5191 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5192 | |
| 5193 | /* |
| 5194 | * Make sure the reported number of snapshot ids wouldn't go |
| 5195 | * beyond the end of our buffer. But before checking that, |
| 5196 | * make sure the computed size of the snapshot context we |
| 5197 | * allocate is representable in a size_t. |
| 5198 | */ |
| 5199 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5200 | / sizeof (u64)) { |
| 5201 | ret = -EINVAL; |
| 5202 | goto out; |
| 5203 | } |
| 5204 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5205 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5206 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5207 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5208 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5209 | if (!snapc) { |
| 5210 | ret = -ENOMEM; |
| 5211 | goto out; |
| 5212 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5213 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5214 | for (i = 0; i < snap_count; i++) |
| 5215 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5216 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5217 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5218 | rbd_dev->header.snapc = snapc; |
| 5219 | |
| 5220 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5221 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5222 | out: |
| 5223 | kfree(reply_buf); |
| 5224 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5225 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5226 | } |
| 5227 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5228 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5229 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5230 | { |
| 5231 | size_t size; |
| 5232 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5233 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5234 | int ret; |
| 5235 | void *p; |
| 5236 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5237 | char *snap_name; |
| 5238 | |
| 5239 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5240 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5241 | if (!reply_buf) |
| 5242 | return ERR_PTR(-ENOMEM); |
| 5243 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5244 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5245 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5246 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5247 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5248 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5249 | if (ret < 0) { |
| 5250 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5251 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5252 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5253 | |
| 5254 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5255 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5256 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5257 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5258 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5259 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5260 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5261 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5262 | out: |
| 5263 | kfree(reply_buf); |
| 5264 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5265 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5266 | } |
| 5267 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5268 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5269 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5270 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5271 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5272 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5273 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5274 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5275 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5276 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5277 | if (first_time) { |
| 5278 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5279 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5280 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5281 | } |
| 5282 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5283 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5284 | if (ret && first_time) { |
| 5285 | kfree(rbd_dev->header.object_prefix); |
| 5286 | rbd_dev->header.object_prefix = NULL; |
| 5287 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5288 | |
| 5289 | return ret; |
| 5290 | } |
| 5291 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5292 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5293 | { |
| 5294 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5295 | |
| 5296 | if (rbd_dev->image_format == 1) |
| 5297 | return rbd_dev_v1_header_info(rbd_dev); |
| 5298 | |
| 5299 | return rbd_dev_v2_header_info(rbd_dev); |
| 5300 | } |
| 5301 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5302 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5303 | * Skips over white space at *buf, and updates *buf to point to the |
| 5304 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5305 | * the token (string of non-white space characters) found. Note |
| 5306 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5307 | */ |
| 5308 | static inline size_t next_token(const char **buf) |
| 5309 | { |
| 5310 | /* |
| 5311 | * These are the characters that produce nonzero for |
| 5312 | * isspace() in the "C" and "POSIX" locales. |
| 5313 | */ |
| 5314 | const char *spaces = " \f\n\r\t\v"; |
| 5315 | |
| 5316 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5317 | |
| 5318 | return strcspn(*buf, spaces); /* Return token length */ |
| 5319 | } |
| 5320 | |
| 5321 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5322 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5323 | * enough to hold a copy of it, and copies the token into the new |
| 5324 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5325 | * that a duplicate buffer is created even for a zero-length token. |
| 5326 | * |
| 5327 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5328 | * pointer if memory for the duplicate was not available. If |
| 5329 | * the lenp argument is a non-null pointer, the length of the token |
| 5330 | * (not including the '\0') is returned in *lenp. |
| 5331 | * |
| 5332 | * If successful, the *buf pointer will be updated to point beyond |
| 5333 | * the end of the found token. |
| 5334 | * |
| 5335 | * Note: uses GFP_KERNEL for allocation. |
| 5336 | */ |
| 5337 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5338 | { |
| 5339 | char *dup; |
| 5340 | size_t len; |
| 5341 | |
| 5342 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5343 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5344 | if (!dup) |
| 5345 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5346 | *(dup + len) = '\0'; |
| 5347 | *buf += len; |
| 5348 | |
| 5349 | if (lenp) |
| 5350 | *lenp = len; |
| 5351 | |
| 5352 | return dup; |
| 5353 | } |
| 5354 | |
| 5355 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5356 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5357 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5358 | * and the data written is passed here via a NUL-terminated buffer. |
| 5359 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5360 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5361 | * The information extracted from these options is recorded in |
| 5362 | * the other parameters which return dynamically-allocated |
| 5363 | * structures: |
| 5364 | * ceph_opts |
| 5365 | * The address of a pointer that will refer to a ceph options |
| 5366 | * structure. Caller must release the returned pointer using |
| 5367 | * ceph_destroy_options() when it is no longer needed. |
| 5368 | * rbd_opts |
| 5369 | * Address of an rbd options pointer. Fully initialized by |
| 5370 | * this function; caller must release with kfree(). |
| 5371 | * spec |
| 5372 | * Address of an rbd image specification pointer. Fully |
| 5373 | * initialized by this function based on parsed options. |
| 5374 | * Caller must release with rbd_spec_put(). |
| 5375 | * |
| 5376 | * The options passed take this form: |
| 5377 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5378 | * where: |
| 5379 | * <mon_addrs> |
| 5380 | * A comma-separated list of one or more monitor addresses. |
| 5381 | * A monitor address is an ip address, optionally followed |
| 5382 | * by a port number (separated by a colon). |
| 5383 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5384 | * <options> |
| 5385 | * A comma-separated list of ceph and/or rbd options. |
| 5386 | * <pool_name> |
| 5387 | * The name of the rados pool containing the rbd image. |
| 5388 | * <image_name> |
| 5389 | * The name of the image in that pool to map. |
| 5390 | * <snap_id> |
| 5391 | * An optional snapshot id. If provided, the mapping will |
| 5392 | * present data from the image at the time that snapshot was |
| 5393 | * created. The image head is used if no snapshot id is |
| 5394 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5395 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5396 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5397 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5398 | struct rbd_options **opts, |
| 5399 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5400 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5401 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5402 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5403 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5404 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5405 | size_t mon_addrs_size; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5406 | struct parse_rbd_opts_ctx pctx = { 0 }; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5407 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5408 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5409 | |
| 5410 | /* The first four tokens are required */ |
| 5411 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5412 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5413 | if (!len) { |
| 5414 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5415 | return -EINVAL; |
| 5416 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5417 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5418 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5419 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5420 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5421 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5422 | options = dup_token(&buf, NULL); |
| 5423 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5424 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5425 | if (!*options) { |
| 5426 | rbd_warn(NULL, "no options provided"); |
| 5427 | goto out_err; |
| 5428 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5429 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5430 | pctx.spec = rbd_spec_alloc(); |
| 5431 | if (!pctx.spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5432 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5433 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5434 | pctx.spec->pool_name = dup_token(&buf, NULL); |
| 5435 | if (!pctx.spec->pool_name) |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5436 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5437 | if (!*pctx.spec->pool_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5438 | rbd_warn(NULL, "no pool name provided"); |
| 5439 | goto out_err; |
| 5440 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5441 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5442 | pctx.spec->image_name = dup_token(&buf, NULL); |
| 5443 | if (!pctx.spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5444 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5445 | if (!*pctx.spec->image_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5446 | rbd_warn(NULL, "no image name provided"); |
| 5447 | goto out_err; |
| 5448 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5449 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5450 | /* |
| 5451 | * Snapshot name is optional; default is to use "-" |
| 5452 | * (indicating the head/no snapshot). |
| 5453 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5454 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5455 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5456 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5457 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5458 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5459 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5460 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5461 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5462 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5463 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5464 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5465 | *(snap_name + len) = '\0'; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5466 | pctx.spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5467 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5468 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5469 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5470 | pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL); |
| 5471 | if (!pctx.opts) |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5472 | goto out_mem; |
| 5473 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5474 | pctx.opts->read_only = RBD_READ_ONLY_DEFAULT; |
| 5475 | pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 5476 | pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5477 | pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT; |
| 5478 | pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
| 5479 | pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
| 5480 | pctx.opts->trim = RBD_TRIM_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5481 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5482 | copts = ceph_parse_options(options, mon_addrs, |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5483 | mon_addrs + mon_addrs_size - 1, |
| 5484 | parse_rbd_opts_token, &pctx); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5485 | if (IS_ERR(copts)) { |
| 5486 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5487 | goto out_err; |
| 5488 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5489 | kfree(options); |
| 5490 | |
| 5491 | *ceph_opts = copts; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5492 | *opts = pctx.opts; |
| 5493 | *rbd_spec = pctx.spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5494 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5495 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5496 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5497 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5498 | out_err: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5499 | kfree(pctx.opts); |
| 5500 | rbd_spec_put(pctx.spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5501 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5502 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5503 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5504 | } |
| 5505 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5506 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5507 | { |
| 5508 | down_write(&rbd_dev->lock_rwsem); |
| 5509 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5510 | rbd_unlock(rbd_dev); |
| 5511 | up_write(&rbd_dev->lock_rwsem); |
| 5512 | } |
| 5513 | |
| 5514 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5515 | { |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5516 | int ret; |
| 5517 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5518 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5519 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5520 | return -EINVAL; |
| 5521 | } |
| 5522 | |
| 5523 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5524 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5525 | ret = rbd_wait_state_locked(rbd_dev, true); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5526 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5527 | if (ret) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5528 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5529 | return -EROFS; |
| 5530 | } |
| 5531 | |
| 5532 | return 0; |
| 5533 | } |
| 5534 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5535 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5536 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5537 | * name given to it by the user. Internally, that identifier is |
| 5538 | * what's used to specify the names of objects related to the image. |
| 5539 | * |
| 5540 | * A special "rbd id" object is used to map an rbd image name to its |
| 5541 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5542 | * with the supplied name. |
| 5543 | * |
| 5544 | * This function will record the given rbd_dev's image_id field if |
| 5545 | * it can be determined, and in that case will return 0. If any |
| 5546 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5547 | * image_id field will be unchanged (and should be NULL). |
| 5548 | */ |
| 5549 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5550 | { |
| 5551 | int ret; |
| 5552 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5553 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5554 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5555 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5556 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5557 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5558 | * When probing a parent image, the image id is already |
| 5559 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5560 | * need to fetch the image id again in this case. We |
| 5561 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5562 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5563 | if (rbd_dev->spec->image_id) { |
| 5564 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5565 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5566 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5567 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5568 | |
| 5569 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5570 | * First, see if the format 2 image id file exists, and if |
| 5571 | * so, get the image's persistent id from it. |
| 5572 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5573 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5574 | rbd_dev->spec->image_name); |
| 5575 | if (ret) |
| 5576 | return ret; |
| 5577 | |
| 5578 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5579 | |
| 5580 | /* Response will be an encoded string, which includes a length */ |
| 5581 | |
| 5582 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5583 | response = kzalloc(size, GFP_NOIO); |
| 5584 | if (!response) { |
| 5585 | ret = -ENOMEM; |
| 5586 | goto out; |
| 5587 | } |
| 5588 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5589 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5590 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5591 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5592 | "get_id", NULL, 0, |
| 5593 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5594 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5595 | if (ret == -ENOENT) { |
| 5596 | image_id = kstrdup("", GFP_KERNEL); |
| 5597 | ret = image_id ? 0 : -ENOMEM; |
| 5598 | if (!ret) |
| 5599 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5600 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5601 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5602 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5603 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5604 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5605 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5606 | if (!ret) |
| 5607 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | if (!ret) { |
| 5611 | rbd_dev->spec->image_id = image_id; |
| 5612 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5613 | } |
| 5614 | out: |
| 5615 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5616 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5617 | return ret; |
| 5618 | } |
| 5619 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5620 | /* |
| 5621 | * Undo whatever state changes are made by v1 or v2 header info |
| 5622 | * call. |
| 5623 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5624 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5625 | { |
| 5626 | struct rbd_image_header *header; |
| 5627 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5628 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5629 | |
| 5630 | /* Free dynamic fields from the header, then zero it out */ |
| 5631 | |
| 5632 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5633 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5634 | kfree(header->snap_sizes); |
| 5635 | kfree(header->snap_names); |
| 5636 | kfree(header->object_prefix); |
| 5637 | memset(header, 0, sizeof (*header)); |
| 5638 | } |
| 5639 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5640 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5641 | { |
| 5642 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5643 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5644 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5645 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5646 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5647 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5648 | /* |
| 5649 | * Get the and check features for the image. Currently the |
| 5650 | * features are assumed to never change. |
| 5651 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5652 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5653 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5654 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5655 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5656 | /* If the image supports fancy striping, get its parameters */ |
| 5657 | |
| 5658 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5659 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5660 | if (ret < 0) |
| 5661 | goto out_err; |
| 5662 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5663 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5664 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5665 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5666 | if (ret) |
| 5667 | goto out_err; |
| 5668 | } |
| 5669 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5670 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5671 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5672 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5673 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5674 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5675 | kfree(rbd_dev->header.object_prefix); |
| 5676 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5677 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5678 | } |
| 5679 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5680 | /* |
| 5681 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5682 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5683 | * length of the already discovered part of the parent chain. |
| 5684 | */ |
| 5685 | 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] | 5686 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5687 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5688 | int ret; |
| 5689 | |
| 5690 | if (!rbd_dev->parent_spec) |
| 5691 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5692 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5693 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5694 | pr_info("parent chain is too long (%d)\n", depth); |
| 5695 | ret = -EINVAL; |
| 5696 | goto out_err; |
| 5697 | } |
| 5698 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5699 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5700 | if (!parent) { |
| 5701 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5702 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5703 | } |
| 5704 | |
| 5705 | /* |
| 5706 | * Images related by parent/child relationships always share |
| 5707 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5708 | */ |
| 5709 | __rbd_get_client(rbd_dev->rbd_client); |
| 5710 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5711 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5712 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5713 | if (ret < 0) |
| 5714 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5715 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5716 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5717 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5718 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5719 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5720 | out_err: |
| 5721 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5722 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5723 | return ret; |
| 5724 | } |
| 5725 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5726 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5727 | { |
| 5728 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5729 | rbd_dev_mapping_clear(rbd_dev); |
| 5730 | rbd_free_disk(rbd_dev); |
| 5731 | if (!single_major) |
| 5732 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5733 | } |
| 5734 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5735 | /* |
| 5736 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5737 | * upon return. |
| 5738 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5739 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5740 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5741 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5742 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5743 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5744 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5745 | if (!single_major) { |
| 5746 | ret = register_blkdev(0, rbd_dev->name); |
| 5747 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5748 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5749 | |
| 5750 | rbd_dev->major = ret; |
| 5751 | rbd_dev->minor = 0; |
| 5752 | } else { |
| 5753 | rbd_dev->major = rbd_major; |
| 5754 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5755 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5756 | |
| 5757 | /* Set up the blkdev mapping. */ |
| 5758 | |
| 5759 | ret = rbd_init_disk(rbd_dev); |
| 5760 | if (ret) |
| 5761 | goto err_out_blkdev; |
| 5762 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5763 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5764 | if (ret) |
| 5765 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5766 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5767 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5768 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5769 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5770 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5771 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5772 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5773 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5774 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5775 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5776 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5777 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5778 | err_out_mapping: |
| 5779 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5780 | err_out_disk: |
| 5781 | rbd_free_disk(rbd_dev); |
| 5782 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5783 | if (!single_major) |
| 5784 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5785 | err_out_unlock: |
| 5786 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5787 | return ret; |
| 5788 | } |
| 5789 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5790 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5791 | { |
| 5792 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5793 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5794 | |
| 5795 | /* Record the header object name for this rbd image. */ |
| 5796 | |
| 5797 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5798 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5799 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5800 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5801 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5802 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5803 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5804 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5805 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5806 | } |
| 5807 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5808 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5809 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5810 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5811 | if (rbd_dev->opts) |
| 5812 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5813 | rbd_dev->image_format = 0; |
| 5814 | kfree(rbd_dev->spec->image_id); |
| 5815 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5816 | } |
| 5817 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5818 | /* |
| 5819 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5820 | * device. If this image is the one being mapped (i.e., not a |
| 5821 | * parent), initiate a watch on its header object before using that |
| 5822 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5823 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5824 | 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] | 5825 | { |
| 5826 | int ret; |
| 5827 | |
| 5828 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5829 | * Get the id from the image id object. Unless there's an |
| 5830 | * error, rbd_dev->spec->image_id will be filled in with |
| 5831 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5832 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5833 | */ |
| 5834 | ret = rbd_dev_image_id(rbd_dev); |
| 5835 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5836 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5837 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5838 | ret = rbd_dev_header_name(rbd_dev); |
| 5839 | if (ret) |
| 5840 | goto err_out_format; |
| 5841 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5842 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5843 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5844 | if (ret) { |
| 5845 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5846 | pr_info("image %s/%s%s%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5847 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5848 | rbd_dev->spec->pool_ns ?: "", |
| 5849 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5850 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5851 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5852 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5853 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5854 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5855 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5856 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5857 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5858 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5859 | /* |
| 5860 | * If this image is the one being mapped, we have pool name and |
| 5861 | * id, image name and id, and snap name - need to fill snap id. |
| 5862 | * Otherwise this is a parent image, identified by pool, image |
| 5863 | * and snap ids - need to fill in names for those ids. |
| 5864 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5865 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5866 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5867 | else |
| 5868 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5869 | if (ret) { |
| 5870 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5871 | pr_info("snap %s/%s%s%s@%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5872 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5873 | rbd_dev->spec->pool_ns ?: "", |
| 5874 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5875 | rbd_dev->spec->image_name, |
| 5876 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5877 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5878 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5879 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5880 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5881 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5882 | if (ret) |
| 5883 | goto err_out_probe; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5884 | } |
| 5885 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5886 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5887 | if (ret) |
| 5888 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5889 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5890 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5891 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5892 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5893 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5894 | err_out_probe: |
| 5895 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5896 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5897 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5898 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5899 | err_out_format: |
| 5900 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5901 | kfree(rbd_dev->spec->image_id); |
| 5902 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5903 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5904 | } |
| 5905 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5906 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 5907 | const char *buf, |
| 5908 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5909 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5910 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5911 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5912 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5913 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5914 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5915 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5916 | |
| 5917 | if (!try_module_get(THIS_MODULE)) |
| 5918 | return -ENODEV; |
| 5919 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5920 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5921 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5922 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5923 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5924 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5925 | rbdc = rbd_get_client(ceph_opts); |
| 5926 | if (IS_ERR(rbdc)) { |
| 5927 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5928 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5929 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5930 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5931 | /* pick the pool */ |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 5932 | 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] | 5933 | if (rc < 0) { |
| 5934 | if (rc == -ENOENT) |
| 5935 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5936 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5937 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5938 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5939 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5940 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5941 | if (!rbd_dev) { |
| 5942 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5943 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 5944 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5945 | rbdc = NULL; /* rbd_dev now owns this */ |
| 5946 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 5947 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 5948 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5949 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 5950 | if (!rbd_dev->config_info) { |
| 5951 | rc = -ENOMEM; |
| 5952 | goto err_out_rbd_dev; |
| 5953 | } |
| 5954 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5955 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5956 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5957 | if (rc < 0) { |
| 5958 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5959 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 5960 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 5961 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5962 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5963 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5964 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5965 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 5966 | if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) { |
| 5967 | rbd_warn(rbd_dev, "alloc_size adjusted to %u", |
| 5968 | rbd_dev->layout.object_size); |
| 5969 | rbd_dev->opts->alloc_size = rbd_dev->layout.object_size; |
| 5970 | } |
| 5971 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5972 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5973 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5974 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5975 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5976 | if (rbd_dev->opts->exclusive) { |
| 5977 | rc = rbd_add_acquire_lock(rbd_dev); |
| 5978 | if (rc) |
| 5979 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5980 | } |
| 5981 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5982 | /* Everything's ready. Announce the disk to the world. */ |
| 5983 | |
| 5984 | rc = device_add(&rbd_dev->dev); |
| 5985 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5986 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5987 | |
| 5988 | add_disk(rbd_dev->disk); |
| 5989 | /* see rbd_init_disk() */ |
| 5990 | blk_put_queue(rbd_dev->disk->queue); |
| 5991 | |
| 5992 | spin_lock(&rbd_dev_list_lock); |
| 5993 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 5994 | spin_unlock(&rbd_dev_list_lock); |
| 5995 | |
| 5996 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 5997 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 5998 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 5999 | rc = count; |
| 6000 | out: |
| 6001 | module_put(THIS_MODULE); |
| 6002 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6003 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6004 | err_out_image_lock: |
| 6005 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6006 | err_out_device_setup: |
| 6007 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6008 | err_out_image_probe: |
| 6009 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6010 | err_out_rbd_dev: |
| 6011 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6012 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6013 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6014 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6015 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6016 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6017 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6018 | } |
| 6019 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6020 | static ssize_t rbd_add(struct bus_type *bus, |
| 6021 | const char *buf, |
| 6022 | size_t count) |
| 6023 | { |
| 6024 | if (single_major) |
| 6025 | return -EINVAL; |
| 6026 | |
| 6027 | return do_rbd_add(bus, buf, count); |
| 6028 | } |
| 6029 | |
| 6030 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6031 | const char *buf, |
| 6032 | size_t count) |
| 6033 | { |
| 6034 | return do_rbd_add(bus, buf, count); |
| 6035 | } |
| 6036 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6037 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6038 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6039 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6040 | struct rbd_device *first = rbd_dev; |
| 6041 | struct rbd_device *second = first->parent; |
| 6042 | struct rbd_device *third; |
| 6043 | |
| 6044 | /* |
| 6045 | * Follow to the parent with no grandparent and |
| 6046 | * remove it. |
| 6047 | */ |
| 6048 | while (second && (third = second->parent)) { |
| 6049 | first = second; |
| 6050 | second = third; |
| 6051 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6052 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6053 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6054 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6055 | first->parent = NULL; |
| 6056 | first->parent_overlap = 0; |
| 6057 | |
| 6058 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6059 | rbd_spec_put(first->parent_spec); |
| 6060 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6064 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6065 | const char *buf, |
| 6066 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6067 | { |
| 6068 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6069 | struct list_head *tmp; |
| 6070 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6071 | char opt_buf[6]; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6072 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6073 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6074 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6075 | dev_id = -1; |
| 6076 | opt_buf[0] = '\0'; |
| 6077 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6078 | if (dev_id < 0) { |
| 6079 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6080 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6081 | } |
| 6082 | if (opt_buf[0] != '\0') { |
| 6083 | if (!strcmp(opt_buf, "force")) { |
| 6084 | force = true; |
| 6085 | } else { |
| 6086 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6087 | return -EINVAL; |
| 6088 | } |
| 6089 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6090 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6091 | ret = -ENOENT; |
| 6092 | spin_lock(&rbd_dev_list_lock); |
| 6093 | list_for_each(tmp, &rbd_dev_list) { |
| 6094 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6095 | if (rbd_dev->dev_id == dev_id) { |
| 6096 | ret = 0; |
| 6097 | break; |
| 6098 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6099 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6100 | if (!ret) { |
| 6101 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6102 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6103 | ret = -EBUSY; |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6104 | else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6105 | &rbd_dev->flags)) |
| 6106 | ret = -EINPROGRESS; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6107 | spin_unlock_irq(&rbd_dev->lock); |
| 6108 | } |
| 6109 | spin_unlock(&rbd_dev_list_lock); |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6110 | if (ret) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6111 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6112 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6113 | if (force) { |
| 6114 | /* |
| 6115 | * Prevent new IO from being queued and wait for existing |
| 6116 | * IO to complete/fail. |
| 6117 | */ |
| 6118 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6119 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6120 | } |
| 6121 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6122 | del_gendisk(rbd_dev->disk); |
| 6123 | spin_lock(&rbd_dev_list_lock); |
| 6124 | list_del_init(&rbd_dev->node); |
| 6125 | spin_unlock(&rbd_dev_list_lock); |
| 6126 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6127 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6128 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6129 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6130 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6131 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6132 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6133 | } |
| 6134 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6135 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6136 | const char *buf, |
| 6137 | size_t count) |
| 6138 | { |
| 6139 | if (single_major) |
| 6140 | return -EINVAL; |
| 6141 | |
| 6142 | return do_rbd_remove(bus, buf, count); |
| 6143 | } |
| 6144 | |
| 6145 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6146 | const char *buf, |
| 6147 | size_t count) |
| 6148 | { |
| 6149 | return do_rbd_remove(bus, buf, count); |
| 6150 | } |
| 6151 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6152 | /* |
| 6153 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6154 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6155 | */ |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6156 | static int __init rbd_sysfs_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6157 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6158 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6159 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6160 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6161 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6162 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6163 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6164 | ret = bus_register(&rbd_bus_type); |
| 6165 | if (ret < 0) |
| 6166 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6167 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6168 | return ret; |
| 6169 | } |
| 6170 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6171 | static void __exit rbd_sysfs_cleanup(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6172 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6173 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6174 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6175 | } |
| 6176 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6177 | static int __init rbd_slab_init(void) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6178 | { |
| 6179 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6180 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6181 | if (!rbd_img_request_cache) |
| 6182 | return -ENOMEM; |
| 6183 | |
| 6184 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6185 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6186 | if (!rbd_obj_request_cache) |
| 6187 | goto out_err; |
| 6188 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6189 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6190 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6191 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6192 | kmem_cache_destroy(rbd_img_request_cache); |
| 6193 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6194 | return -ENOMEM; |
| 6195 | } |
| 6196 | |
| 6197 | static void rbd_slab_exit(void) |
| 6198 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6199 | rbd_assert(rbd_obj_request_cache); |
| 6200 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6201 | rbd_obj_request_cache = NULL; |
| 6202 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6203 | rbd_assert(rbd_img_request_cache); |
| 6204 | kmem_cache_destroy(rbd_img_request_cache); |
| 6205 | rbd_img_request_cache = NULL; |
| 6206 | } |
| 6207 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6208 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6209 | { |
| 6210 | int rc; |
| 6211 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6212 | if (!libceph_compatible(NULL)) { |
| 6213 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6214 | return -EINVAL; |
| 6215 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6216 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6217 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6218 | if (rc) |
| 6219 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6220 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6221 | /* |
| 6222 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6223 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6224 | */ |
| 6225 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6226 | if (!rbd_wq) { |
| 6227 | rc = -ENOMEM; |
| 6228 | goto err_out_slab; |
| 6229 | } |
| 6230 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6231 | if (single_major) { |
| 6232 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6233 | if (rbd_major < 0) { |
| 6234 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6235 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6236 | } |
| 6237 | } |
| 6238 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6239 | rc = rbd_sysfs_init(); |
| 6240 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6241 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6242 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6243 | if (single_major) |
| 6244 | pr_info("loaded (major %d)\n", rbd_major); |
| 6245 | else |
| 6246 | pr_info("loaded\n"); |
| 6247 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6248 | return 0; |
| 6249 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6250 | err_out_blkdev: |
| 6251 | if (single_major) |
| 6252 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6253 | err_out_wq: |
| 6254 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6255 | err_out_slab: |
| 6256 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6257 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6258 | } |
| 6259 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6260 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6261 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6262 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6263 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6264 | if (single_major) |
| 6265 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6266 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6267 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | module_init(rbd_init); |
| 6271 | module_exit(rbd_exit); |
| 6272 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6273 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6274 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6275 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6276 | /* following authorship retained from original osdblk.c */ |
| 6277 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6278 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6279 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6280 | MODULE_LICENSE("GPL"); |