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) |
Ilya Dryomov | b9f6d44 | 2019-02-25 18:55:38 +0100 | [diff] [blame] | 118 | #define RBD_FEATURE_DEEP_FLATTEN (1ULL<<5) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 119 | #define RBD_FEATURE_DATA_POOL (1ULL<<7) |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 120 | #define RBD_FEATURE_OPERATIONS (1ULL<<8) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 121 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 122 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 123 | RBD_FEATURE_STRIPINGV2 | \ |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 124 | RBD_FEATURE_EXCLUSIVE_LOCK | \ |
Ilya Dryomov | b9f6d44 | 2019-02-25 18:55:38 +0100 | [diff] [blame] | 125 | RBD_FEATURE_DEEP_FLATTEN | \ |
Ilya Dryomov | e573427 | 2018-01-16 15:41:54 +0100 | [diff] [blame] | 126 | RBD_FEATURE_DATA_POOL | \ |
| 127 | RBD_FEATURE_OPERATIONS) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 128 | |
| 129 | /* Features supported by this (client software) implementation. */ |
| 130 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 131 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 132 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 133 | /* |
| 134 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 135 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 136 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 137 | #define DEV_NAME_LEN 32 |
| 138 | |
| 139 | /* |
| 140 | * block device image metadata (in-memory version) |
| 141 | */ |
| 142 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 143 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 144 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 145 | __u8 obj_order; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 146 | u64 stripe_unit; |
| 147 | u64 stripe_count; |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 148 | s64 data_pool_id; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 149 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 150 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 151 | /* The remaining fields need to be updated occasionally */ |
| 152 | u64 image_size; |
| 153 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 154 | char *snap_names; /* format 1 only */ |
| 155 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 158 | /* |
| 159 | * An rbd image specification. |
| 160 | * |
| 161 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 162 | * identify an image. Each rbd_dev structure includes a pointer to |
| 163 | * an rbd_spec structure that encapsulates this identity. |
| 164 | * |
| 165 | * Each of the id's in an rbd_spec has an associated name. For a |
| 166 | * user-mapped image, the names are supplied and the id's associated |
| 167 | * with them are looked up. For a layered image, a parent image is |
| 168 | * defined by the tuple, and the names are looked up. |
| 169 | * |
| 170 | * An rbd_dev structure contains a parent_spec pointer which is |
| 171 | * non-null if the image it represents is a child in a layered |
| 172 | * image. This pointer will refer to the rbd_spec structure used |
| 173 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 174 | * is shared between the parent and child). |
| 175 | * |
| 176 | * Since these structures are populated once, during the discovery |
| 177 | * phase of image construction, they are effectively immutable so |
| 178 | * we make no effort to synchronize access to them. |
| 179 | * |
| 180 | * Note that code herein does not assume the image name is known (it |
| 181 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 182 | */ |
| 183 | struct rbd_spec { |
| 184 | u64 pool_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 185 | const char *pool_name; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 186 | const char *pool_ns; /* NULL if default, never "" */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 187 | |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 188 | const char *image_id; |
| 189 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 190 | |
| 191 | u64 snap_id; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 192 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 193 | |
| 194 | struct kref kref; |
| 195 | }; |
| 196 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 197 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 198 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 199 | */ |
| 200 | struct rbd_client { |
| 201 | struct ceph_client *client; |
| 202 | struct kref kref; |
| 203 | struct list_head node; |
| 204 | }; |
| 205 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 206 | struct pending_result { |
| 207 | int result; /* first nonzero result */ |
| 208 | int num_pending; |
| 209 | }; |
| 210 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 211 | struct rbd_img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 212 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 213 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 214 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 215 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 216 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 217 | OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */ |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 218 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 219 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 220 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 221 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 222 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 223 | OBJ_OP_DISCARD, |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 224 | OBJ_OP_ZEROOUT, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 225 | }; |
| 226 | |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 227 | #define RBD_OBJ_FLAG_DELETION (1U << 0) |
| 228 | #define RBD_OBJ_FLAG_COPYUP_ENABLED (1U << 1) |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 229 | #define RBD_OBJ_FLAG_COPYUP_ZEROS (1U << 2) |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 230 | |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 231 | enum rbd_obj_read_state { |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 232 | RBD_OBJ_READ_START = 1, |
| 233 | RBD_OBJ_READ_OBJECT, |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 234 | RBD_OBJ_READ_PARENT, |
| 235 | }; |
| 236 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Writes go through the following state machine to deal with |
| 239 | * layering: |
| 240 | * |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 241 | * . . . . . RBD_OBJ_WRITE_GUARD. . . . . . . . . . . . . . |
| 242 | * . | . |
| 243 | * . v . |
| 244 | * . RBD_OBJ_WRITE_READ_FROM_PARENT. . . . |
| 245 | * . | . . |
| 246 | * . v v (deep-copyup . |
| 247 | * (image . RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC . not needed) . |
| 248 | * flattened) v | . . |
| 249 | * . v . . |
| 250 | * . . . .RBD_OBJ_WRITE_COPYUP_OPS. . . . . (copyup . |
| 251 | * | not needed) v |
| 252 | * v . |
| 253 | * done . . . . . . . . . . . . . . . . . . |
| 254 | * ^ |
| 255 | * | |
| 256 | * RBD_OBJ_WRITE_FLAT |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 257 | * |
| 258 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 259 | * assert_exists guard is needed or not (in some cases it's not needed |
| 260 | * even if there is a parent). |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 261 | */ |
| 262 | enum rbd_obj_write_state { |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 263 | RBD_OBJ_WRITE_START = 1, |
| 264 | RBD_OBJ_WRITE_OBJECT, |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 265 | __RBD_OBJ_WRITE_COPYUP, |
| 266 | RBD_OBJ_WRITE_COPYUP, |
| 267 | }; |
| 268 | |
| 269 | enum rbd_obj_copyup_state { |
| 270 | RBD_OBJ_COPYUP_START = 1, |
| 271 | RBD_OBJ_COPYUP_READ_PARENT, |
| 272 | __RBD_OBJ_COPYUP_WRITE_OBJECT, |
| 273 | RBD_OBJ_COPYUP_WRITE_OBJECT, |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 274 | }; |
| 275 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | struct rbd_obj_request { |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 277 | struct ceph_object_extent ex; |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 278 | unsigned int flags; /* RBD_OBJ_FLAG_* */ |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 279 | union { |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 280 | enum rbd_obj_read_state read_state; /* for reads */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 281 | enum rbd_obj_write_state write_state; /* for writes */ |
| 282 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 283 | |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 284 | struct rbd_img_request *img_request; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 285 | struct ceph_file_extent *img_extents; |
| 286 | u32 num_img_extents; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 287 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 288 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 289 | struct ceph_bio_iter bio_pos; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 290 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 291 | struct ceph_bvec_iter bvec_pos; |
| 292 | u32 bvec_count; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 293 | u32 bvec_idx; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 294 | }; |
| 295 | }; |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 296 | |
| 297 | enum rbd_obj_copyup_state copyup_state; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 298 | struct bio_vec *copyup_bvecs; |
| 299 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 300 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 301 | struct list_head osd_reqs; /* w/ r_private_item */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 303 | struct mutex state_mutex; |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 304 | struct pending_result pending; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 305 | struct kref kref; |
| 306 | }; |
| 307 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 308 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 309 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 310 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 311 | }; |
| 312 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 313 | enum rbd_img_state { |
| 314 | RBD_IMG_START = 1, |
| 315 | __RBD_IMG_OBJECT_REQUESTS, |
| 316 | RBD_IMG_OBJECT_REQUESTS, |
| 317 | }; |
| 318 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 319 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 320 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 321 | enum obj_operation_type op_type; |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 322 | enum obj_request_type data_type; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 323 | unsigned long flags; |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 324 | enum rbd_img_state state; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 325 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 326 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 327 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 328 | }; |
| 329 | union { |
| 330 | struct request *rq; /* block request */ |
| 331 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 332 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 333 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 334 | struct list_head lock_item; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 335 | struct list_head object_extents; /* obj_req.ex structs */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 336 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 337 | struct mutex state_mutex; |
| 338 | struct pending_result pending; |
| 339 | struct work_struct work; |
| 340 | int work_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 341 | struct kref kref; |
| 342 | }; |
| 343 | |
| 344 | #define for_each_obj_request(ireq, oreq) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 345 | list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 346 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 347 | 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] | 348 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 349 | enum rbd_watch_state { |
| 350 | RBD_WATCH_STATE_UNREGISTERED, |
| 351 | RBD_WATCH_STATE_REGISTERED, |
| 352 | RBD_WATCH_STATE_ERROR, |
| 353 | }; |
| 354 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 355 | enum rbd_lock_state { |
| 356 | RBD_LOCK_STATE_UNLOCKED, |
| 357 | RBD_LOCK_STATE_LOCKED, |
| 358 | RBD_LOCK_STATE_RELEASING, |
| 359 | }; |
| 360 | |
| 361 | /* WatchNotify::ClientId */ |
| 362 | struct rbd_client_id { |
| 363 | u64 gid; |
| 364 | u64 handle; |
| 365 | }; |
| 366 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 367 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 368 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 369 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 370 | }; |
| 371 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 372 | /* |
| 373 | * a single device |
| 374 | */ |
| 375 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 376 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 377 | |
| 378 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 379 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 380 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 381 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 382 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 383 | struct rbd_client *rbd_client; |
| 384 | |
| 385 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 386 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 387 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 388 | |
| 389 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 390 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 391 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 392 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 393 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 394 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 395 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 396 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 397 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 398 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 399 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 400 | struct mutex watch_mutex; |
| 401 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 402 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 403 | u64 watch_cookie; |
| 404 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 405 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 406 | struct rw_semaphore lock_rwsem; |
| 407 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 408 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 409 | struct rbd_client_id owner_cid; |
| 410 | struct work_struct acquired_lock_work; |
| 411 | struct work_struct released_lock_work; |
| 412 | struct delayed_work lock_dwork; |
| 413 | struct work_struct unlock_work; |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 414 | spinlock_t lock_lists_lock; |
| 415 | struct list_head running_list; |
| 416 | struct completion releasing_wait; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 417 | wait_queue_head_t lock_waitq; |
| 418 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 419 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 420 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 421 | struct rbd_spec *parent_spec; |
| 422 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 423 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 424 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 425 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 426 | /* Block layer tags. */ |
| 427 | struct blk_mq_tag_set tag_set; |
| 428 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 429 | /* protects updating the header */ |
| 430 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 431 | |
| 432 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 433 | |
| 434 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 435 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 436 | /* sysfs related */ |
| 437 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 438 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 439 | }; |
| 440 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 441 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 442 | * Flag bits for rbd_dev->flags: |
| 443 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 444 | * by rbd_dev->lock |
| 445 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 446 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 447 | enum rbd_dev_flags { |
| 448 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 449 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 450 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 451 | }; |
| 452 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 453 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 454 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 455 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 456 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 457 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 458 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 459 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 460 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 461 | /* Slab caches for frequently-allocated structures */ |
| 462 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 463 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 464 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 465 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 466 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 467 | static DEFINE_IDA(rbd_dev_id_ida); |
| 468 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 469 | static struct workqueue_struct *rbd_wq; |
| 470 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 471 | static struct ceph_snap_context rbd_empty_snapc = { |
| 472 | .nref = REFCOUNT_INIT(1), |
| 473 | }; |
| 474 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 475 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 476 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 477 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 478 | static bool single_major = true; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 479 | module_param(single_major, bool, 0444); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 480 | 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] | 481 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 482 | static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count); |
| 483 | static ssize_t remove_store(struct bus_type *bus, const char *buf, |
| 484 | size_t count); |
| 485 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, |
| 486 | size_t count); |
| 487 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, |
| 488 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 489 | 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] | 490 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 491 | static int rbd_dev_id_to_minor(int dev_id) |
| 492 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 493 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static int minor_to_rbd_dev_id(int minor) |
| 497 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 498 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 501 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 502 | { |
| 503 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 504 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 505 | } |
| 506 | |
| 507 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 508 | { |
| 509 | bool is_lock_owner; |
| 510 | |
| 511 | down_read(&rbd_dev->lock_rwsem); |
| 512 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 513 | up_read(&rbd_dev->lock_rwsem); |
| 514 | return is_lock_owner; |
| 515 | } |
| 516 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 517 | static ssize_t supported_features_show(struct bus_type *bus, char *buf) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 518 | { |
| 519 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 520 | } |
| 521 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 522 | static BUS_ATTR_WO(add); |
| 523 | static BUS_ATTR_WO(remove); |
| 524 | static BUS_ATTR_WO(add_single_major); |
| 525 | static BUS_ATTR_WO(remove_single_major); |
| 526 | static BUS_ATTR_RO(supported_features); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 527 | |
| 528 | static struct attribute *rbd_bus_attrs[] = { |
| 529 | &bus_attr_add.attr, |
| 530 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 531 | &bus_attr_add_single_major.attr, |
| 532 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 533 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 534 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 535 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 536 | |
| 537 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 538 | struct attribute *attr, int index) |
| 539 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 540 | if (!single_major && |
| 541 | (attr == &bus_attr_add_single_major.attr || |
| 542 | attr == &bus_attr_remove_single_major.attr)) |
| 543 | return 0; |
| 544 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 545 | return attr->mode; |
| 546 | } |
| 547 | |
| 548 | static const struct attribute_group rbd_bus_group = { |
| 549 | .attrs = rbd_bus_attrs, |
| 550 | .is_visible = rbd_bus_is_visible, |
| 551 | }; |
| 552 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 553 | |
| 554 | static struct bus_type rbd_bus_type = { |
| 555 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 556 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | static void rbd_root_dev_release(struct device *dev) |
| 560 | { |
| 561 | } |
| 562 | |
| 563 | static struct device rbd_root_dev = { |
| 564 | .init_name = "rbd", |
| 565 | .release = rbd_root_dev_release, |
| 566 | }; |
| 567 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 568 | static __printf(2, 3) |
| 569 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 570 | { |
| 571 | struct va_format vaf; |
| 572 | va_list args; |
| 573 | |
| 574 | va_start(args, fmt); |
| 575 | vaf.fmt = fmt; |
| 576 | vaf.va = &args; |
| 577 | |
| 578 | if (!rbd_dev) |
| 579 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 580 | else if (rbd_dev->disk) |
| 581 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 582 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 583 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 584 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 585 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 586 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 587 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 588 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 589 | else /* punt */ |
| 590 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 591 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 592 | va_end(args); |
| 593 | } |
| 594 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 595 | #ifdef RBD_DEBUG |
| 596 | #define rbd_assert(expr) \ |
| 597 | if (unlikely(!(expr))) { \ |
| 598 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 599 | "at line %d:\n\n" \ |
| 600 | "\trbd_assert(%s);\n\n", \ |
| 601 | __func__, __LINE__, #expr); \ |
| 602 | BUG(); \ |
| 603 | } |
| 604 | #else /* !RBD_DEBUG */ |
| 605 | # define rbd_assert(expr) ((void) 0) |
| 606 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 607 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 608 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 609 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 610 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 611 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 612 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 613 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 614 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 615 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 616 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 617 | u8 *order, u64 *snap_size); |
| 618 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 619 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 620 | |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 621 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result); |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 622 | static void rbd_img_handle_request(struct rbd_img_request *img_req, int result); |
| 623 | |
| 624 | /* |
| 625 | * Return true if nothing else is pending. |
| 626 | */ |
| 627 | static bool pending_result_dec(struct pending_result *pending, int *result) |
| 628 | { |
| 629 | rbd_assert(pending->num_pending > 0); |
| 630 | |
| 631 | if (*result && !pending->result) |
| 632 | pending->result = *result; |
| 633 | if (--pending->num_pending) |
| 634 | return false; |
| 635 | |
| 636 | *result = pending->result; |
| 637 | return true; |
| 638 | } |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 639 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 640 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 641 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 642 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 643 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 644 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 645 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 646 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 647 | removing = true; |
| 648 | else |
| 649 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 650 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 651 | if (removing) |
| 652 | return -ENOENT; |
| 653 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 654 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 655 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 659 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 660 | { |
| 661 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 662 | unsigned long open_count_before; |
| 663 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 664 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 665 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 666 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 667 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 668 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 669 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 672 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 673 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 674 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 675 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 676 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 677 | return -EFAULT; |
| 678 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 679 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 680 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 681 | return -EROFS; |
| 682 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 683 | /* Let blkdev_roset() handle it */ |
| 684 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 688 | unsigned int cmd, unsigned long arg) |
| 689 | { |
| 690 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 691 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 692 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 693 | switch (cmd) { |
| 694 | case BLKROSET: |
| 695 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 696 | break; |
| 697 | default: |
| 698 | ret = -ENOTTY; |
| 699 | } |
| 700 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | #ifdef CONFIG_COMPAT |
| 705 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 706 | unsigned int cmd, unsigned long arg) |
| 707 | { |
| 708 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 709 | } |
| 710 | #endif /* CONFIG_COMPAT */ |
| 711 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 712 | static const struct block_device_operations rbd_bd_ops = { |
| 713 | .owner = THIS_MODULE, |
| 714 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 715 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 716 | .ioctl = rbd_ioctl, |
| 717 | #ifdef CONFIG_COMPAT |
| 718 | .compat_ioctl = rbd_compat_ioctl, |
| 719 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 720 | }; |
| 721 | |
| 722 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 723 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 724 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 725 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 726 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 727 | { |
| 728 | struct rbd_client *rbdc; |
| 729 | int ret = -ENOMEM; |
| 730 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 731 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 732 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 733 | if (!rbdc) |
| 734 | goto out_opt; |
| 735 | |
| 736 | kref_init(&rbdc->kref); |
| 737 | INIT_LIST_HEAD(&rbdc->node); |
| 738 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 739 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 740 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 741 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 742 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 743 | |
| 744 | ret = ceph_open_session(rbdc->client); |
| 745 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 746 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 747 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 748 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 749 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 750 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 751 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 752 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 753 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 754 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 755 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 756 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 757 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 758 | kfree(rbdc); |
| 759 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 760 | if (ceph_opts) |
| 761 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 762 | dout("%s: error %d\n", __func__, ret); |
| 763 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 764 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 767 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 768 | { |
| 769 | kref_get(&rbdc->kref); |
| 770 | |
| 771 | return rbdc; |
| 772 | } |
| 773 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 774 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 775 | * Find a ceph client with specific addr and configuration. If |
| 776 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 777 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 778 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 779 | { |
| 780 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 781 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 782 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 783 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 784 | return NULL; |
| 785 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 786 | spin_lock(&rbd_client_list_lock); |
| 787 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 788 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 789 | __rbd_get_client(client_node); |
| 790 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 791 | found = true; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | spin_unlock(&rbd_client_list_lock); |
| 796 | |
| 797 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 801 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 802 | */ |
| 803 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 804 | Opt_queue_depth, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 805 | Opt_alloc_size, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 806 | Opt_lock_timeout, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 807 | Opt_last_int, |
| 808 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 809 | Opt_pool_ns, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 810 | Opt_last_string, |
| 811 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 812 | Opt_read_only, |
| 813 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 814 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 815 | Opt_exclusive, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 816 | Opt_notrim, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 817 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 818 | }; |
| 819 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 820 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 821 | {Opt_queue_depth, "queue_depth=%d"}, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 822 | {Opt_alloc_size, "alloc_size=%d"}, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 823 | {Opt_lock_timeout, "lock_timeout=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 824 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 825 | {Opt_pool_ns, "_pool_ns=%s"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 826 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 827 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 828 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 829 | {Opt_read_write, "read_write"}, |
| 830 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 831 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 832 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 833 | {Opt_notrim, "notrim"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 834 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 835 | }; |
| 836 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 837 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 838 | int queue_depth; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 839 | int alloc_size; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 840 | unsigned long lock_timeout; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 841 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 842 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 843 | bool exclusive; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 844 | bool trim; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 845 | }; |
| 846 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 847 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 848 | #define RBD_ALLOC_SIZE_DEFAULT (64 * 1024) |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 849 | #define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 850 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 851 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 852 | #define RBD_EXCLUSIVE_DEFAULT false |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 853 | #define RBD_TRIM_DEFAULT true |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 854 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 855 | struct parse_rbd_opts_ctx { |
| 856 | struct rbd_spec *spec; |
| 857 | struct rbd_options *opts; |
| 858 | }; |
| 859 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 860 | static int parse_rbd_opts_token(char *c, void *private) |
| 861 | { |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 862 | struct parse_rbd_opts_ctx *pctx = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 863 | substring_t argstr[MAX_OPT_ARGS]; |
| 864 | int token, intval, ret; |
| 865 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 866 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 867 | if (token < Opt_last_int) { |
| 868 | ret = match_int(&argstr[0], &intval); |
| 869 | if (ret < 0) { |
Ilya Dryomov | 2f56b6b | 2018-06-27 16:38:13 +0200 | [diff] [blame] | 870 | pr_err("bad option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 871 | return ret; |
| 872 | } |
| 873 | dout("got int token %d val %d\n", token, intval); |
| 874 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 875 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 876 | } else { |
| 877 | dout("got token %d\n", token); |
| 878 | } |
| 879 | |
| 880 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 881 | case Opt_queue_depth: |
| 882 | if (intval < 1) { |
| 883 | pr_err("queue_depth out of range\n"); |
| 884 | return -EINVAL; |
| 885 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 886 | pctx->opts->queue_depth = intval; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 887 | break; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 888 | case Opt_alloc_size: |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 889 | if (intval < SECTOR_SIZE) { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 890 | pr_err("alloc_size out of range\n"); |
| 891 | return -EINVAL; |
| 892 | } |
| 893 | if (!is_power_of_2(intval)) { |
| 894 | pr_err("alloc_size must be a power of 2\n"); |
| 895 | return -EINVAL; |
| 896 | } |
| 897 | pctx->opts->alloc_size = intval; |
| 898 | break; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 899 | case Opt_lock_timeout: |
| 900 | /* 0 is "wait forever" (i.e. infinite timeout) */ |
| 901 | if (intval < 0 || intval > INT_MAX / 1000) { |
| 902 | pr_err("lock_timeout out of range\n"); |
| 903 | return -EINVAL; |
| 904 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 905 | pctx->opts->lock_timeout = msecs_to_jiffies(intval * 1000); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 906 | break; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 907 | case Opt_pool_ns: |
| 908 | kfree(pctx->spec->pool_ns); |
| 909 | pctx->spec->pool_ns = match_strdup(argstr); |
| 910 | if (!pctx->spec->pool_ns) |
| 911 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 912 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 913 | case Opt_read_only: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 914 | pctx->opts->read_only = true; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 915 | break; |
| 916 | case Opt_read_write: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 917 | pctx->opts->read_only = false; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 918 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 919 | case Opt_lock_on_read: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 920 | pctx->opts->lock_on_read = true; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 921 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 922 | case Opt_exclusive: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 923 | pctx->opts->exclusive = true; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 924 | break; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 925 | case Opt_notrim: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 926 | pctx->opts->trim = false; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 927 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 928 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 929 | /* libceph prints "bad option" msg */ |
| 930 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 931 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 932 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 936 | static char* obj_op_name(enum obj_operation_type op_type) |
| 937 | { |
| 938 | switch (op_type) { |
| 939 | case OBJ_OP_READ: |
| 940 | return "read"; |
| 941 | case OBJ_OP_WRITE: |
| 942 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 943 | case OBJ_OP_DISCARD: |
| 944 | return "discard"; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 945 | case OBJ_OP_ZEROOUT: |
| 946 | return "zeroout"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 947 | default: |
| 948 | return "???"; |
| 949 | } |
| 950 | } |
| 951 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 952 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 953 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 954 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 955 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 956 | */ |
| 957 | static void rbd_client_release(struct kref *kref) |
| 958 | { |
| 959 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 960 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 961 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 962 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 963 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 964 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 965 | |
| 966 | ceph_destroy_client(rbdc->client); |
| 967 | kfree(rbdc); |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 972 | * it. |
| 973 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 974 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 975 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 976 | if (rbdc) |
| 977 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 980 | /* |
| 981 | * Get a ceph client with specific addr and configuration, if one does |
| 982 | * not exist create it. Either way, ceph_opts is consumed by this |
| 983 | * function. |
| 984 | */ |
| 985 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
| 986 | { |
| 987 | struct rbd_client *rbdc; |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 988 | int ret; |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 989 | |
Ilya Dryomov | a32e414 | 2019-05-02 15:56:00 +0200 | [diff] [blame] | 990 | mutex_lock(&client_mutex); |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 991 | rbdc = rbd_client_find(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 992 | if (rbdc) { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 993 | ceph_destroy_options(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Using an existing client. Make sure ->pg_pools is up to |
| 997 | * date before we look up the pool id in do_rbd_add(). |
| 998 | */ |
Ilya Dryomov | 9d4a227 | 2019-03-20 10:58:05 +0100 | [diff] [blame] | 999 | ret = ceph_wait_for_latest_osdmap(rbdc->client, |
| 1000 | rbdc->client->options->mount_timeout); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 1001 | if (ret) { |
| 1002 | rbd_warn(NULL, "failed to get latest osdmap: %d", ret); |
| 1003 | rbd_put_client(rbdc); |
| 1004 | rbdc = ERR_PTR(ret); |
| 1005 | } |
| 1006 | } else { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 1007 | rbdc = rbd_client_create(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 1008 | } |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 1009 | mutex_unlock(&client_mutex); |
| 1010 | |
| 1011 | return rbdc; |
| 1012 | } |
| 1013 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 1014 | static bool rbd_image_format_valid(u32 image_format) |
| 1015 | { |
| 1016 | return image_format == 1 || image_format == 2; |
| 1017 | } |
| 1018 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1019 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 1020 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1021 | size_t size; |
| 1022 | u32 snap_count; |
| 1023 | |
| 1024 | /* The header has to start with the magic rbd header text */ |
| 1025 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 1026 | return false; |
| 1027 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1028 | /* The bio layer requires at least sector-sized I/O */ |
| 1029 | |
| 1030 | if (ondisk->options.order < SECTOR_SHIFT) |
| 1031 | return false; |
| 1032 | |
| 1033 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 1034 | |
| 1035 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 1036 | return false; |
| 1037 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1038 | /* |
| 1039 | * The size of a snapshot header has to fit in a size_t, and |
| 1040 | * that limits the number of snapshots. |
| 1041 | */ |
| 1042 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 1043 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 1044 | if (snap_count > size / sizeof (__le64)) |
| 1045 | return false; |
| 1046 | |
| 1047 | /* |
| 1048 | * Not only that, but the size of the entire the snapshot |
| 1049 | * header must also be representable in a size_t. |
| 1050 | */ |
| 1051 | size -= snap_count * sizeof (__le64); |
| 1052 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 1053 | return false; |
| 1054 | |
| 1055 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1056 | } |
| 1057 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1058 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1059 | * returns the size of an object in the image |
| 1060 | */ |
| 1061 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 1062 | { |
| 1063 | return 1U << header->obj_order; |
| 1064 | } |
| 1065 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1066 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 1067 | { |
| 1068 | if (rbd_dev->header.stripe_unit == 0 || |
| 1069 | rbd_dev->header.stripe_count == 0) { |
| 1070 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 1071 | rbd_dev->header.stripe_count = 1; |
| 1072 | } |
| 1073 | |
| 1074 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 1075 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 1076 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1077 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 1078 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1079 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 1080 | } |
| 1081 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1082 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1083 | * Fill an rbd image header with information from the given format 1 |
| 1084 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1085 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1086 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1087 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1088 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1089 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1090 | bool first_time = header->object_prefix == NULL; |
| 1091 | struct ceph_snap_context *snapc; |
| 1092 | char *object_prefix = NULL; |
| 1093 | char *snap_names = NULL; |
| 1094 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1095 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1096 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1097 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1098 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1099 | /* Allocate this now to avoid having to handle failure below */ |
| 1100 | |
| 1101 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1102 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1103 | sizeof(ondisk->object_prefix), |
| 1104 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1105 | if (!object_prefix) |
| 1106 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1110 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1111 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1112 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1113 | if (!snapc) |
| 1114 | goto out_err; |
| 1115 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1116 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1117 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1118 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1119 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1120 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1121 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1122 | if (snap_names_len > (u64)SIZE_MAX) |
| 1123 | goto out_2big; |
| 1124 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1125 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1126 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1127 | |
| 1128 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1129 | snap_sizes = kmalloc_array(snap_count, |
| 1130 | sizeof(*header->snap_sizes), |
| 1131 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1132 | if (!snap_sizes) |
| 1133 | goto out_err; |
| 1134 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1135 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1136 | * Copy the names, and fill in each snapshot's id |
| 1137 | * and size. |
| 1138 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1139 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1140 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1141 | * snap_names_len bytes beyond the end of the |
| 1142 | * snapshot id array, this memcpy() is safe. |
| 1143 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1144 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1145 | snaps = ondisk->snaps; |
| 1146 | for (i = 0; i < snap_count; i++) { |
| 1147 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1148 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1149 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1150 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1151 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1152 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1153 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1154 | if (first_time) { |
| 1155 | header->object_prefix = object_prefix; |
| 1156 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1157 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1158 | } else { |
| 1159 | ceph_put_snap_context(header->snapc); |
| 1160 | kfree(header->snap_names); |
| 1161 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1165 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1166 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1167 | header->snapc = snapc; |
| 1168 | header->snap_names = snap_names; |
| 1169 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1170 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1171 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1172 | out_2big: |
| 1173 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1174 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1175 | kfree(snap_sizes); |
| 1176 | kfree(snap_names); |
| 1177 | ceph_put_snap_context(snapc); |
| 1178 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1179 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1180 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1183 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1184 | { |
| 1185 | const char *snap_name; |
| 1186 | |
| 1187 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1188 | |
| 1189 | /* Skip over names until we find the one we are looking for */ |
| 1190 | |
| 1191 | snap_name = rbd_dev->header.snap_names; |
| 1192 | while (which--) |
| 1193 | snap_name += strlen(snap_name) + 1; |
| 1194 | |
| 1195 | return kstrdup(snap_name, GFP_KERNEL); |
| 1196 | } |
| 1197 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1198 | /* |
| 1199 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1200 | * Note that result is for snapshots in *descending* order. |
| 1201 | */ |
| 1202 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1203 | { |
| 1204 | u64 snap_id1 = *(u64 *)s1; |
| 1205 | u64 snap_id2 = *(u64 *)s2; |
| 1206 | |
| 1207 | if (snap_id1 < snap_id2) |
| 1208 | return 1; |
| 1209 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Search a snapshot context to see if the given snapshot id is |
| 1214 | * present. |
| 1215 | * |
| 1216 | * Returns the position of the snapshot id in the array if it's found, |
| 1217 | * or BAD_SNAP_INDEX otherwise. |
| 1218 | * |
| 1219 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1220 | * reverse order, highest snapshot id first. |
| 1221 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1222 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1223 | { |
| 1224 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1225 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1226 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1227 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1228 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1229 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1230 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1231 | } |
| 1232 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1233 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1234 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1235 | { |
| 1236 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1237 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1238 | |
| 1239 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1240 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1241 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1242 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1243 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1244 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1247 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1248 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1249 | if (snap_id == CEPH_NOSNAP) |
| 1250 | return RBD_SNAP_HEAD_NAME; |
| 1251 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1252 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1253 | if (rbd_dev->image_format == 1) |
| 1254 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1255 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1256 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1257 | } |
| 1258 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1259 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1260 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1261 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1262 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1263 | if (snap_id == CEPH_NOSNAP) { |
| 1264 | *snap_size = rbd_dev->header.image_size; |
| 1265 | } else if (rbd_dev->image_format == 1) { |
| 1266 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1267 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1268 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1269 | if (which == BAD_SNAP_INDEX) |
| 1270 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1271 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1272 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1273 | } else { |
| 1274 | u64 size = 0; |
| 1275 | int ret; |
| 1276 | |
| 1277 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1278 | if (ret) |
| 1279 | return ret; |
| 1280 | |
| 1281 | *snap_size = size; |
| 1282 | } |
| 1283 | return 0; |
| 1284 | } |
| 1285 | |
| 1286 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1287 | u64 *snap_features) |
| 1288 | { |
| 1289 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1290 | if (snap_id == CEPH_NOSNAP) { |
| 1291 | *snap_features = rbd_dev->header.features; |
| 1292 | } else if (rbd_dev->image_format == 1) { |
| 1293 | *snap_features = 0; /* No features for format 1 */ |
| 1294 | } else { |
| 1295 | u64 features = 0; |
| 1296 | int ret; |
| 1297 | |
| 1298 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1299 | if (ret) |
| 1300 | return ret; |
| 1301 | |
| 1302 | *snap_features = features; |
| 1303 | } |
| 1304 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1307 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1308 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1309 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1310 | u64 size = 0; |
| 1311 | u64 features = 0; |
| 1312 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1313 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1314 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1315 | if (ret) |
| 1316 | return ret; |
| 1317 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1318 | if (ret) |
| 1319 | return ret; |
| 1320 | |
| 1321 | rbd_dev->mapping.size = size; |
| 1322 | rbd_dev->mapping.features = features; |
| 1323 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1324 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1327 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1328 | { |
| 1329 | rbd_dev->mapping.size = 0; |
| 1330 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1331 | } |
| 1332 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1333 | static void zero_bvec(struct bio_vec *bv) |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1334 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1335 | void *buf; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1336 | unsigned long flags; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1337 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1338 | buf = bvec_kmap_irq(bv, &flags); |
| 1339 | memset(buf, 0, bv->bv_len); |
| 1340 | flush_dcache_page(bv->bv_page); |
| 1341 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1344 | 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] | 1345 | { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1346 | struct ceph_bio_iter it = *bio_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1347 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1348 | ceph_bio_iter_advance(&it, off); |
| 1349 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1350 | zero_bvec(&bv); |
| 1351 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1352 | } |
| 1353 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1354 | 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] | 1355 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1356 | struct ceph_bvec_iter it = *bvec_pos; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1357 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1358 | ceph_bvec_iter_advance(&it, off); |
| 1359 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1360 | zero_bvec(&bv); |
| 1361 | })); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1362 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1363 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1364 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1365 | * 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] | 1366 | * (private) bio_vec array. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1367 | * |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1368 | * @off is relative to the start of the data buffer. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1369 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1370 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1371 | u32 bytes) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1372 | { |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 1373 | dout("%s %p data buf %u~%u\n", __func__, obj_req, off, bytes); |
| 1374 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1375 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1376 | case OBJ_REQUEST_BIO: |
| 1377 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1378 | break; |
| 1379 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1380 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1381 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1382 | break; |
| 1383 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame] | 1384 | BUG(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1385 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1389 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1390 | { |
| 1391 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1392 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1393 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1394 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1395 | } |
| 1396 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1397 | static void rbd_img_request_destroy(struct kref *kref); |
| 1398 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1399 | { |
| 1400 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1401 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1402 | kref_read(&img_request->kref)); |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1403 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1407 | struct rbd_obj_request *obj_request) |
| 1408 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1409 | rbd_assert(obj_request->img_request == NULL); |
| 1410 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1411 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1412 | obj_request->img_request = img_request; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1413 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1417 | struct rbd_obj_request *obj_request) |
| 1418 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1419 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1420 | list_del(&obj_request->ex.oe_item); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1421 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1422 | rbd_obj_request_put(obj_request); |
| 1423 | } |
| 1424 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 1425 | static void rbd_osd_submit(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1426 | { |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 1427 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1428 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 1429 | dout("%s osd_req %p for obj_req %p objno %llu %llu~%llu\n", |
| 1430 | __func__, osd_req, obj_req, obj_req->ex.oe_objno, |
| 1431 | obj_req->ex.oe_off, obj_req->ex.oe_len); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1432 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1433 | } |
| 1434 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1435 | /* |
| 1436 | * The default/initial value for all image request flags is 0. Each |
| 1437 | * is conditionally set to 1 at image request initialization time |
| 1438 | * and currently never change thereafter. |
| 1439 | */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1440 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1441 | { |
| 1442 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1443 | smp_mb(); |
| 1444 | } |
| 1445 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1446 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1447 | { |
| 1448 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1449 | smp_mb(); |
| 1450 | } |
| 1451 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1452 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1453 | { |
| 1454 | smp_mb(); |
| 1455 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1456 | } |
| 1457 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1458 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1459 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1460 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1461 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1462 | return !obj_req->ex.oe_off && |
| 1463 | obj_req->ex.oe_len == rbd_dev->layout.object_size; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1466 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1467 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1468 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1469 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1470 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1471 | rbd_dev->layout.object_size; |
| 1472 | } |
| 1473 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1474 | /* |
| 1475 | * Must be called after rbd_obj_calc_img_extents(). |
| 1476 | */ |
| 1477 | static bool rbd_obj_copyup_enabled(struct rbd_obj_request *obj_req) |
| 1478 | { |
| 1479 | if (!obj_req->num_img_extents || |
Ilya Dryomov | 9b17eb2 | 2019-02-28 15:51:39 +0100 | [diff] [blame] | 1480 | (rbd_obj_is_entire(obj_req) && |
| 1481 | !obj_req->img_request->snapc->num_snaps)) |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1482 | return false; |
| 1483 | |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1487 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
| 1488 | { |
| 1489 | return ceph_file_extents_bytes(obj_req->img_extents, |
| 1490 | obj_req->num_img_extents); |
| 1491 | } |
| 1492 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1493 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1494 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1495 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1496 | case OBJ_OP_READ: |
| 1497 | return false; |
| 1498 | case OBJ_OP_WRITE: |
| 1499 | case OBJ_OP_DISCARD: |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1500 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1501 | return true; |
| 1502 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 1503 | BUG(); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1504 | } |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1505 | } |
| 1506 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1507 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1508 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1509 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 1510 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1511 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1512 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1513 | osd_req->r_result, obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1514 | |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 1515 | /* |
| 1516 | * Writes aren't allowed to return a data payload. In some |
| 1517 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1518 | * a stat response makes it through, but we don't care. |
| 1519 | */ |
| 1520 | if (osd_req->r_result > 0 && rbd_img_is_write(obj_req->img_request)) |
| 1521 | result = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1522 | else |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 1523 | result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1524 | |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 1525 | rbd_obj_handle_request(obj_req, result); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1526 | } |
| 1527 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1528 | static void rbd_osd_format_read(struct ceph_osd_request *osd_req) |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1529 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1530 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1531 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1532 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1533 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1534 | } |
| 1535 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1536 | static void rbd_osd_format_write(struct ceph_osd_request *osd_req) |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1537 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1538 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1539 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1540 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Arnd Bergmann | fac02dd | 2018-07-13 22:18:37 +0200 | [diff] [blame] | 1541 | ktime_get_real_ts64(&osd_req->r_mtime); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1542 | osd_req->r_data_offset = obj_request->ex.oe_off; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1543 | } |
| 1544 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1545 | static struct ceph_osd_request * |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1546 | __rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, |
| 1547 | struct ceph_snap_context *snapc, int num_ops) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1548 | { |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1549 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1550 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1551 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1552 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1553 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1554 | int ret; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1555 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1556 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1557 | if (!req) |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1558 | return ERR_PTR(-ENOMEM); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1559 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1560 | list_add_tail(&req->r_private_item, &obj_req->osd_reqs); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1561 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1562 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1563 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1564 | /* |
| 1565 | * Data objects may be stored in a separate pool, but always in |
| 1566 | * the same namespace in that pool as the header in its pool. |
| 1567 | */ |
| 1568 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1569 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1570 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1571 | ret = ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
| 1572 | rbd_dev->header.object_prefix, |
| 1573 | obj_req->ex.oe_objno); |
| 1574 | if (ret) |
| 1575 | return ERR_PTR(ret); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1576 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1577 | return req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1578 | } |
| 1579 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1580 | static struct ceph_osd_request * |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1581 | rbd_obj_add_osd_request(struct rbd_obj_request *obj_req, int num_ops) |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1582 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1583 | return __rbd_obj_add_osd_request(obj_req, obj_req->img_request->snapc, |
| 1584 | num_ops); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1585 | } |
| 1586 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1587 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1588 | { |
| 1589 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1590 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1591 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1592 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1593 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1594 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1595 | ceph_object_extent_init(&obj_request->ex); |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1596 | INIT_LIST_HEAD(&obj_request->osd_reqs); |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 1597 | mutex_init(&obj_request->state_mutex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1598 | kref_init(&obj_request->kref); |
| 1599 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1600 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1601 | return obj_request; |
| 1602 | } |
| 1603 | |
| 1604 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1605 | { |
| 1606 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1607 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1608 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1609 | |
| 1610 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1611 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1612 | dout("%s: obj %p\n", __func__, obj_request); |
| 1613 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1614 | while (!list_empty(&obj_request->osd_reqs)) { |
| 1615 | osd_req = list_first_entry(&obj_request->osd_reqs, |
| 1616 | struct ceph_osd_request, r_private_item); |
| 1617 | list_del_init(&osd_req->r_private_item); |
| 1618 | ceph_osdc_put_request(osd_req); |
| 1619 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1620 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1621 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1622 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1623 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1624 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1625 | break; /* Nothing to do */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1626 | case OBJ_REQUEST_OWN_BVECS: |
| 1627 | kfree(obj_request->bvec_pos.bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1628 | break; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1629 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame] | 1630 | BUG(); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1631 | } |
| 1632 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1633 | kfree(obj_request->img_extents); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1634 | if (obj_request->copyup_bvecs) { |
| 1635 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1636 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1637 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1638 | } |
| 1639 | kfree(obj_request->copyup_bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1640 | } |
| 1641 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1642 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1643 | } |
| 1644 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1645 | /* It's OK to call this for a device with no parent */ |
| 1646 | |
| 1647 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1648 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1649 | { |
| 1650 | rbd_dev_remove_parent(rbd_dev); |
| 1651 | rbd_spec_put(rbd_dev->parent_spec); |
| 1652 | rbd_dev->parent_spec = NULL; |
| 1653 | rbd_dev->parent_overlap = 0; |
| 1654 | } |
| 1655 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1656 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1657 | * Parent image reference counting is used to determine when an |
| 1658 | * image's parent fields can be safely torn down--after there are no |
| 1659 | * more in-flight requests to the parent image. When the last |
| 1660 | * reference is dropped, cleaning them up is safe. |
| 1661 | */ |
| 1662 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1663 | { |
| 1664 | int counter; |
| 1665 | |
| 1666 | if (!rbd_dev->parent_spec) |
| 1667 | return; |
| 1668 | |
| 1669 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1670 | if (counter > 0) |
| 1671 | return; |
| 1672 | |
| 1673 | /* Last reference; clean up parent data structures */ |
| 1674 | |
| 1675 | if (!counter) |
| 1676 | rbd_dev_unparent(rbd_dev); |
| 1677 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1678 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* |
| 1682 | * If an image has a non-zero parent overlap, get a reference to its |
| 1683 | * parent. |
| 1684 | * |
| 1685 | * Returns true if the rbd device has a parent with a non-zero |
| 1686 | * overlap and a reference for it was successfully taken, or |
| 1687 | * false otherwise. |
| 1688 | */ |
| 1689 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1690 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1691 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1692 | |
| 1693 | if (!rbd_dev->parent_spec) |
| 1694 | return false; |
| 1695 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1696 | down_read(&rbd_dev->header_rwsem); |
| 1697 | if (rbd_dev->parent_overlap) |
| 1698 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1699 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1700 | |
| 1701 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1702 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1703 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1704 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1705 | } |
| 1706 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1707 | /* |
| 1708 | * Caller is responsible for filling in the list of object requests |
| 1709 | * that comprises the image request, and the Linux request pointer |
| 1710 | * (if there is one). |
| 1711 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1712 | static struct rbd_img_request *rbd_img_request_create( |
| 1713 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1714 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1715 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1716 | { |
| 1717 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1718 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1719 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1720 | if (!img_request) |
| 1721 | return NULL; |
| 1722 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1723 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1724 | img_request->op_type = op_type; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1725 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1726 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1727 | else |
| 1728 | img_request->snapc = snapc; |
| 1729 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1730 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1731 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1732 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 1733 | INIT_LIST_HEAD(&img_request->lock_item); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1734 | INIT_LIST_HEAD(&img_request->object_extents); |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 1735 | mutex_init(&img_request->state_mutex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1736 | kref_init(&img_request->kref); |
| 1737 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1738 | dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, |
| 1739 | obj_op_name(op_type), img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1740 | return img_request; |
| 1741 | } |
| 1742 | |
| 1743 | static void rbd_img_request_destroy(struct kref *kref) |
| 1744 | { |
| 1745 | struct rbd_img_request *img_request; |
| 1746 | struct rbd_obj_request *obj_request; |
| 1747 | struct rbd_obj_request *next_obj_request; |
| 1748 | |
| 1749 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1750 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1751 | dout("%s: img %p\n", __func__, img_request); |
| 1752 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 1753 | WARN_ON(!list_empty(&img_request->lock_item)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1754 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1755 | rbd_img_obj_request_del(img_request, obj_request); |
| 1756 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1757 | if (img_request_layered_test(img_request)) { |
| 1758 | img_request_layered_clear(img_request); |
| 1759 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1760 | } |
| 1761 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1762 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1763 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1764 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1765 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1766 | } |
| 1767 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1768 | static void prune_extents(struct ceph_file_extent *img_extents, |
| 1769 | u32 *num_img_extents, u64 overlap) |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1770 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1771 | u32 cnt = *num_img_extents; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1772 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1773 | /* drop extents completely beyond the overlap */ |
| 1774 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) |
| 1775 | cnt--; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1776 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1777 | if (cnt) { |
| 1778 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1779 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1780 | /* trim final overlapping extent */ |
| 1781 | if (ex->fe_off + ex->fe_len > overlap) |
| 1782 | ex->fe_len = overlap - ex->fe_off; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1783 | } |
| 1784 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1785 | *num_img_extents = cnt; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1786 | } |
| 1787 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1788 | /* |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1789 | * Determine the byte range(s) covered by either just the object extent |
| 1790 | * or the entire object in the parent image. |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1791 | */ |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1792 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, |
| 1793 | bool entire) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1794 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1795 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1796 | int ret; |
| 1797 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1798 | if (!rbd_dev->parent_overlap) |
| 1799 | return 0; |
| 1800 | |
| 1801 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
| 1802 | entire ? 0 : obj_req->ex.oe_off, |
| 1803 | entire ? rbd_dev->layout.object_size : |
| 1804 | obj_req->ex.oe_len, |
| 1805 | &obj_req->img_extents, |
| 1806 | &obj_req->num_img_extents); |
| 1807 | if (ret) |
| 1808 | return ret; |
| 1809 | |
| 1810 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 1811 | rbd_dev->parent_overlap); |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1815 | static void rbd_osd_setup_data(struct ceph_osd_request *osd_req, int which) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1816 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1817 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
| 1818 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1819 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1820 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1821 | osd_req_op_extent_osd_data_bio(osd_req, which, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1822 | &obj_req->bio_pos, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1823 | obj_req->ex.oe_len); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1824 | break; |
| 1825 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1826 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1827 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1828 | obj_req->ex.oe_len); |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1829 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1830 | osd_req_op_extent_osd_data_bvec_pos(osd_req, which, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1831 | &obj_req->bvec_pos); |
| 1832 | break; |
| 1833 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame] | 1834 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1838 | static int rbd_osd_setup_stat(struct ceph_osd_request *osd_req, int which) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1839 | { |
| 1840 | struct page **pages; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1841 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1842 | /* |
| 1843 | * The response data for a STAT call consists of: |
| 1844 | * le64 length; |
| 1845 | * struct { |
| 1846 | * le32 tv_sec; |
| 1847 | * le32 tv_nsec; |
| 1848 | * } mtime; |
| 1849 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1850 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1851 | if (IS_ERR(pages)) |
| 1852 | return PTR_ERR(pages); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1853 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1854 | osd_req_op_init(osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1855 | osd_req_op_raw_data_in_pages(osd_req, which, pages, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1856 | 8 + sizeof(struct ceph_timespec), |
| 1857 | 0, false, true); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1858 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1859 | } |
| 1860 | |
Ilya Dryomov | b5ae8cb | 2019-05-29 16:53:14 +0200 | [diff] [blame] | 1861 | static int rbd_osd_setup_copyup(struct ceph_osd_request *osd_req, int which, |
| 1862 | u32 bytes) |
| 1863 | { |
| 1864 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
| 1865 | int ret; |
| 1866 | |
| 1867 | ret = osd_req_op_cls_init(osd_req, which, "rbd", "copyup"); |
| 1868 | if (ret) |
| 1869 | return ret; |
| 1870 | |
| 1871 | osd_req_op_cls_request_data_bvecs(osd_req, which, obj_req->copyup_bvecs, |
| 1872 | obj_req->copyup_bvec_count, bytes); |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 1876 | static int rbd_obj_init_read(struct rbd_obj_request *obj_req) |
| 1877 | { |
| 1878 | obj_req->read_state = RBD_OBJ_READ_START; |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1882 | static void __rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req, |
| 1883 | int which) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1884 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1885 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1886 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1887 | u16 opcode; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1888 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1889 | osd_req_op_alloc_hint_init(osd_req, which++, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1890 | rbd_dev->layout.object_size, |
| 1891 | rbd_dev->layout.object_size); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1892 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1893 | if (rbd_obj_is_entire(obj_req)) |
| 1894 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1895 | else |
| 1896 | opcode = CEPH_OSD_OP_WRITE; |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1897 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1898 | osd_req_op_extent_init(osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1899 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1900 | rbd_osd_setup_data(osd_req, which); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1901 | } |
| 1902 | |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 1903 | static int rbd_obj_init_write(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1904 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1905 | int ret; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 1906 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1907 | /* reverse map the entire object onto the parent */ |
| 1908 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1909 | if (ret) |
| 1910 | return ret; |
| 1911 | |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 1912 | if (rbd_obj_copyup_enabled(obj_req)) |
| 1913 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED; |
| 1914 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 1915 | obj_req->write_state = RBD_OBJ_WRITE_START; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1916 | return 0; |
| 1917 | } |
| 1918 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1919 | static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req) |
| 1920 | { |
| 1921 | return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE : |
| 1922 | CEPH_OSD_OP_ZERO; |
| 1923 | } |
| 1924 | |
Ilya Dryomov | 27bbd91 | 2019-05-29 17:31:37 +0200 | [diff] [blame] | 1925 | static void __rbd_osd_setup_discard_ops(struct ceph_osd_request *osd_req, |
| 1926 | int which) |
| 1927 | { |
| 1928 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
| 1929 | |
| 1930 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) { |
| 1931 | rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION); |
| 1932 | osd_req_op_init(osd_req, which, CEPH_OSD_OP_DELETE, 0); |
| 1933 | } else { |
| 1934 | osd_req_op_extent_init(osd_req, which, |
| 1935 | truncate_or_zero_opcode(obj_req), |
| 1936 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
| 1937 | 0, 0); |
| 1938 | } |
| 1939 | } |
| 1940 | |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 1941 | static int rbd_obj_init_discard(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1942 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1943 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 27bbd91 | 2019-05-29 17:31:37 +0200 | [diff] [blame] | 1944 | u64 off, next_off; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1945 | int ret; |
| 1946 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1947 | /* |
| 1948 | * Align the range to alloc_size boundary and punt on discards |
| 1949 | * that are too small to free up any space. |
| 1950 | * |
| 1951 | * alloc_size == object_size && is_tail() is a special case for |
| 1952 | * filestore with filestore_punch_hole = false, needed to allow |
| 1953 | * truncate (in addition to delete). |
| 1954 | */ |
| 1955 | if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size || |
| 1956 | !rbd_obj_is_tail(obj_req)) { |
Ilya Dryomov | 27bbd91 | 2019-05-29 17:31:37 +0200 | [diff] [blame] | 1957 | off = round_up(obj_req->ex.oe_off, rbd_dev->opts->alloc_size); |
| 1958 | next_off = round_down(obj_req->ex.oe_off + obj_req->ex.oe_len, |
| 1959 | rbd_dev->opts->alloc_size); |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1960 | if (off >= next_off) |
| 1961 | return 1; |
Ilya Dryomov | 27bbd91 | 2019-05-29 17:31:37 +0200 | [diff] [blame] | 1962 | |
| 1963 | dout("%s %p %llu~%llu -> %llu~%llu\n", __func__, |
| 1964 | obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len, |
| 1965 | off, next_off - off); |
| 1966 | obj_req->ex.oe_off = off; |
| 1967 | obj_req->ex.oe_len = next_off - off; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1970 | /* reverse map the entire object onto the parent */ |
| 1971 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1972 | if (ret) |
| 1973 | return ret; |
| 1974 | |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 1975 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) |
| 1976 | obj_req->flags |= RBD_OBJ_FLAG_DELETION; |
| 1977 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 1978 | obj_req->write_state = RBD_OBJ_WRITE_START; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1979 | return 0; |
| 1980 | } |
| 1981 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1982 | static void __rbd_osd_setup_zeroout_ops(struct ceph_osd_request *osd_req, |
| 1983 | int which) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1984 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1985 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1986 | u16 opcode; |
| 1987 | |
| 1988 | if (rbd_obj_is_entire(obj_req)) { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1989 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 1990 | if (!(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)) |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1991 | osd_req_op_init(osd_req, which++, |
Ilya Dryomov | 9b17eb2 | 2019-02-28 15:51:39 +0100 | [diff] [blame] | 1992 | CEPH_OSD_OP_CREATE, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1993 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1994 | } else { |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 1995 | rbd_assert(obj_req->flags & RBD_OBJ_FLAG_DELETION); |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 1996 | osd_req_op_init(osd_req, which++, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1997 | CEPH_OSD_OP_DELETE, 0); |
| 1998 | opcode = 0; |
| 1999 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2000 | } else { |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2001 | opcode = truncate_or_zero_opcode(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2002 | } |
| 2003 | |
| 2004 | if (opcode) |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2005 | osd_req_op_extent_init(osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2006 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2007 | 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2008 | } |
| 2009 | |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 2010 | static int rbd_obj_init_zeroout(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2011 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2012 | int ret; |
| 2013 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2014 | /* reverse map the entire object onto the parent */ |
| 2015 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 2016 | if (ret) |
| 2017 | return ret; |
| 2018 | |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2019 | if (rbd_obj_copyup_enabled(obj_req)) |
| 2020 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ENABLED; |
| 2021 | if (!obj_req->num_img_extents) { |
| 2022 | if (rbd_obj_is_entire(obj_req)) |
| 2023 | obj_req->flags |= RBD_OBJ_FLAG_DELETION; |
| 2024 | } |
| 2025 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2026 | obj_req->write_state = RBD_OBJ_WRITE_START; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2027 | return 0; |
| 2028 | } |
| 2029 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2030 | static int count_write_ops(struct rbd_obj_request *obj_req) |
| 2031 | { |
| 2032 | switch (obj_req->img_request->op_type) { |
| 2033 | case OBJ_OP_WRITE: |
| 2034 | return 2; /* setallochint + write/writefull */ |
| 2035 | case OBJ_OP_DISCARD: |
| 2036 | return 1; /* delete/truncate/zero */ |
| 2037 | case OBJ_OP_ZEROOUT: |
| 2038 | if (rbd_obj_is_entire(obj_req) && obj_req->num_img_extents && |
| 2039 | !(obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED)) |
| 2040 | return 2; /* create + truncate */ |
| 2041 | |
| 2042 | return 1; /* delete/truncate/zero */ |
| 2043 | default: |
| 2044 | BUG(); |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | static void rbd_osd_setup_write_ops(struct ceph_osd_request *osd_req, |
| 2049 | int which) |
| 2050 | { |
| 2051 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
| 2052 | |
| 2053 | switch (obj_req->img_request->op_type) { |
| 2054 | case OBJ_OP_WRITE: |
| 2055 | __rbd_osd_setup_write_ops(osd_req, which); |
| 2056 | break; |
| 2057 | case OBJ_OP_DISCARD: |
| 2058 | __rbd_osd_setup_discard_ops(osd_req, which); |
| 2059 | break; |
| 2060 | case OBJ_OP_ZEROOUT: |
| 2061 | __rbd_osd_setup_zeroout_ops(osd_req, which); |
| 2062 | break; |
| 2063 | default: |
| 2064 | BUG(); |
| 2065 | } |
| 2066 | } |
| 2067 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2068 | /* |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2069 | * Prune the list of object requests (adjust offset and/or length, drop |
| 2070 | * redundant requests). Prepare object request state machines and image |
| 2071 | * request state machine for execution. |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2072 | */ |
| 2073 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 2074 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2075 | struct rbd_obj_request *obj_req, *next_obj_req; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2076 | int ret; |
| 2077 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2078 | for_each_obj_request_safe(img_req, obj_req, next_obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2079 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2080 | case OBJ_OP_READ: |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 2081 | ret = rbd_obj_init_read(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2082 | break; |
| 2083 | case OBJ_OP_WRITE: |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 2084 | ret = rbd_obj_init_write(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2085 | break; |
| 2086 | case OBJ_OP_DISCARD: |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 2087 | ret = rbd_obj_init_discard(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2088 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2089 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | ea9b743 | 2019-05-31 15:11:26 +0200 | [diff] [blame] | 2090 | ret = rbd_obj_init_zeroout(obj_req); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2091 | break; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2092 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame] | 2093 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2094 | } |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2095 | if (ret < 0) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2096 | return ret; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2097 | if (ret > 0) { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2098 | rbd_img_obj_request_del(img_req, obj_req); |
| 2099 | continue; |
| 2100 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2101 | } |
| 2102 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2103 | img_req->state = RBD_IMG_START; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2107 | union rbd_img_fill_iter { |
| 2108 | struct ceph_bio_iter bio_iter; |
| 2109 | struct ceph_bvec_iter bvec_iter; |
| 2110 | }; |
| 2111 | |
| 2112 | struct rbd_img_fill_ctx { |
| 2113 | enum obj_request_type pos_type; |
| 2114 | union rbd_img_fill_iter *pos; |
| 2115 | union rbd_img_fill_iter iter; |
| 2116 | ceph_object_extent_fn_t set_pos_fn; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2117 | ceph_object_extent_fn_t count_fn; |
| 2118 | ceph_object_extent_fn_t copy_fn; |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2119 | }; |
| 2120 | |
| 2121 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
| 2122 | { |
| 2123 | struct rbd_img_request *img_req = arg; |
| 2124 | struct rbd_obj_request *obj_req; |
| 2125 | |
| 2126 | obj_req = rbd_obj_request_create(); |
| 2127 | if (!obj_req) |
| 2128 | return NULL; |
| 2129 | |
| 2130 | rbd_img_obj_request_add(img_req, obj_req); |
| 2131 | return &obj_req->ex; |
| 2132 | } |
| 2133 | |
| 2134 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2135 | * While su != os && sc == 1 is technically not fancy (it's the same |
| 2136 | * layout as su == os && sc == 1), we can't use the nocopy path for it |
| 2137 | * because ->set_pos_fn() should be called only once per object. |
| 2138 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so |
| 2139 | * treat su != os && sc == 1 as fancy. |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2140 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2141 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) |
| 2142 | { |
| 2143 | return l->stripe_unit != l->object_size; |
| 2144 | } |
| 2145 | |
| 2146 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
| 2147 | struct ceph_file_extent *img_extents, |
| 2148 | u32 num_img_extents, |
| 2149 | struct rbd_img_fill_ctx *fctx) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2150 | { |
| 2151 | u32 i; |
| 2152 | int ret; |
| 2153 | |
| 2154 | img_req->data_type = fctx->pos_type; |
| 2155 | |
| 2156 | /* |
| 2157 | * Create object requests and set each object request's starting |
| 2158 | * position in the provided bio (list) or bio_vec array. |
| 2159 | */ |
| 2160 | fctx->iter = *fctx->pos; |
| 2161 | for (i = 0; i < num_img_extents; i++) { |
| 2162 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, |
| 2163 | img_extents[i].fe_off, |
| 2164 | img_extents[i].fe_len, |
| 2165 | &img_req->object_extents, |
| 2166 | alloc_object_extent, img_req, |
| 2167 | fctx->set_pos_fn, &fctx->iter); |
| 2168 | if (ret) |
| 2169 | return ret; |
| 2170 | } |
| 2171 | |
| 2172 | return __rbd_img_fill_request(img_req); |
| 2173 | } |
| 2174 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2175 | /* |
| 2176 | * Map a list of image extents to a list of object extents, create the |
| 2177 | * corresponding object requests (normally each to a different object, |
| 2178 | * but not always) and add them to @img_req. For each object request, |
| 2179 | * set up its data descriptor to point to the corresponding chunk(s) of |
| 2180 | * @fctx->pos data buffer. |
| 2181 | * |
| 2182 | * Because ceph_file_to_extents() will merge adjacent object extents |
| 2183 | * together, each object request's data descriptor may point to multiple |
| 2184 | * different chunks of @fctx->pos data buffer. |
| 2185 | * |
| 2186 | * @fctx->pos data buffer is assumed to be large enough. |
| 2187 | */ |
| 2188 | static int rbd_img_fill_request(struct rbd_img_request *img_req, |
| 2189 | struct ceph_file_extent *img_extents, |
| 2190 | u32 num_img_extents, |
| 2191 | struct rbd_img_fill_ctx *fctx) |
| 2192 | { |
| 2193 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2194 | struct rbd_obj_request *obj_req; |
| 2195 | u32 i; |
| 2196 | int ret; |
| 2197 | |
| 2198 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
| 2199 | !rbd_layout_is_fancy(&rbd_dev->layout)) |
| 2200 | return rbd_img_fill_request_nocopy(img_req, img_extents, |
| 2201 | num_img_extents, fctx); |
| 2202 | |
| 2203 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
| 2204 | |
| 2205 | /* |
| 2206 | * Create object requests and determine ->bvec_count for each object |
| 2207 | * request. Note that ->bvec_count sum over all object requests may |
| 2208 | * be greater than the number of bio_vecs in the provided bio (list) |
| 2209 | * or bio_vec array because when mapped, those bio_vecs can straddle |
| 2210 | * stripe unit boundaries. |
| 2211 | */ |
| 2212 | fctx->iter = *fctx->pos; |
| 2213 | for (i = 0; i < num_img_extents; i++) { |
| 2214 | ret = ceph_file_to_extents(&rbd_dev->layout, |
| 2215 | img_extents[i].fe_off, |
| 2216 | img_extents[i].fe_len, |
| 2217 | &img_req->object_extents, |
| 2218 | alloc_object_extent, img_req, |
| 2219 | fctx->count_fn, &fctx->iter); |
| 2220 | if (ret) |
| 2221 | return ret; |
| 2222 | } |
| 2223 | |
| 2224 | for_each_obj_request(img_req, obj_req) { |
| 2225 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, |
| 2226 | sizeof(*obj_req->bvec_pos.bvecs), |
| 2227 | GFP_NOIO); |
| 2228 | if (!obj_req->bvec_pos.bvecs) |
| 2229 | return -ENOMEM; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2233 | * Fill in each object request's private bio_vec array, splitting and |
| 2234 | * rearranging the provided bio_vecs in stripe unit chunks as needed. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2235 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2236 | fctx->iter = *fctx->pos; |
| 2237 | for (i = 0; i < num_img_extents; i++) { |
| 2238 | ret = ceph_iterate_extents(&rbd_dev->layout, |
| 2239 | img_extents[i].fe_off, |
| 2240 | img_extents[i].fe_len, |
| 2241 | &img_req->object_extents, |
| 2242 | fctx->copy_fn, &fctx->iter); |
| 2243 | if (ret) |
| 2244 | return ret; |
| 2245 | } |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2246 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2247 | return __rbd_img_fill_request(img_req); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2248 | } |
| 2249 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2250 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, |
| 2251 | u64 off, u64 len) |
| 2252 | { |
| 2253 | struct ceph_file_extent ex = { off, len }; |
| 2254 | union rbd_img_fill_iter dummy; |
| 2255 | struct rbd_img_fill_ctx fctx = { |
| 2256 | .pos_type = OBJ_REQUEST_NODATA, |
| 2257 | .pos = &dummy, |
| 2258 | }; |
| 2259 | |
| 2260 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); |
| 2261 | } |
| 2262 | |
| 2263 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2264 | { |
| 2265 | struct rbd_obj_request *obj_req = |
| 2266 | container_of(ex, struct rbd_obj_request, ex); |
| 2267 | struct ceph_bio_iter *it = arg; |
| 2268 | |
| 2269 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2270 | obj_req->bio_pos = *it; |
| 2271 | ceph_bio_iter_advance(it, bytes); |
| 2272 | } |
| 2273 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2274 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2275 | { |
| 2276 | struct rbd_obj_request *obj_req = |
| 2277 | container_of(ex, struct rbd_obj_request, ex); |
| 2278 | struct ceph_bio_iter *it = arg; |
| 2279 | |
| 2280 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2281 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2282 | obj_req->bvec_count++; |
| 2283 | })); |
| 2284 | |
| 2285 | } |
| 2286 | |
| 2287 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2288 | { |
| 2289 | struct rbd_obj_request *obj_req = |
| 2290 | container_of(ex, struct rbd_obj_request, ex); |
| 2291 | struct ceph_bio_iter *it = arg; |
| 2292 | |
| 2293 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2294 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2295 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2296 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2297 | })); |
| 2298 | } |
| 2299 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2300 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2301 | struct ceph_file_extent *img_extents, |
| 2302 | u32 num_img_extents, |
| 2303 | struct ceph_bio_iter *bio_pos) |
| 2304 | { |
| 2305 | struct rbd_img_fill_ctx fctx = { |
| 2306 | .pos_type = OBJ_REQUEST_BIO, |
| 2307 | .pos = (union rbd_img_fill_iter *)bio_pos, |
| 2308 | .set_pos_fn = set_bio_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2309 | .count_fn = count_bio_bvecs, |
| 2310 | .copy_fn = copy_bio_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2311 | }; |
| 2312 | |
| 2313 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2314 | &fctx); |
| 2315 | } |
| 2316 | |
| 2317 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2318 | u64 off, u64 len, struct bio *bio) |
| 2319 | { |
| 2320 | struct ceph_file_extent ex = { off, len }; |
| 2321 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; |
| 2322 | |
| 2323 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
| 2324 | } |
| 2325 | |
| 2326 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2327 | { |
| 2328 | struct rbd_obj_request *obj_req = |
| 2329 | container_of(ex, struct rbd_obj_request, ex); |
| 2330 | struct ceph_bvec_iter *it = arg; |
| 2331 | |
| 2332 | obj_req->bvec_pos = *it; |
| 2333 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); |
| 2334 | ceph_bvec_iter_advance(it, bytes); |
| 2335 | } |
| 2336 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2337 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2338 | { |
| 2339 | struct rbd_obj_request *obj_req = |
| 2340 | container_of(ex, struct rbd_obj_request, ex); |
| 2341 | struct ceph_bvec_iter *it = arg; |
| 2342 | |
| 2343 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2344 | obj_req->bvec_count++; |
| 2345 | })); |
| 2346 | } |
| 2347 | |
| 2348 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2349 | { |
| 2350 | struct rbd_obj_request *obj_req = |
| 2351 | container_of(ex, struct rbd_obj_request, ex); |
| 2352 | struct ceph_bvec_iter *it = arg; |
| 2353 | |
| 2354 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2355 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2356 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2357 | })); |
| 2358 | } |
| 2359 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2360 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2361 | struct ceph_file_extent *img_extents, |
| 2362 | u32 num_img_extents, |
| 2363 | struct ceph_bvec_iter *bvec_pos) |
| 2364 | { |
| 2365 | struct rbd_img_fill_ctx fctx = { |
| 2366 | .pos_type = OBJ_REQUEST_BVECS, |
| 2367 | .pos = (union rbd_img_fill_iter *)bvec_pos, |
| 2368 | .set_pos_fn = set_bvec_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2369 | .count_fn = count_bvecs, |
| 2370 | .copy_fn = copy_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2371 | }; |
| 2372 | |
| 2373 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2374 | &fctx); |
| 2375 | } |
| 2376 | |
| 2377 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2378 | struct ceph_file_extent *img_extents, |
| 2379 | u32 num_img_extents, |
| 2380 | struct bio_vec *bvecs) |
| 2381 | { |
| 2382 | struct ceph_bvec_iter it = { |
| 2383 | .bvecs = bvecs, |
| 2384 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, |
| 2385 | num_img_extents) }, |
| 2386 | }; |
| 2387 | |
| 2388 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
| 2389 | &it); |
| 2390 | } |
| 2391 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2392 | static void rbd_img_handle_request_work(struct work_struct *work) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2393 | { |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2394 | struct rbd_img_request *img_req = |
| 2395 | container_of(work, struct rbd_img_request, work); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2396 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2397 | rbd_img_handle_request(img_req, img_req->work_result); |
| 2398 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2399 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2400 | static void rbd_img_schedule(struct rbd_img_request *img_req, int result) |
| 2401 | { |
| 2402 | INIT_WORK(&img_req->work, rbd_img_handle_request_work); |
| 2403 | img_req->work_result = result; |
| 2404 | queue_work(rbd_wq, &img_req->work); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2405 | } |
| 2406 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2407 | static int rbd_obj_read_object(struct rbd_obj_request *obj_req) |
| 2408 | { |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2409 | struct ceph_osd_request *osd_req; |
| 2410 | int ret; |
| 2411 | |
| 2412 | osd_req = __rbd_obj_add_osd_request(obj_req, NULL, 1); |
| 2413 | if (IS_ERR(osd_req)) |
| 2414 | return PTR_ERR(osd_req); |
| 2415 | |
| 2416 | osd_req_op_extent_init(osd_req, 0, CEPH_OSD_OP_READ, |
| 2417 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
| 2418 | rbd_osd_setup_data(osd_req, 0); |
| 2419 | rbd_osd_format_read(osd_req); |
| 2420 | |
| 2421 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
| 2422 | if (ret) |
| 2423 | return ret; |
| 2424 | |
| 2425 | rbd_osd_submit(osd_req); |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2426 | return 0; |
| 2427 | } |
| 2428 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2429 | 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] | 2430 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2431 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2432 | struct rbd_img_request *child_img_req; |
| 2433 | int ret; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2434 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2435 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
| 2436 | OBJ_OP_READ, NULL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2437 | if (!child_img_req) |
| 2438 | return -ENOMEM; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2439 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2440 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
| 2441 | child_img_req->obj_request = obj_req; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2442 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2443 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2444 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2445 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2446 | ret = __rbd_img_fill_from_bio(child_img_req, |
| 2447 | obj_req->img_extents, |
| 2448 | obj_req->num_img_extents, |
| 2449 | &obj_req->bio_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2450 | break; |
| 2451 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2452 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2453 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
| 2454 | obj_req->img_extents, |
| 2455 | obj_req->num_img_extents, |
| 2456 | &obj_req->bvec_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2457 | break; |
| 2458 | default: |
Arnd Bergmann | d342a15 | 2019-03-22 15:36:37 +0100 | [diff] [blame] | 2459 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2460 | } |
| 2461 | } else { |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2462 | ret = rbd_img_fill_from_bvecs(child_img_req, |
| 2463 | obj_req->img_extents, |
| 2464 | obj_req->num_img_extents, |
| 2465 | obj_req->copyup_bvecs); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2466 | } |
| 2467 | if (ret) { |
| 2468 | rbd_img_request_put(child_img_req); |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2472 | /* avoid parent chain recursion */ |
| 2473 | rbd_img_schedule(child_img_req, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2474 | return 0; |
| 2475 | } |
| 2476 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2477 | static bool rbd_obj_advance_read(struct rbd_obj_request *obj_req, int *result) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2478 | { |
| 2479 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2480 | int ret; |
| 2481 | |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 2482 | switch (obj_req->read_state) { |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2483 | case RBD_OBJ_READ_START: |
| 2484 | rbd_assert(!*result); |
| 2485 | |
| 2486 | ret = rbd_obj_read_object(obj_req); |
| 2487 | if (ret) { |
| 2488 | *result = ret; |
| 2489 | return true; |
| 2490 | } |
| 2491 | obj_req->read_state = RBD_OBJ_READ_OBJECT; |
| 2492 | return false; |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 2493 | case RBD_OBJ_READ_OBJECT: |
| 2494 | if (*result == -ENOENT && rbd_dev->parent_overlap) { |
| 2495 | /* reverse map this object extent onto the parent */ |
| 2496 | ret = rbd_obj_calc_img_extents(obj_req, false); |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2497 | if (ret) { |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 2498 | *result = ret; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2499 | return true; |
| 2500 | } |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 2501 | if (obj_req->num_img_extents) { |
| 2502 | ret = rbd_obj_read_from_parent(obj_req); |
| 2503 | if (ret) { |
| 2504 | *result = ret; |
| 2505 | return true; |
| 2506 | } |
| 2507 | obj_req->read_state = RBD_OBJ_READ_PARENT; |
| 2508 | return false; |
| 2509 | } |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2510 | } |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2511 | |
Ilya Dryomov | a9b67e6 | 2019-05-08 13:35:57 +0200 | [diff] [blame] | 2512 | /* |
| 2513 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2514 | * length of the request. A short read also implies zero-fill |
| 2515 | * to the end of the request. |
| 2516 | */ |
| 2517 | if (*result == -ENOENT) { |
| 2518 | rbd_obj_zero_range(obj_req, 0, obj_req->ex.oe_len); |
| 2519 | *result = 0; |
| 2520 | } else if (*result >= 0) { |
| 2521 | if (*result < obj_req->ex.oe_len) |
| 2522 | rbd_obj_zero_range(obj_req, *result, |
| 2523 | obj_req->ex.oe_len - *result); |
| 2524 | else |
| 2525 | rbd_assert(*result == obj_req->ex.oe_len); |
| 2526 | *result = 0; |
| 2527 | } |
| 2528 | return true; |
| 2529 | case RBD_OBJ_READ_PARENT: |
| 2530 | return true; |
| 2531 | default: |
| 2532 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2533 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2536 | static int rbd_obj_write_object(struct rbd_obj_request *obj_req) |
| 2537 | { |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2538 | struct ceph_osd_request *osd_req; |
| 2539 | int num_ops = count_write_ops(obj_req); |
| 2540 | int which = 0; |
| 2541 | int ret; |
| 2542 | |
| 2543 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) |
| 2544 | num_ops++; /* stat */ |
| 2545 | |
| 2546 | osd_req = rbd_obj_add_osd_request(obj_req, num_ops); |
| 2547 | if (IS_ERR(osd_req)) |
| 2548 | return PTR_ERR(osd_req); |
| 2549 | |
| 2550 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) { |
| 2551 | ret = rbd_osd_setup_stat(osd_req, which++); |
| 2552 | if (ret) |
| 2553 | return ret; |
| 2554 | } |
| 2555 | |
| 2556 | rbd_osd_setup_write_ops(osd_req, which); |
| 2557 | rbd_osd_format_write(osd_req); |
| 2558 | |
| 2559 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
| 2560 | if (ret) |
| 2561 | return ret; |
| 2562 | |
| 2563 | rbd_osd_submit(osd_req); |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2564 | return 0; |
| 2565 | } |
| 2566 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2567 | /* |
| 2568 | * copyup_bvecs pages are never highmem pages |
| 2569 | */ |
| 2570 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2571 | { |
| 2572 | struct ceph_bvec_iter it = { |
| 2573 | .bvecs = bvecs, |
| 2574 | .iter = { .bi_size = bytes }, |
| 2575 | }; |
| 2576 | |
| 2577 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2578 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2579 | bv.bv_len)) |
| 2580 | return false; |
| 2581 | })); |
| 2582 | return true; |
| 2583 | } |
| 2584 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2585 | #define MODS_ONLY U32_MAX |
| 2586 | |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2587 | static int rbd_obj_copyup_empty_snapc(struct rbd_obj_request *obj_req, |
| 2588 | u32 bytes) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2589 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2590 | struct ceph_osd_request *osd_req; |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2591 | int ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2592 | |
| 2593 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2594 | rbd_assert(bytes > 0 && bytes != MODS_ONLY); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2595 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2596 | osd_req = __rbd_obj_add_osd_request(obj_req, &rbd_empty_snapc, 1); |
| 2597 | if (IS_ERR(osd_req)) |
| 2598 | return PTR_ERR(osd_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2599 | |
Ilya Dryomov | b5ae8cb | 2019-05-29 16:53:14 +0200 | [diff] [blame] | 2600 | ret = rbd_osd_setup_copyup(osd_req, 0, bytes); |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2601 | if (ret) |
| 2602 | return ret; |
| 2603 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2604 | rbd_osd_format_write(osd_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2605 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2606 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2607 | if (ret) |
| 2608 | return ret; |
| 2609 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2610 | rbd_osd_submit(osd_req); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2611 | return 0; |
| 2612 | } |
| 2613 | |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2614 | static int rbd_obj_copyup_current_snapc(struct rbd_obj_request *obj_req, |
| 2615 | u32 bytes) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2616 | { |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2617 | struct ceph_osd_request *osd_req; |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2618 | int num_ops = count_write_ops(obj_req); |
| 2619 | int which = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2620 | int ret; |
| 2621 | |
| 2622 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2623 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2624 | if (bytes != MODS_ONLY) |
| 2625 | num_ops++; /* copyup */ |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2626 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2627 | osd_req = rbd_obj_add_osd_request(obj_req, num_ops); |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2628 | if (IS_ERR(osd_req)) |
| 2629 | return PTR_ERR(osd_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2630 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2631 | if (bytes != MODS_ONLY) { |
Ilya Dryomov | b5ae8cb | 2019-05-29 16:53:14 +0200 | [diff] [blame] | 2632 | ret = rbd_osd_setup_copyup(osd_req, which++, bytes); |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2633 | if (ret) |
| 2634 | return ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2635 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2636 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2637 | rbd_osd_setup_write_ops(osd_req, which); |
| 2638 | rbd_osd_format_write(osd_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2639 | |
Ilya Dryomov | bcbab1d | 2019-05-27 11:41:36 +0200 | [diff] [blame] | 2640 | ret = ceph_osdc_alloc_messages(osd_req, GFP_NOIO); |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 2641 | if (ret) |
| 2642 | return ret; |
| 2643 | |
Ilya Dryomov | a086a1b | 2019-06-12 18:33:31 +0200 | [diff] [blame] | 2644 | rbd_osd_submit(osd_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2645 | return 0; |
| 2646 | } |
| 2647 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2648 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2649 | { |
| 2650 | u32 i; |
| 2651 | |
| 2652 | rbd_assert(!obj_req->copyup_bvecs); |
| 2653 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2654 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2655 | sizeof(*obj_req->copyup_bvecs), |
| 2656 | GFP_NOIO); |
| 2657 | if (!obj_req->copyup_bvecs) |
| 2658 | return -ENOMEM; |
| 2659 | |
| 2660 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2661 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2662 | |
| 2663 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2664 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2665 | return -ENOMEM; |
| 2666 | |
| 2667 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2668 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2669 | obj_overlap -= len; |
| 2670 | } |
| 2671 | |
| 2672 | rbd_assert(!obj_overlap); |
| 2673 | return 0; |
| 2674 | } |
| 2675 | |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2676 | /* |
| 2677 | * The target object doesn't exist. Read the data for the entire |
| 2678 | * target object up to the overlap point (if any) from the parent, |
| 2679 | * so we can use it for a copyup. |
| 2680 | */ |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2681 | static int rbd_obj_copyup_read_parent(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2682 | { |
| 2683 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2684 | int ret; |
| 2685 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2686 | rbd_assert(obj_req->num_img_extents); |
| 2687 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 2688 | rbd_dev->parent_overlap); |
| 2689 | if (!obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2690 | /* |
| 2691 | * The overlap has become 0 (most likely because the |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2692 | * image has been flattened). Re-submit the original write |
| 2693 | * request -- pass MODS_ONLY since the copyup isn't needed |
| 2694 | * anymore. |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2695 | */ |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2696 | return rbd_obj_copyup_current_snapc(obj_req, MODS_ONLY); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2697 | } |
| 2698 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2699 | 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] | 2700 | if (ret) |
| 2701 | return ret; |
| 2702 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2703 | return rbd_obj_read_from_parent(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2706 | static void rbd_obj_copyup_write_object(struct rbd_obj_request *obj_req) |
| 2707 | { |
| 2708 | u32 bytes = rbd_obj_img_extents_bytes(obj_req); |
| 2709 | int ret; |
| 2710 | |
| 2711 | rbd_assert(!obj_req->pending.result && !obj_req->pending.num_pending); |
| 2712 | |
| 2713 | /* |
| 2714 | * Only send non-zero copyup data to save some I/O and network |
| 2715 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2716 | * existing. |
| 2717 | */ |
| 2718 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ZEROS) |
| 2719 | bytes = 0; |
| 2720 | |
| 2721 | if (obj_req->img_request->snapc->num_snaps && bytes > 0) { |
| 2722 | /* |
| 2723 | * Send a copyup request with an empty snapshot context to |
| 2724 | * deep-copyup the object through all existing snapshots. |
| 2725 | * A second request with the current snapshot context will be |
| 2726 | * sent for the actual modification. |
| 2727 | */ |
| 2728 | ret = rbd_obj_copyup_empty_snapc(obj_req, bytes); |
| 2729 | if (ret) { |
| 2730 | obj_req->pending.result = ret; |
| 2731 | return; |
| 2732 | } |
| 2733 | |
| 2734 | obj_req->pending.num_pending++; |
| 2735 | bytes = MODS_ONLY; |
| 2736 | } |
| 2737 | |
| 2738 | ret = rbd_obj_copyup_current_snapc(obj_req, bytes); |
| 2739 | if (ret) { |
| 2740 | obj_req->pending.result = ret; |
| 2741 | return; |
| 2742 | } |
| 2743 | |
| 2744 | obj_req->pending.num_pending++; |
| 2745 | } |
| 2746 | |
| 2747 | static bool rbd_obj_advance_copyup(struct rbd_obj_request *obj_req, int *result) |
| 2748 | { |
| 2749 | int ret; |
| 2750 | |
| 2751 | again: |
| 2752 | switch (obj_req->copyup_state) { |
| 2753 | case RBD_OBJ_COPYUP_START: |
| 2754 | rbd_assert(!*result); |
| 2755 | |
| 2756 | ret = rbd_obj_copyup_read_parent(obj_req); |
| 2757 | if (ret) { |
| 2758 | *result = ret; |
| 2759 | return true; |
| 2760 | } |
| 2761 | if (obj_req->num_img_extents) |
| 2762 | obj_req->copyup_state = RBD_OBJ_COPYUP_READ_PARENT; |
| 2763 | else |
| 2764 | obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT; |
| 2765 | return false; |
| 2766 | case RBD_OBJ_COPYUP_READ_PARENT: |
| 2767 | if (*result) |
| 2768 | return true; |
| 2769 | |
| 2770 | if (is_zero_bvecs(obj_req->copyup_bvecs, |
| 2771 | rbd_obj_img_extents_bytes(obj_req))) { |
| 2772 | dout("%s %p detected zeros\n", __func__, obj_req); |
| 2773 | obj_req->flags |= RBD_OBJ_FLAG_COPYUP_ZEROS; |
| 2774 | } |
| 2775 | |
| 2776 | rbd_obj_copyup_write_object(obj_req); |
| 2777 | if (!obj_req->pending.num_pending) { |
| 2778 | *result = obj_req->pending.result; |
| 2779 | obj_req->copyup_state = RBD_OBJ_COPYUP_WRITE_OBJECT; |
| 2780 | goto again; |
| 2781 | } |
| 2782 | obj_req->copyup_state = __RBD_OBJ_COPYUP_WRITE_OBJECT; |
| 2783 | return false; |
| 2784 | case __RBD_OBJ_COPYUP_WRITE_OBJECT: |
| 2785 | if (!pending_result_dec(&obj_req->pending, result)) |
| 2786 | return false; |
| 2787 | /* fall through */ |
| 2788 | case RBD_OBJ_COPYUP_WRITE_OBJECT: |
| 2789 | return true; |
| 2790 | default: |
| 2791 | BUG(); |
| 2792 | } |
| 2793 | } |
| 2794 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2795 | static bool rbd_obj_advance_write(struct rbd_obj_request *obj_req, int *result) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2796 | { |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2797 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2798 | int ret; |
| 2799 | |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2800 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2801 | switch (obj_req->write_state) { |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2802 | case RBD_OBJ_WRITE_START: |
| 2803 | rbd_assert(!*result); |
| 2804 | |
| 2805 | ret = rbd_obj_write_object(obj_req); |
| 2806 | if (ret) { |
| 2807 | *result = ret; |
| 2808 | return true; |
| 2809 | } |
| 2810 | obj_req->write_state = RBD_OBJ_WRITE_OBJECT; |
| 2811 | return false; |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2812 | case RBD_OBJ_WRITE_OBJECT: |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 2813 | if (*result == -ENOENT) { |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2814 | if (obj_req->flags & RBD_OBJ_FLAG_COPYUP_ENABLED) { |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2815 | *result = 0; |
| 2816 | obj_req->copyup_state = RBD_OBJ_COPYUP_START; |
| 2817 | obj_req->write_state = __RBD_OBJ_WRITE_COPYUP; |
| 2818 | goto again; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2819 | } |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2820 | /* |
| 2821 | * On a non-existent object: |
| 2822 | * delete - -ENOENT, truncate/zero - 0 |
| 2823 | */ |
| 2824 | if (obj_req->flags & RBD_OBJ_FLAG_DELETION) |
| 2825 | *result = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2826 | } |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2827 | if (*result) |
| 2828 | return true; |
| 2829 | |
| 2830 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP; |
| 2831 | goto again; |
| 2832 | case __RBD_OBJ_WRITE_COPYUP: |
| 2833 | if (!rbd_obj_advance_copyup(obj_req, result)) |
| 2834 | return false; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2835 | /* fall through */ |
Ilya Dryomov | 793333a30 | 2019-06-13 17:44:08 +0200 | [diff] [blame] | 2836 | case RBD_OBJ_WRITE_COPYUP: |
| 2837 | if (*result) |
| 2838 | rbd_warn(rbd_dev, "copyup failed: %d", *result); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2839 | return true; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2840 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2841 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | /* |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2846 | * Return true if @obj_req is completed. |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2847 | */ |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 2848 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req, |
| 2849 | int *result) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2850 | { |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2851 | struct rbd_img_request *img_req = obj_req->img_request; |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2852 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2853 | bool done; |
| 2854 | |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2855 | mutex_lock(&obj_req->state_mutex); |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2856 | if (!rbd_img_is_write(img_req)) |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2857 | done = rbd_obj_advance_read(obj_req, result); |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2858 | else |
Ilya Dryomov | 85b5e6d | 2019-05-14 21:06:07 +0200 | [diff] [blame] | 2859 | done = rbd_obj_advance_write(obj_req, result); |
| 2860 | mutex_unlock(&obj_req->state_mutex); |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2861 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2862 | if (done && *result) { |
| 2863 | rbd_assert(*result < 0); |
| 2864 | rbd_warn(rbd_dev, "%s at objno %llu %llu~%llu result %d", |
| 2865 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, |
| 2866 | obj_req->ex.oe_off, obj_req->ex.oe_len, *result); |
| 2867 | } |
Ilya Dryomov | 0ad5d95 | 2019-05-14 20:45:38 +0200 | [diff] [blame] | 2868 | return done; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2871 | /* |
| 2872 | * This is open-coded in rbd_img_handle_request() to avoid parent chain |
| 2873 | * recursion. |
| 2874 | */ |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 2875 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req, int result) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2876 | { |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2877 | if (__rbd_obj_handle_request(obj_req, &result)) |
| 2878 | rbd_img_handle_request(obj_req->img_request, result); |
| 2879 | } |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2880 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 2881 | static bool need_exclusive_lock(struct rbd_img_request *img_req) |
| 2882 | { |
| 2883 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2884 | |
| 2885 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) |
| 2886 | return false; |
| 2887 | |
| 2888 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 2889 | return false; |
| 2890 | |
| 2891 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2892 | if (rbd_dev->opts->lock_on_read) |
| 2893 | return true; |
| 2894 | |
| 2895 | return rbd_img_is_write(img_req); |
| 2896 | } |
| 2897 | |
| 2898 | static void rbd_lock_add_request(struct rbd_img_request *img_req) |
| 2899 | { |
| 2900 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2901 | |
| 2902 | lockdep_assert_held(&rbd_dev->lock_rwsem); |
| 2903 | spin_lock(&rbd_dev->lock_lists_lock); |
| 2904 | rbd_assert(list_empty(&img_req->lock_item)); |
| 2905 | list_add_tail(&img_req->lock_item, &rbd_dev->running_list); |
| 2906 | spin_unlock(&rbd_dev->lock_lists_lock); |
| 2907 | } |
| 2908 | |
| 2909 | static void rbd_lock_del_request(struct rbd_img_request *img_req) |
| 2910 | { |
| 2911 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2912 | bool need_wakeup; |
| 2913 | |
| 2914 | lockdep_assert_held(&rbd_dev->lock_rwsem); |
| 2915 | spin_lock(&rbd_dev->lock_lists_lock); |
| 2916 | rbd_assert(!list_empty(&img_req->lock_item)); |
| 2917 | list_del_init(&img_req->lock_item); |
| 2918 | need_wakeup = (rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING && |
| 2919 | list_empty(&rbd_dev->running_list)); |
| 2920 | spin_unlock(&rbd_dev->lock_lists_lock); |
| 2921 | if (need_wakeup) |
| 2922 | complete(&rbd_dev->releasing_wait); |
| 2923 | } |
| 2924 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2925 | static void rbd_img_object_requests(struct rbd_img_request *img_req) |
| 2926 | { |
| 2927 | struct rbd_obj_request *obj_req; |
| 2928 | |
| 2929 | rbd_assert(!img_req->pending.result && !img_req->pending.num_pending); |
| 2930 | |
| 2931 | for_each_obj_request(img_req, obj_req) { |
| 2932 | int result = 0; |
| 2933 | |
| 2934 | if (__rbd_obj_handle_request(obj_req, &result)) { |
| 2935 | if (result) { |
| 2936 | img_req->pending.result = result; |
| 2937 | return; |
| 2938 | } |
| 2939 | } else { |
| 2940 | img_req->pending.num_pending++; |
| 2941 | } |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | static bool rbd_img_advance(struct rbd_img_request *img_req, int *result) |
| 2946 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2947 | again: |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2948 | switch (img_req->state) { |
| 2949 | case RBD_IMG_START: |
| 2950 | rbd_assert(!*result); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2951 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2952 | rbd_img_object_requests(img_req); |
| 2953 | if (!img_req->pending.num_pending) { |
| 2954 | *result = img_req->pending.result; |
| 2955 | img_req->state = RBD_IMG_OBJECT_REQUESTS; |
| 2956 | goto again; |
| 2957 | } |
| 2958 | img_req->state = __RBD_IMG_OBJECT_REQUESTS; |
| 2959 | return false; |
| 2960 | case __RBD_IMG_OBJECT_REQUESTS: |
| 2961 | if (!pending_result_dec(&img_req->pending, result)) |
| 2962 | return false; |
| 2963 | /* fall through */ |
| 2964 | case RBD_IMG_OBJECT_REQUESTS: |
| 2965 | return true; |
| 2966 | default: |
| 2967 | BUG(); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2968 | } |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2969 | } |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2970 | |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2971 | /* |
| 2972 | * Return true if @img_req is completed. |
| 2973 | */ |
| 2974 | static bool __rbd_img_handle_request(struct rbd_img_request *img_req, |
| 2975 | int *result) |
| 2976 | { |
| 2977 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2978 | bool done; |
| 2979 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 2980 | if (need_exclusive_lock(img_req)) { |
| 2981 | down_read(&rbd_dev->lock_rwsem); |
| 2982 | mutex_lock(&img_req->state_mutex); |
| 2983 | done = rbd_img_advance(img_req, result); |
| 2984 | if (done) |
| 2985 | rbd_lock_del_request(img_req); |
| 2986 | mutex_unlock(&img_req->state_mutex); |
| 2987 | up_read(&rbd_dev->lock_rwsem); |
| 2988 | } else { |
| 2989 | mutex_lock(&img_req->state_mutex); |
| 2990 | done = rbd_img_advance(img_req, result); |
| 2991 | mutex_unlock(&img_req->state_mutex); |
| 2992 | } |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 2993 | |
| 2994 | if (done && *result) { |
| 2995 | rbd_assert(*result < 0); |
| 2996 | rbd_warn(rbd_dev, "%s%s result %d", |
| 2997 | test_bit(IMG_REQ_CHILD, &img_req->flags) ? "child " : "", |
| 2998 | obj_op_name(img_req->op_type), *result); |
| 2999 | } |
| 3000 | return done; |
| 3001 | } |
| 3002 | |
| 3003 | static void rbd_img_handle_request(struct rbd_img_request *img_req, int result) |
| 3004 | { |
| 3005 | again: |
| 3006 | if (!__rbd_img_handle_request(img_req, &result)) |
| 3007 | return; |
| 3008 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 3009 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 3010 | struct rbd_obj_request *obj_req = img_req->obj_request; |
| 3011 | |
Ilya Dryomov | 54ab3b2 | 2019-05-11 16:21:49 +0200 | [diff] [blame] | 3012 | rbd_img_request_put(img_req); |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 3013 | if (__rbd_obj_handle_request(obj_req, &result)) { |
| 3014 | img_req = obj_req->img_request; |
| 3015 | goto again; |
| 3016 | } |
| 3017 | } else { |
| 3018 | struct request *rq = img_req->rq; |
| 3019 | |
| 3020 | rbd_img_request_put(img_req); |
| 3021 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 3022 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3023 | } |
| 3024 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3025 | static const struct rbd_client_id rbd_empty_cid; |
| 3026 | |
| 3027 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3028 | const struct rbd_client_id *rhs) |
| 3029 | { |
| 3030 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3031 | } |
| 3032 | |
| 3033 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3034 | { |
| 3035 | struct rbd_client_id cid; |
| 3036 | |
| 3037 | mutex_lock(&rbd_dev->watch_mutex); |
| 3038 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3039 | cid.handle = rbd_dev->watch_cookie; |
| 3040 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3041 | return cid; |
| 3042 | } |
| 3043 | |
| 3044 | /* |
| 3045 | * lock_rwsem must be held for write |
| 3046 | */ |
| 3047 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3048 | const struct rbd_client_id *cid) |
| 3049 | { |
| 3050 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3051 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3052 | cid->gid, cid->handle); |
| 3053 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3054 | } |
| 3055 | |
| 3056 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3057 | { |
| 3058 | mutex_lock(&rbd_dev->watch_mutex); |
| 3059 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3060 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3061 | } |
| 3062 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3063 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 3064 | { |
| 3065 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3066 | |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3067 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3068 | strcpy(rbd_dev->lock_cookie, cookie); |
| 3069 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3070 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3071 | } |
| 3072 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3073 | /* |
| 3074 | * lock_rwsem must be held for write |
| 3075 | */ |
| 3076 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3077 | { |
| 3078 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3079 | char cookie[32]; |
| 3080 | int ret; |
| 3081 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3082 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 3083 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3084 | |
| 3085 | format_lock_cookie(rbd_dev, cookie); |
| 3086 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3087 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3088 | RBD_LOCK_TAG, "", 0); |
| 3089 | if (ret) |
| 3090 | return ret; |
| 3091 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3092 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | /* |
| 3097 | * lock_rwsem must be held for write |
| 3098 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3099 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3100 | { |
| 3101 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3102 | int ret; |
| 3103 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3104 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 3105 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3106 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3107 | 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] | 3108 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3109 | if (ret && ret != -ENOENT) |
| 3110 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3111 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3112 | /* treat errors as the image is unlocked */ |
| 3113 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3114 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3115 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3116 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3120 | enum rbd_notify_op notify_op, |
| 3121 | struct page ***preply_pages, |
| 3122 | size_t *preply_len) |
| 3123 | { |
| 3124 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3125 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 3126 | char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN]; |
| 3127 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3128 | void *p = buf; |
| 3129 | |
| 3130 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3131 | |
| 3132 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3133 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3134 | ceph_encode_32(&p, notify_op); |
| 3135 | ceph_encode_64(&p, cid.gid); |
| 3136 | ceph_encode_64(&p, cid.handle); |
| 3137 | |
| 3138 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3139 | &rbd_dev->header_oloc, buf, buf_size, |
| 3140 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3141 | } |
| 3142 | |
| 3143 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3144 | enum rbd_notify_op notify_op) |
| 3145 | { |
| 3146 | struct page **reply_pages; |
| 3147 | size_t reply_len; |
| 3148 | |
| 3149 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3150 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3151 | } |
| 3152 | |
| 3153 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3154 | { |
| 3155 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3156 | acquired_lock_work); |
| 3157 | |
| 3158 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3159 | } |
| 3160 | |
| 3161 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3162 | { |
| 3163 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3164 | released_lock_work); |
| 3165 | |
| 3166 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3167 | } |
| 3168 | |
| 3169 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3170 | { |
| 3171 | struct page **reply_pages; |
| 3172 | size_t reply_len; |
| 3173 | bool lock_owner_responded = false; |
| 3174 | int ret; |
| 3175 | |
| 3176 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3177 | |
| 3178 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3179 | &reply_pages, &reply_len); |
| 3180 | if (ret && ret != -ETIMEDOUT) { |
| 3181 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3182 | goto out; |
| 3183 | } |
| 3184 | |
| 3185 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3186 | void *p = page_address(reply_pages[0]); |
| 3187 | void *const end = p + reply_len; |
| 3188 | u32 n; |
| 3189 | |
| 3190 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3191 | while (n--) { |
| 3192 | u8 struct_v; |
| 3193 | u32 len; |
| 3194 | |
| 3195 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3196 | p += 8 + 8; /* skip gid and cookie */ |
| 3197 | |
| 3198 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3199 | if (!len) |
| 3200 | continue; |
| 3201 | |
| 3202 | if (lock_owner_responded) { |
| 3203 | rbd_warn(rbd_dev, |
| 3204 | "duplicate lock owners detected"); |
| 3205 | ret = -EIO; |
| 3206 | goto out; |
| 3207 | } |
| 3208 | |
| 3209 | lock_owner_responded = true; |
| 3210 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3211 | &struct_v, &len); |
| 3212 | if (ret) { |
| 3213 | rbd_warn(rbd_dev, |
| 3214 | "failed to decode ResponseMessage: %d", |
| 3215 | ret); |
| 3216 | goto e_inval; |
| 3217 | } |
| 3218 | |
| 3219 | ret = ceph_decode_32(&p); |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | if (!lock_owner_responded) { |
| 3224 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3225 | ret = -ETIMEDOUT; |
| 3226 | } |
| 3227 | |
| 3228 | out: |
| 3229 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3230 | return ret; |
| 3231 | |
| 3232 | e_inval: |
| 3233 | ret = -EINVAL; |
| 3234 | goto out; |
| 3235 | } |
| 3236 | |
| 3237 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3238 | { |
| 3239 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3240 | |
| 3241 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3242 | if (wake_all) |
| 3243 | wake_up_all(&rbd_dev->lock_waitq); |
| 3244 | else |
| 3245 | wake_up(&rbd_dev->lock_waitq); |
| 3246 | } |
| 3247 | |
| 3248 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3249 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3250 | { |
| 3251 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3252 | u8 lock_type; |
| 3253 | char *lock_tag; |
| 3254 | int ret; |
| 3255 | |
| 3256 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3257 | |
| 3258 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3259 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3260 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3261 | if (ret) |
| 3262 | return ret; |
| 3263 | |
| 3264 | if (*num_lockers == 0) { |
| 3265 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3266 | goto out; |
| 3267 | } |
| 3268 | |
| 3269 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3270 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3271 | lock_tag); |
| 3272 | ret = -EBUSY; |
| 3273 | goto out; |
| 3274 | } |
| 3275 | |
| 3276 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3277 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3278 | ret = -EBUSY; |
| 3279 | goto out; |
| 3280 | } |
| 3281 | |
| 3282 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3283 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3284 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3285 | (*lockers)[0].id.cookie); |
| 3286 | ret = -EBUSY; |
| 3287 | goto out; |
| 3288 | } |
| 3289 | |
| 3290 | out: |
| 3291 | kfree(lock_tag); |
| 3292 | return ret; |
| 3293 | } |
| 3294 | |
| 3295 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3296 | const struct ceph_locker *locker) |
| 3297 | { |
| 3298 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3299 | struct ceph_watch_item *watchers; |
| 3300 | u32 num_watchers; |
| 3301 | u64 cookie; |
| 3302 | int i; |
| 3303 | int ret; |
| 3304 | |
| 3305 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3306 | &rbd_dev->header_oloc, &watchers, |
| 3307 | &num_watchers); |
| 3308 | if (ret) |
| 3309 | return ret; |
| 3310 | |
| 3311 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3312 | for (i = 0; i < num_watchers; i++) { |
| 3313 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3314 | sizeof(locker->info.addr)) && |
| 3315 | watchers[i].cookie == cookie) { |
| 3316 | struct rbd_client_id cid = { |
| 3317 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3318 | .handle = cookie, |
| 3319 | }; |
| 3320 | |
| 3321 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3322 | rbd_dev, cid.gid, cid.handle); |
| 3323 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3324 | ret = 1; |
| 3325 | goto out; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3330 | ret = 0; |
| 3331 | out: |
| 3332 | kfree(watchers); |
| 3333 | return ret; |
| 3334 | } |
| 3335 | |
| 3336 | /* |
| 3337 | * lock_rwsem must be held for write |
| 3338 | */ |
| 3339 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3340 | { |
| 3341 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3342 | struct ceph_locker *lockers; |
| 3343 | u32 num_lockers; |
| 3344 | int ret; |
| 3345 | |
| 3346 | for (;;) { |
| 3347 | ret = rbd_lock(rbd_dev); |
| 3348 | if (ret != -EBUSY) |
| 3349 | return ret; |
| 3350 | |
| 3351 | /* determine if the current lock holder is still alive */ |
| 3352 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3353 | if (ret) |
| 3354 | return ret; |
| 3355 | |
| 3356 | if (num_lockers == 0) |
| 3357 | goto again; |
| 3358 | |
| 3359 | ret = find_watcher(rbd_dev, lockers); |
| 3360 | if (ret) { |
| 3361 | if (ret > 0) |
| 3362 | ret = 0; /* have to request lock */ |
| 3363 | goto out; |
| 3364 | } |
| 3365 | |
| 3366 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3367 | ENTITY_NAME(lockers[0].id.name)); |
| 3368 | |
| 3369 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3370 | &lockers[0].info.addr); |
| 3371 | if (ret) { |
| 3372 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3373 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3374 | goto out; |
| 3375 | } |
| 3376 | |
| 3377 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3378 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3379 | lockers[0].id.cookie, |
| 3380 | &lockers[0].id.name); |
| 3381 | if (ret && ret != -ENOENT) |
| 3382 | goto out; |
| 3383 | |
| 3384 | again: |
| 3385 | ceph_free_lockers(lockers, num_lockers); |
| 3386 | } |
| 3387 | |
| 3388 | out: |
| 3389 | ceph_free_lockers(lockers, num_lockers); |
| 3390 | return ret; |
| 3391 | } |
| 3392 | |
| 3393 | /* |
| 3394 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3395 | */ |
| 3396 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3397 | int *pret) |
| 3398 | { |
| 3399 | enum rbd_lock_state lock_state; |
| 3400 | |
| 3401 | down_read(&rbd_dev->lock_rwsem); |
| 3402 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3403 | rbd_dev->lock_state); |
| 3404 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3405 | lock_state = rbd_dev->lock_state; |
| 3406 | up_read(&rbd_dev->lock_rwsem); |
| 3407 | return lock_state; |
| 3408 | } |
| 3409 | |
| 3410 | up_read(&rbd_dev->lock_rwsem); |
| 3411 | down_write(&rbd_dev->lock_rwsem); |
| 3412 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3413 | rbd_dev->lock_state); |
| 3414 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3415 | *pret = rbd_try_lock(rbd_dev); |
| 3416 | if (*pret) |
| 3417 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3418 | } |
| 3419 | |
| 3420 | lock_state = rbd_dev->lock_state; |
| 3421 | up_write(&rbd_dev->lock_rwsem); |
| 3422 | return lock_state; |
| 3423 | } |
| 3424 | |
| 3425 | static void rbd_acquire_lock(struct work_struct *work) |
| 3426 | { |
| 3427 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3428 | struct rbd_device, lock_dwork); |
| 3429 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3430 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3431 | |
| 3432 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3433 | again: |
| 3434 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3435 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3436 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3437 | wake_requests(rbd_dev, true); |
| 3438 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3439 | rbd_dev, lock_state, ret); |
| 3440 | return; |
| 3441 | } |
| 3442 | |
| 3443 | ret = rbd_request_lock(rbd_dev); |
| 3444 | if (ret == -ETIMEDOUT) { |
| 3445 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3446 | } else if (ret == -EROFS) { |
| 3447 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3448 | /* |
| 3449 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3450 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3451 | * want to block. |
| 3452 | */ |
| 3453 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3454 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3455 | /* wake "rbd map --exclusive" process */ |
| 3456 | wake_requests(rbd_dev, false); |
| 3457 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3458 | } else if (ret < 0) { |
| 3459 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3460 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3461 | RBD_RETRY_DELAY); |
| 3462 | } else { |
| 3463 | /* |
| 3464 | * lock owner acked, but resend if we don't see them |
| 3465 | * release the lock |
| 3466 | */ |
| 3467 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3468 | rbd_dev); |
| 3469 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3470 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3471 | } |
| 3472 | } |
| 3473 | |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3474 | static bool rbd_quiesce_lock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3475 | { |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3476 | bool need_wait; |
| 3477 | |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3478 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3479 | lockdep_assert_held_exclusive(&rbd_dev->lock_rwsem); |
| 3480 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3481 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3482 | return false; |
| 3483 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3484 | /* |
| 3485 | * Ensure that all in-flight IO is flushed. |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3486 | */ |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3487 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3488 | rbd_assert(!completion_done(&rbd_dev->releasing_wait)); |
| 3489 | need_wait = !list_empty(&rbd_dev->running_list); |
| 3490 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3491 | if (need_wait) |
| 3492 | wait_for_completion(&rbd_dev->releasing_wait); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3493 | up_read(&rbd_dev->lock_rwsem); |
| 3494 | |
| 3495 | down_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3496 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3497 | return false; |
| 3498 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3499 | rbd_assert(list_empty(&rbd_dev->running_list)); |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3500 | return true; |
| 3501 | } |
| 3502 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3503 | static void __rbd_release_lock(struct rbd_device *rbd_dev) |
| 3504 | { |
| 3505 | rbd_assert(list_empty(&rbd_dev->running_list)); |
| 3506 | |
| 3507 | rbd_unlock(rbd_dev); |
| 3508 | } |
| 3509 | |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3510 | /* |
| 3511 | * lock_rwsem must be held for write |
| 3512 | */ |
| 3513 | static void rbd_release_lock(struct rbd_device *rbd_dev) |
| 3514 | { |
| 3515 | if (!rbd_quiesce_lock(rbd_dev)) |
| 3516 | return; |
| 3517 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3518 | __rbd_release_lock(rbd_dev); |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3519 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3520 | /* |
| 3521 | * Give others a chance to grab the lock - we would re-acquire |
| 3522 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3523 | * otherwise. We need to ack our own notifications, so this |
| 3524 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3525 | * after wake_requests() in rbd_handle_released_lock(). |
| 3526 | */ |
| 3527 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | static void rbd_release_lock_work(struct work_struct *work) |
| 3531 | { |
| 3532 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3533 | unlock_work); |
| 3534 | |
| 3535 | down_write(&rbd_dev->lock_rwsem); |
| 3536 | rbd_release_lock(rbd_dev); |
| 3537 | up_write(&rbd_dev->lock_rwsem); |
| 3538 | } |
| 3539 | |
| 3540 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3541 | void **p) |
| 3542 | { |
| 3543 | struct rbd_client_id cid = { 0 }; |
| 3544 | |
| 3545 | if (struct_v >= 2) { |
| 3546 | cid.gid = ceph_decode_64(p); |
| 3547 | cid.handle = ceph_decode_64(p); |
| 3548 | } |
| 3549 | |
| 3550 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3551 | cid.handle); |
| 3552 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3553 | down_write(&rbd_dev->lock_rwsem); |
| 3554 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3555 | /* |
| 3556 | * we already know that the remote client is |
| 3557 | * the owner |
| 3558 | */ |
| 3559 | up_write(&rbd_dev->lock_rwsem); |
| 3560 | return; |
| 3561 | } |
| 3562 | |
| 3563 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3564 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3565 | } else { |
| 3566 | down_read(&rbd_dev->lock_rwsem); |
| 3567 | } |
| 3568 | |
| 3569 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3570 | wake_requests(rbd_dev, false); |
| 3571 | up_read(&rbd_dev->lock_rwsem); |
| 3572 | } |
| 3573 | |
| 3574 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3575 | void **p) |
| 3576 | { |
| 3577 | struct rbd_client_id cid = { 0 }; |
| 3578 | |
| 3579 | if (struct_v >= 2) { |
| 3580 | cid.gid = ceph_decode_64(p); |
| 3581 | cid.handle = ceph_decode_64(p); |
| 3582 | } |
| 3583 | |
| 3584 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3585 | cid.handle); |
| 3586 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3587 | down_write(&rbd_dev->lock_rwsem); |
| 3588 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3589 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3590 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3591 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3592 | up_write(&rbd_dev->lock_rwsem); |
| 3593 | return; |
| 3594 | } |
| 3595 | |
| 3596 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3597 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3598 | } else { |
| 3599 | down_read(&rbd_dev->lock_rwsem); |
| 3600 | } |
| 3601 | |
| 3602 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3603 | wake_requests(rbd_dev, false); |
| 3604 | up_read(&rbd_dev->lock_rwsem); |
| 3605 | } |
| 3606 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3607 | /* |
| 3608 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3609 | * ResponseMessage is needed. |
| 3610 | */ |
| 3611 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3612 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3613 | { |
| 3614 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3615 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3616 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3617 | |
| 3618 | if (struct_v >= 2) { |
| 3619 | cid.gid = ceph_decode_64(p); |
| 3620 | cid.handle = ceph_decode_64(p); |
| 3621 | } |
| 3622 | |
| 3623 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3624 | cid.handle); |
| 3625 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3626 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3627 | |
| 3628 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3629 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3630 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3631 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3632 | goto out_unlock; |
| 3633 | |
| 3634 | /* |
| 3635 | * encode ResponseMessage(0) so the peer can detect |
| 3636 | * a missing owner |
| 3637 | */ |
| 3638 | result = 0; |
| 3639 | |
| 3640 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3641 | if (!rbd_dev->opts->exclusive) { |
| 3642 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3643 | __func__, rbd_dev); |
| 3644 | queue_work(rbd_dev->task_wq, |
| 3645 | &rbd_dev->unlock_work); |
| 3646 | } else { |
| 3647 | /* refuse to release the lock */ |
| 3648 | result = -EROFS; |
| 3649 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3650 | } |
| 3651 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3652 | |
| 3653 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3654 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3655 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3656 | } |
| 3657 | |
| 3658 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3659 | u64 notify_id, u64 cookie, s32 *result) |
| 3660 | { |
| 3661 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 3662 | char buf[4 + CEPH_ENCODING_START_BLK_LEN]; |
| 3663 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3664 | int ret; |
| 3665 | |
| 3666 | if (result) { |
| 3667 | void *p = buf; |
| 3668 | |
| 3669 | /* encode ResponseMessage */ |
| 3670 | ceph_start_encoding(&p, 1, 1, |
| 3671 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3672 | ceph_encode_32(&p, *result); |
| 3673 | } else { |
| 3674 | buf_size = 0; |
| 3675 | } |
| 3676 | |
| 3677 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3678 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3679 | buf, buf_size); |
| 3680 | if (ret) |
| 3681 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3682 | } |
| 3683 | |
| 3684 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3685 | u64 cookie) |
| 3686 | { |
| 3687 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3688 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3689 | } |
| 3690 | |
| 3691 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3692 | u64 notify_id, u64 cookie, s32 result) |
| 3693 | { |
| 3694 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3695 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3696 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3697 | |
| 3698 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3699 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3700 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3701 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3702 | void *p = data; |
| 3703 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3704 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3705 | u32 len; |
| 3706 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3707 | int ret; |
| 3708 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3709 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3710 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3711 | if (data_len) { |
| 3712 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3713 | &struct_v, &len); |
| 3714 | if (ret) { |
| 3715 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3716 | ret); |
| 3717 | return; |
| 3718 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3719 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3720 | notify_op = ceph_decode_32(&p); |
| 3721 | } else { |
| 3722 | /* legacy notification for header updates */ |
| 3723 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3724 | len = 0; |
| 3725 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3726 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3727 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3728 | switch (notify_op) { |
| 3729 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3730 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3731 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3732 | break; |
| 3733 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3734 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3735 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3736 | break; |
| 3737 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3738 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3739 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3740 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3741 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3742 | else |
| 3743 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3744 | break; |
| 3745 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3746 | ret = rbd_dev_refresh(rbd_dev); |
| 3747 | if (ret) |
| 3748 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3749 | |
| 3750 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3751 | break; |
| 3752 | default: |
| 3753 | if (rbd_is_lock_owner(rbd_dev)) |
| 3754 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3755 | cookie, -EOPNOTSUPP); |
| 3756 | else |
| 3757 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3758 | break; |
| 3759 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3760 | } |
| 3761 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3762 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3763 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3764 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3765 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3766 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3767 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3768 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3769 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3770 | down_write(&rbd_dev->lock_rwsem); |
| 3771 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3772 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3773 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3774 | mutex_lock(&rbd_dev->watch_mutex); |
| 3775 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3776 | __rbd_unregister_watch(rbd_dev); |
| 3777 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3778 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3779 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3780 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3781 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3785 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3786 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3787 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3788 | { |
| 3789 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3790 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3791 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3792 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3793 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3794 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3795 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3796 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3797 | rbd_watch_errcb, rbd_dev); |
| 3798 | if (IS_ERR(handle)) |
| 3799 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3800 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3801 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3802 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3803 | } |
| 3804 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3805 | /* |
| 3806 | * watch_mutex must be locked |
| 3807 | */ |
| 3808 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3809 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3810 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3811 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3812 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3813 | rbd_assert(rbd_dev->watch_handle); |
| 3814 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3815 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3816 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3817 | if (ret) |
| 3818 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3819 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3820 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3821 | } |
| 3822 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3823 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3824 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3825 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3826 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3827 | mutex_lock(&rbd_dev->watch_mutex); |
| 3828 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3829 | ret = __rbd_register_watch(rbd_dev); |
| 3830 | if (ret) |
| 3831 | goto out; |
| 3832 | |
| 3833 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3834 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3835 | |
| 3836 | out: |
| 3837 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3838 | return ret; |
| 3839 | } |
| 3840 | |
| 3841 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3842 | { |
| 3843 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3844 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3845 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3846 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3847 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3848 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3849 | } |
| 3850 | |
| 3851 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3852 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3853 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3854 | cancel_tasks_sync(rbd_dev); |
| 3855 | |
| 3856 | mutex_lock(&rbd_dev->watch_mutex); |
| 3857 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3858 | __rbd_unregister_watch(rbd_dev); |
| 3859 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3860 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3861 | |
Dongsheng Yang | 23edca8 | 2018-06-04 06:24:37 -0400 | [diff] [blame] | 3862 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3863 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3864 | } |
| 3865 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3866 | /* |
| 3867 | * lock_rwsem must be held for write |
| 3868 | */ |
| 3869 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3870 | { |
| 3871 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3872 | char cookie[32]; |
| 3873 | int ret; |
| 3874 | |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3875 | if (!rbd_quiesce_lock(rbd_dev)) |
| 3876 | return; |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3877 | |
| 3878 | format_lock_cookie(rbd_dev, cookie); |
| 3879 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3880 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3881 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3882 | RBD_LOCK_TAG, cookie); |
| 3883 | if (ret) { |
| 3884 | if (ret != -EOPNOTSUPP) |
| 3885 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3886 | ret); |
| 3887 | |
| 3888 | /* |
| 3889 | * Lock cookie cannot be updated on older OSDs, so do |
| 3890 | * a manual release and queue an acquire. |
| 3891 | */ |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 3892 | __rbd_release_lock(rbd_dev); |
Ilya Dryomov | a2b1da0 | 2019-05-30 11:15:23 +0200 | [diff] [blame] | 3893 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3894 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3895 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3899 | static void rbd_reregister_watch(struct work_struct *work) |
| 3900 | { |
| 3901 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3902 | struct rbd_device, watch_dwork); |
| 3903 | int ret; |
| 3904 | |
| 3905 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3906 | |
| 3907 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3908 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3909 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3910 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3911 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3912 | |
| 3913 | ret = __rbd_register_watch(rbd_dev); |
| 3914 | if (ret) { |
| 3915 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3916 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3917 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3918 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3919 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3920 | queue_delayed_work(rbd_dev->task_wq, |
| 3921 | &rbd_dev->watch_dwork, |
| 3922 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3923 | } |
| 3924 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3925 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3929 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3930 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3931 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3932 | down_write(&rbd_dev->lock_rwsem); |
| 3933 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3934 | rbd_reacquire_lock(rbd_dev); |
| 3935 | up_write(&rbd_dev->lock_rwsem); |
| 3936 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3937 | ret = rbd_dev_refresh(rbd_dev); |
| 3938 | if (ret) |
Colin Ian King | f6870cc | 2018-03-19 13:33:10 +0000 | [diff] [blame] | 3939 | rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3940 | } |
| 3941 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3942 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3943 | * Synchronous osd object method call. Returns the number of bytes |
| 3944 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3945 | */ |
| 3946 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3947 | struct ceph_object_id *oid, |
| 3948 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3949 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3950 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3951 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3952 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3953 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3954 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3955 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3956 | struct page *req_page = NULL; |
| 3957 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3958 | int ret; |
| 3959 | |
| 3960 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3961 | * Method calls are ultimately read operations. The result |
| 3962 | * should placed into the inbound buffer provided. They |
| 3963 | * also supply outbound data--parameters for the object |
| 3964 | * method. Currently if this is present it will be a |
| 3965 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3966 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3967 | if (outbound) { |
| 3968 | if (outbound_size > PAGE_SIZE) |
| 3969 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3970 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3971 | req_page = alloc_page(GFP_KERNEL); |
| 3972 | if (!req_page) |
| 3973 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3974 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3975 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3976 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3977 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3978 | reply_page = alloc_page(GFP_KERNEL); |
| 3979 | if (!reply_page) { |
| 3980 | if (req_page) |
| 3981 | __free_page(req_page); |
| 3982 | return -ENOMEM; |
| 3983 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3984 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3985 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3986 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3987 | reply_page, &inbound_size); |
| 3988 | if (!ret) { |
| 3989 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3990 | ret = inbound_size; |
| 3991 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3992 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3993 | if (req_page) |
| 3994 | __free_page(req_page); |
| 3995 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3996 | return ret; |
| 3997 | } |
| 3998 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3999 | /* |
| 4000 | * lock_rwsem must be held for read |
| 4001 | */ |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4002 | 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] | 4003 | { |
| 4004 | DEFINE_WAIT(wait); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 4005 | unsigned long timeout; |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4006 | int ret = 0; |
| 4007 | |
| 4008 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
| 4009 | return -EBLACKLISTED; |
| 4010 | |
| 4011 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 4012 | return 0; |
| 4013 | |
| 4014 | if (!may_acquire) { |
| 4015 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 4016 | return -EROFS; |
| 4017 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4018 | |
| 4019 | do { |
| 4020 | /* |
| 4021 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 4022 | * and cancel_delayed_work() in wake_requests(). |
| 4023 | */ |
| 4024 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 4025 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 4026 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 4027 | TASK_UNINTERRUPTIBLE); |
| 4028 | up_read(&rbd_dev->lock_rwsem); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 4029 | timeout = schedule_timeout(ceph_timeout_jiffies( |
| 4030 | rbd_dev->opts->lock_timeout)); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4031 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4032 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 4033 | ret = -EBLACKLISTED; |
| 4034 | break; |
| 4035 | } |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 4036 | if (!timeout) { |
| 4037 | rbd_warn(rbd_dev, "timed out waiting for lock"); |
| 4038 | ret = -ETIMEDOUT; |
| 4039 | break; |
| 4040 | } |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4041 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4042 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4043 | finish_wait(&rbd_dev->lock_waitq, &wait); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4044 | return ret; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4045 | } |
| 4046 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4047 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4048 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4049 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 4050 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4051 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4052 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4053 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4054 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4055 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4056 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4057 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4058 | int result; |
| 4059 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4060 | switch (req_op(rq)) { |
| 4061 | case REQ_OP_DISCARD: |
| 4062 | op_type = OBJ_OP_DISCARD; |
| 4063 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 4064 | case REQ_OP_WRITE_ZEROES: |
| 4065 | op_type = OBJ_OP_ZEROOUT; |
| 4066 | break; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 4067 | case REQ_OP_WRITE: |
| 4068 | op_type = OBJ_OP_WRITE; |
| 4069 | break; |
| 4070 | case REQ_OP_READ: |
| 4071 | op_type = OBJ_OP_READ; |
| 4072 | break; |
| 4073 | default: |
| 4074 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4075 | result = -EIO; |
| 4076 | goto err; |
| 4077 | } |
| 4078 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4079 | /* Ignore/skip any zero-length requests */ |
| 4080 | |
| 4081 | if (!length) { |
| 4082 | dout("%s: zero-length request\n", __func__); |
| 4083 | result = 0; |
| 4084 | goto err_rq; |
| 4085 | } |
| 4086 | |
Ilya Dryomov | b91a7bd | 2019-05-03 17:27:03 +0200 | [diff] [blame] | 4087 | if (op_type != OBJ_OP_READ && rbd_dev->spec->snap_id != CEPH_NOSNAP) { |
| 4088 | rbd_warn(rbd_dev, "%s on read-only snapshot", |
| 4089 | obj_op_name(op_type)); |
| 4090 | result = -EIO; |
| 4091 | goto err; |
| 4092 | } |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4093 | |
| 4094 | /* |
| 4095 | * Quit early if the mapped snapshot no longer exists. It's |
| 4096 | * still possible the snapshot will have disappeared by the |
| 4097 | * time our request arrives at the osd, but there's no sense in |
| 4098 | * sending it if we already know. |
| 4099 | */ |
| 4100 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4101 | dout("request for non-existent snapshot"); |
| 4102 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4103 | result = -ENXIO; |
| 4104 | goto err_rq; |
| 4105 | } |
| 4106 | |
| 4107 | if (offset && length > U64_MAX - offset + 1) { |
| 4108 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4109 | length); |
| 4110 | result = -EINVAL; |
| 4111 | goto err_rq; /* Shouldn't happen */ |
| 4112 | } |
| 4113 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4114 | blk_mq_start_request(rq); |
| 4115 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4116 | down_read(&rbd_dev->header_rwsem); |
| 4117 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4118 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4119 | snapc = rbd_dev->header.snapc; |
| 4120 | ceph_get_snap_context(snapc); |
| 4121 | } |
| 4122 | up_read(&rbd_dev->header_rwsem); |
| 4123 | |
| 4124 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4125 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4126 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4127 | result = -EIO; |
| 4128 | goto err_rq; |
| 4129 | } |
| 4130 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 4131 | must_be_locked = |
| 4132 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 4133 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4134 | if (must_be_locked) { |
| 4135 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 4136 | result = rbd_wait_state_locked(rbd_dev, |
| 4137 | !rbd_dev->opts->exclusive); |
| 4138 | if (result) |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 4139 | goto err_unlock; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4140 | } |
| 4141 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 4142 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4143 | if (!img_request) { |
| 4144 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4145 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4146 | } |
| 4147 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4148 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4149 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 4150 | if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 4151 | result = rbd_img_fill_nodata(img_request, offset, length); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4152 | else |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 4153 | result = rbd_img_fill_from_bio(img_request, offset, length, |
| 4154 | rq->bio); |
Ilya Dryomov | 0192ce2 | 2019-05-16 15:06:56 +0200 | [diff] [blame] | 4155 | if (result) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4156 | goto err_img_request; |
| 4157 | |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 4158 | if (must_be_locked) { |
| 4159 | rbd_lock_add_request(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4160 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 4161 | } |
| 4162 | |
| 4163 | rbd_img_handle_request(img_request, 0); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4164 | return; |
| 4165 | |
| 4166 | err_img_request: |
| 4167 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4168 | err_unlock: |
| 4169 | if (must_be_locked) |
| 4170 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4171 | err_rq: |
| 4172 | if (result) |
| 4173 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4174 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4175 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4176 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 4177 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4178 | } |
| 4179 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4180 | 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] | 4181 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4182 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4183 | struct request *rq = bd->rq; |
| 4184 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4185 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4186 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 4187 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4188 | } |
| 4189 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4190 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4191 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4192 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 4193 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 4194 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4195 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4198 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4199 | struct ceph_object_id *oid, |
| 4200 | struct ceph_object_locator *oloc, |
| 4201 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4202 | |
| 4203 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4204 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4205 | struct ceph_osd_request *req; |
| 4206 | struct page **pages; |
| 4207 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4208 | int ret; |
| 4209 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4210 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 4211 | if (!req) |
| 4212 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4213 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4214 | ceph_oid_copy(&req->r_base_oid, oid); |
| 4215 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 4216 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4217 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4218 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 4219 | if (IS_ERR(pages)) { |
| 4220 | ret = PTR_ERR(pages); |
| 4221 | goto out_req; |
| 4222 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4223 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4224 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 4225 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 4226 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4227 | |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 4228 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
| 4229 | if (ret) |
| 4230 | goto out_req; |
| 4231 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4232 | ceph_osdc_start_request(osdc, req, false); |
| 4233 | ret = ceph_osdc_wait_request(osdc, req); |
| 4234 | if (ret >= 0) |
| 4235 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 4236 | |
| 4237 | out_req: |
| 4238 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4239 | return ret; |
| 4240 | } |
| 4241 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4242 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4243 | * Read the complete header for the given rbd device. On successful |
| 4244 | * return, the rbd_dev->header field will contain up-to-date |
| 4245 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4246 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4247 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4248 | { |
| 4249 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4250 | u32 snap_count = 0; |
| 4251 | u64 names_size = 0; |
| 4252 | u32 want_count; |
| 4253 | int ret; |
| 4254 | |
| 4255 | /* |
| 4256 | * The complete header will include an array of its 64-bit |
| 4257 | * snapshot ids, followed by the names of those snapshots as |
| 4258 | * a contiguous block of NUL-terminated strings. Note that |
| 4259 | * the number of snapshots could change by the time we read |
| 4260 | * it in, in which case we re-read it. |
| 4261 | */ |
| 4262 | do { |
| 4263 | size_t size; |
| 4264 | |
| 4265 | kfree(ondisk); |
| 4266 | |
| 4267 | size = sizeof (*ondisk); |
| 4268 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4269 | size += names_size; |
| 4270 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4271 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4272 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4273 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4274 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4275 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4276 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4277 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4278 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4279 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4280 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4281 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4282 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4283 | } |
| 4284 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4285 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4286 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4287 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4291 | want_count = snap_count; |
| 4292 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4293 | } while (snap_count != want_count); |
| 4294 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4295 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4296 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4297 | kfree(ondisk); |
| 4298 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4299 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4300 | } |
| 4301 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4302 | /* |
| 4303 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4304 | * has disappeared from the (just updated) snapshot context. |
| 4305 | */ |
| 4306 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4307 | { |
| 4308 | u64 snap_id; |
| 4309 | |
| 4310 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4311 | return; |
| 4312 | |
| 4313 | snap_id = rbd_dev->spec->snap_id; |
| 4314 | if (snap_id == CEPH_NOSNAP) |
| 4315 | return; |
| 4316 | |
| 4317 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4318 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4319 | } |
| 4320 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4321 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4322 | { |
| 4323 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4324 | |
| 4325 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4326 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4327 | * try to update its size. If REMOVING is set, updating size |
| 4328 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4329 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4330 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4331 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4332 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4333 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4334 | set_capacity(rbd_dev->disk, size); |
| 4335 | revalidate_disk(rbd_dev->disk); |
| 4336 | } |
| 4337 | } |
| 4338 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4339 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4340 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4341 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4342 | int ret; |
| 4343 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4344 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4345 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4346 | |
| 4347 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4348 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4349 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4350 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4351 | /* |
| 4352 | * If there is a parent, see if it has disappeared due to the |
| 4353 | * mapped image getting flattened. |
| 4354 | */ |
| 4355 | if (rbd_dev->parent) { |
| 4356 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4357 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4358 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4359 | } |
| 4360 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4361 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4362 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4363 | } else { |
| 4364 | /* validate mapped snapshot's EXISTS flag */ |
| 4365 | rbd_exists_validate(rbd_dev); |
| 4366 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4367 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4368 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4369 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4370 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4371 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4372 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4373 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4374 | } |
| 4375 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 4376 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 4377 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4378 | { |
| 4379 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4380 | |
| 4381 | INIT_WORK(work, rbd_queue_workfn); |
| 4382 | return 0; |
| 4383 | } |
| 4384 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 4385 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4386 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4387 | .init_request = rbd_init_request, |
| 4388 | }; |
| 4389 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4390 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4391 | { |
| 4392 | struct gendisk *disk; |
| 4393 | struct request_queue *q; |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4394 | unsigned int objset_bytes = |
| 4395 | rbd_dev->layout.object_size * rbd_dev->layout.stripe_count; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4396 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4397 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4398 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4399 | disk = alloc_disk(single_major ? |
| 4400 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4401 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4402 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4403 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4404 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4405 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4406 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4407 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4408 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4409 | if (single_major) |
| 4410 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4411 | disk->fops = &rbd_bd_ops; |
| 4412 | disk->private_data = rbd_dev; |
| 4413 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4414 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4415 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4416 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4417 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ming Lei | 56d18f6 | 2019-02-15 19:13:24 +0800 | [diff] [blame] | 4418 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4419 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4420 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4421 | |
| 4422 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4423 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4424 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4425 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4426 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4427 | if (IS_ERR(q)) { |
| 4428 | err = PTR_ERR(q); |
| 4429 | goto out_tag_set; |
| 4430 | } |
| 4431 | |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 4432 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4433 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4434 | |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4435 | blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4436 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 4437 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 4438 | blk_queue_max_segment_size(q, UINT_MAX); |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 4439 | blk_queue_io_min(q, rbd_dev->opts->alloc_size); |
| 4440 | blk_queue_io_opt(q, rbd_dev->opts->alloc_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4441 | |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4442 | if (rbd_dev->opts->trim) { |
| 4443 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 4444 | q->limits.discard_granularity = rbd_dev->opts->alloc_size; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4445 | blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4446 | blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4447 | } |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4448 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4449 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4450 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4451 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4452 | /* |
| 4453 | * disk_release() expects a queue ref from add_disk() and will |
| 4454 | * put it. Hold an extra ref until add_disk() is called. |
| 4455 | */ |
| 4456 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4457 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4458 | q->queuedata = rbd_dev; |
| 4459 | |
| 4460 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4461 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4462 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4463 | out_tag_set: |
| 4464 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4465 | out_disk: |
| 4466 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4467 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4468 | } |
| 4469 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4470 | /* |
| 4471 | sysfs |
| 4472 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4473 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4474 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4475 | { |
| 4476 | return container_of(dev, struct rbd_device, dev); |
| 4477 | } |
| 4478 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4479 | static ssize_t rbd_size_show(struct device *dev, |
| 4480 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4481 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4482 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4483 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4484 | return sprintf(buf, "%llu\n", |
| 4485 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4486 | } |
| 4487 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4488 | /* |
| 4489 | * Note this shows the features for whatever's mapped, which is not |
| 4490 | * necessarily the base image. |
| 4491 | */ |
| 4492 | static ssize_t rbd_features_show(struct device *dev, |
| 4493 | struct device_attribute *attr, char *buf) |
| 4494 | { |
| 4495 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4496 | |
| 4497 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4498 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4499 | } |
| 4500 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4501 | static ssize_t rbd_major_show(struct device *dev, |
| 4502 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4503 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4504 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4505 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4506 | if (rbd_dev->major) |
| 4507 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4508 | |
| 4509 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4510 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4511 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4512 | static ssize_t rbd_minor_show(struct device *dev, |
| 4513 | struct device_attribute *attr, char *buf) |
| 4514 | { |
| 4515 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4516 | |
| 4517 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4518 | } |
| 4519 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4520 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4521 | struct device_attribute *attr, char *buf) |
| 4522 | { |
| 4523 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4524 | struct ceph_entity_addr *client_addr = |
| 4525 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4526 | |
| 4527 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4528 | le32_to_cpu(client_addr->nonce)); |
| 4529 | } |
| 4530 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4531 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4532 | struct device_attribute *attr, char *buf) |
| 4533 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4534 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4535 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4536 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4537 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4538 | } |
| 4539 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4540 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4541 | struct device_attribute *attr, char *buf) |
| 4542 | { |
| 4543 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4544 | |
| 4545 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4546 | } |
| 4547 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4548 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4549 | struct device_attribute *attr, char *buf) |
| 4550 | { |
| 4551 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4552 | |
| 4553 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4554 | } |
| 4555 | |
| 4556 | static ssize_t rbd_pool_show(struct device *dev, |
| 4557 | struct device_attribute *attr, char *buf) |
| 4558 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4559 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4560 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4561 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4562 | } |
| 4563 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4564 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4565 | struct device_attribute *attr, char *buf) |
| 4566 | { |
| 4567 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4568 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4569 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4570 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4571 | } |
| 4572 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4573 | static ssize_t rbd_pool_ns_show(struct device *dev, |
| 4574 | struct device_attribute *attr, char *buf) |
| 4575 | { |
| 4576 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4577 | |
| 4578 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: ""); |
| 4579 | } |
| 4580 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4581 | static ssize_t rbd_name_show(struct device *dev, |
| 4582 | struct device_attribute *attr, char *buf) |
| 4583 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4584 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4585 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4586 | if (rbd_dev->spec->image_name) |
| 4587 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4588 | |
| 4589 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4590 | } |
| 4591 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4592 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4593 | struct device_attribute *attr, char *buf) |
| 4594 | { |
| 4595 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4596 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4597 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4598 | } |
| 4599 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4600 | /* |
| 4601 | * Shows the name of the currently-mapped snapshot (or |
| 4602 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4603 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4604 | static ssize_t rbd_snap_show(struct device *dev, |
| 4605 | struct device_attribute *attr, |
| 4606 | char *buf) |
| 4607 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4608 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4609 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4610 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4611 | } |
| 4612 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4613 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4614 | struct device_attribute *attr, char *buf) |
| 4615 | { |
| 4616 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4617 | |
| 4618 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4619 | } |
| 4620 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4621 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4622 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4623 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4624 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4625 | */ |
| 4626 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4627 | struct device_attribute *attr, |
| 4628 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4629 | { |
| 4630 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4631 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4632 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4633 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4634 | return sprintf(buf, "(no parent image)\n"); |
| 4635 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4636 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4637 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4638 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4639 | count += sprintf(&buf[count], "%s" |
| 4640 | "pool_id %llu\npool_name %s\n" |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4641 | "pool_ns %s\n" |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4642 | "image_id %s\nimage_name %s\n" |
| 4643 | "snap_id %llu\nsnap_name %s\n" |
| 4644 | "overlap %llu\n", |
| 4645 | !count ? "" : "\n", /* first? */ |
| 4646 | spec->pool_id, spec->pool_name, |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4647 | spec->pool_ns ?: "", |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4648 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4649 | spec->snap_id, spec->snap_name, |
| 4650 | rbd_dev->parent_overlap); |
| 4651 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4652 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4653 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4654 | } |
| 4655 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4656 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4657 | struct device_attribute *attr, |
| 4658 | const char *buf, |
| 4659 | size_t size) |
| 4660 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4661 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4662 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4663 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4664 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4665 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4666 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4667 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4668 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4669 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4670 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4671 | static DEVICE_ATTR(size, 0444, rbd_size_show, NULL); |
| 4672 | static DEVICE_ATTR(features, 0444, rbd_features_show, NULL); |
| 4673 | static DEVICE_ATTR(major, 0444, rbd_major_show, NULL); |
| 4674 | static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL); |
| 4675 | static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL); |
| 4676 | static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL); |
| 4677 | static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL); |
| 4678 | static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL); |
| 4679 | static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL); |
| 4680 | static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4681 | static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL); |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4682 | static DEVICE_ATTR(name, 0444, rbd_name_show, NULL); |
| 4683 | static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL); |
| 4684 | static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh); |
| 4685 | static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL); |
| 4686 | static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL); |
| 4687 | static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4688 | |
| 4689 | static struct attribute *rbd_attrs[] = { |
| 4690 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4691 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4692 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4693 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4694 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4695 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4696 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4697 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4698 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4699 | &dev_attr_pool_id.attr, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4700 | &dev_attr_pool_ns.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4701 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4702 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4703 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4704 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4705 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4706 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4707 | NULL |
| 4708 | }; |
| 4709 | |
| 4710 | static struct attribute_group rbd_attr_group = { |
| 4711 | .attrs = rbd_attrs, |
| 4712 | }; |
| 4713 | |
| 4714 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4715 | &rbd_attr_group, |
| 4716 | NULL |
| 4717 | }; |
| 4718 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4719 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4720 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4721 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4722 | .name = "rbd", |
| 4723 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4724 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4725 | }; |
| 4726 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4727 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4728 | { |
| 4729 | kref_get(&spec->kref); |
| 4730 | |
| 4731 | return spec; |
| 4732 | } |
| 4733 | |
| 4734 | static void rbd_spec_free(struct kref *kref); |
| 4735 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4736 | { |
| 4737 | if (spec) |
| 4738 | kref_put(&spec->kref, rbd_spec_free); |
| 4739 | } |
| 4740 | |
| 4741 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4742 | { |
| 4743 | struct rbd_spec *spec; |
| 4744 | |
| 4745 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4746 | if (!spec) |
| 4747 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4748 | |
| 4749 | spec->pool_id = CEPH_NOPOOL; |
| 4750 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4751 | kref_init(&spec->kref); |
| 4752 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4753 | return spec; |
| 4754 | } |
| 4755 | |
| 4756 | static void rbd_spec_free(struct kref *kref) |
| 4757 | { |
| 4758 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4759 | |
| 4760 | kfree(spec->pool_name); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4761 | kfree(spec->pool_ns); |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4762 | kfree(spec->image_id); |
| 4763 | kfree(spec->image_name); |
| 4764 | kfree(spec->snap_name); |
| 4765 | kfree(spec); |
| 4766 | } |
| 4767 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4768 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4769 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4770 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4771 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4772 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4773 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4774 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4775 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4776 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4777 | rbd_put_client(rbd_dev->rbd_client); |
| 4778 | rbd_spec_put(rbd_dev->spec); |
| 4779 | kfree(rbd_dev->opts); |
| 4780 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4781 | } |
| 4782 | |
| 4783 | static void rbd_dev_release(struct device *dev) |
| 4784 | { |
| 4785 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4786 | bool need_put = !!rbd_dev->opts; |
| 4787 | |
| 4788 | if (need_put) { |
| 4789 | destroy_workqueue(rbd_dev->task_wq); |
| 4790 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4791 | } |
| 4792 | |
| 4793 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4794 | |
| 4795 | /* |
| 4796 | * This is racy, but way better than putting module outside of |
| 4797 | * the release callback. The race window is pretty small, so |
| 4798 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4799 | */ |
| 4800 | if (need_put) |
| 4801 | module_put(THIS_MODULE); |
| 4802 | } |
| 4803 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4804 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4805 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4806 | { |
| 4807 | struct rbd_device *rbd_dev; |
| 4808 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4809 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4810 | if (!rbd_dev) |
| 4811 | return NULL; |
| 4812 | |
| 4813 | spin_lock_init(&rbd_dev->lock); |
| 4814 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4815 | init_rwsem(&rbd_dev->header_rwsem); |
| 4816 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4817 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4818 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4819 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4820 | if (spec->pool_ns) { |
| 4821 | WARN_ON(!*spec->pool_ns); |
| 4822 | rbd_dev->header_oloc.pool_ns = |
| 4823 | ceph_find_or_create_string(spec->pool_ns, |
| 4824 | strlen(spec->pool_ns)); |
| 4825 | } |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4826 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4827 | mutex_init(&rbd_dev->watch_mutex); |
| 4828 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4829 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4830 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4831 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4832 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4833 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4834 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4835 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4836 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 4837 | spin_lock_init(&rbd_dev->lock_lists_lock); |
| 4838 | INIT_LIST_HEAD(&rbd_dev->running_list); |
| 4839 | init_completion(&rbd_dev->releasing_wait); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4840 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4841 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4842 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4843 | rbd_dev->dev.type = &rbd_device_type; |
| 4844 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4845 | device_initialize(&rbd_dev->dev); |
| 4846 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4847 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4848 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4849 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4850 | return rbd_dev; |
| 4851 | } |
| 4852 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4853 | /* |
| 4854 | * Create a mapping rbd_dev. |
| 4855 | */ |
| 4856 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4857 | struct rbd_spec *spec, |
| 4858 | struct rbd_options *opts) |
| 4859 | { |
| 4860 | struct rbd_device *rbd_dev; |
| 4861 | |
| 4862 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4863 | if (!rbd_dev) |
| 4864 | return NULL; |
| 4865 | |
| 4866 | rbd_dev->opts = opts; |
| 4867 | |
| 4868 | /* get an id and fill in device name */ |
| 4869 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4870 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4871 | GFP_KERNEL); |
| 4872 | if (rbd_dev->dev_id < 0) |
| 4873 | goto fail_rbd_dev; |
| 4874 | |
| 4875 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4876 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4877 | rbd_dev->name); |
| 4878 | if (!rbd_dev->task_wq) |
| 4879 | goto fail_dev_id; |
| 4880 | |
| 4881 | /* we have a ref from do_rbd_add() */ |
| 4882 | __module_get(THIS_MODULE); |
| 4883 | |
| 4884 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4885 | return rbd_dev; |
| 4886 | |
| 4887 | fail_dev_id: |
| 4888 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4889 | fail_rbd_dev: |
| 4890 | rbd_dev_free(rbd_dev); |
| 4891 | return NULL; |
| 4892 | } |
| 4893 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4894 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4895 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4896 | if (rbd_dev) |
| 4897 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4898 | } |
| 4899 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4900 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4901 | * Get the size and object order for an image snapshot, or if |
| 4902 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4903 | * image. |
| 4904 | */ |
| 4905 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4906 | u8 *order, u64 *snap_size) |
| 4907 | { |
| 4908 | __le64 snapid = cpu_to_le64(snap_id); |
| 4909 | int ret; |
| 4910 | struct { |
| 4911 | u8 order; |
| 4912 | __le64 size; |
| 4913 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4914 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4915 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4916 | &rbd_dev->header_oloc, "get_size", |
| 4917 | &snapid, sizeof(snapid), |
| 4918 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4919 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4920 | if (ret < 0) |
| 4921 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4922 | if (ret < sizeof (size_buf)) |
| 4923 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4924 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4925 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4926 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4927 | dout(" order %u", (unsigned int)*order); |
| 4928 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4929 | *snap_size = le64_to_cpu(size_buf.size); |
| 4930 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4931 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4932 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4933 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4934 | |
| 4935 | return 0; |
| 4936 | } |
| 4937 | |
| 4938 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4939 | { |
| 4940 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4941 | &rbd_dev->header.obj_order, |
| 4942 | &rbd_dev->header.image_size); |
| 4943 | } |
| 4944 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4945 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4946 | { |
| 4947 | void *reply_buf; |
| 4948 | int ret; |
| 4949 | void *p; |
| 4950 | |
| 4951 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4952 | if (!reply_buf) |
| 4953 | return -ENOMEM; |
| 4954 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4955 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4956 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4957 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4958 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4959 | if (ret < 0) |
| 4960 | goto out; |
| 4961 | |
| 4962 | p = reply_buf; |
| 4963 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4964 | p + ret, NULL, GFP_NOIO); |
| 4965 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4966 | |
| 4967 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4968 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4969 | rbd_dev->header.object_prefix = NULL; |
| 4970 | } else { |
| 4971 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4972 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4973 | out: |
| 4974 | kfree(reply_buf); |
| 4975 | |
| 4976 | return ret; |
| 4977 | } |
| 4978 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4979 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4980 | u64 *snap_features) |
| 4981 | { |
| 4982 | __le64 snapid = cpu_to_le64(snap_id); |
| 4983 | struct { |
| 4984 | __le64 features; |
| 4985 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4986 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4987 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4988 | int ret; |
| 4989 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4990 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4991 | &rbd_dev->header_oloc, "get_features", |
| 4992 | &snapid, sizeof(snapid), |
| 4993 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4994 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4995 | if (ret < 0) |
| 4996 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4997 | if (ret < sizeof (features_buf)) |
| 4998 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4999 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5000 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 5001 | if (unsup) { |
| 5002 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 5003 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5004 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5005 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 5006 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5007 | *snap_features = le64_to_cpu(features_buf.features); |
| 5008 | |
| 5009 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5010 | (unsigned long long)snap_id, |
| 5011 | (unsigned long long)*snap_features, |
| 5012 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5013 | |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 5018 | { |
| 5019 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 5020 | &rbd_dev->header.features); |
| 5021 | } |
| 5022 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5023 | struct parent_image_info { |
| 5024 | u64 pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5025 | const char *pool_ns; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5026 | const char *image_id; |
| 5027 | u64 snap_id; |
| 5028 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5029 | bool has_overlap; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5030 | u64 overlap; |
| 5031 | }; |
| 5032 | |
| 5033 | /* |
| 5034 | * The caller is responsible for @pii. |
| 5035 | */ |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5036 | static int decode_parent_image_spec(void **p, void *end, |
| 5037 | struct parent_image_info *pii) |
| 5038 | { |
| 5039 | u8 struct_v; |
| 5040 | u32 struct_len; |
| 5041 | int ret; |
| 5042 | |
| 5043 | ret = ceph_start_decoding(p, end, 1, "ParentImageSpec", |
| 5044 | &struct_v, &struct_len); |
| 5045 | if (ret) |
| 5046 | return ret; |
| 5047 | |
| 5048 | ceph_decode_64_safe(p, end, pii->pool_id, e_inval); |
| 5049 | pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 5050 | if (IS_ERR(pii->pool_ns)) { |
| 5051 | ret = PTR_ERR(pii->pool_ns); |
| 5052 | pii->pool_ns = NULL; |
| 5053 | return ret; |
| 5054 | } |
| 5055 | pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 5056 | if (IS_ERR(pii->image_id)) { |
| 5057 | ret = PTR_ERR(pii->image_id); |
| 5058 | pii->image_id = NULL; |
| 5059 | return ret; |
| 5060 | } |
| 5061 | ceph_decode_64_safe(p, end, pii->snap_id, e_inval); |
| 5062 | return 0; |
| 5063 | |
| 5064 | e_inval: |
| 5065 | return -EINVAL; |
| 5066 | } |
| 5067 | |
| 5068 | static int __get_parent_info(struct rbd_device *rbd_dev, |
| 5069 | struct page *req_page, |
| 5070 | struct page *reply_page, |
| 5071 | struct parent_image_info *pii) |
| 5072 | { |
| 5073 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5074 | size_t reply_len = PAGE_SIZE; |
| 5075 | void *p, *end; |
| 5076 | int ret; |
| 5077 | |
| 5078 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 5079 | "rbd", "parent_get", CEPH_OSD_FLAG_READ, |
| 5080 | req_page, sizeof(u64), reply_page, &reply_len); |
| 5081 | if (ret) |
| 5082 | return ret == -EOPNOTSUPP ? 1 : ret; |
| 5083 | |
| 5084 | p = page_address(reply_page); |
| 5085 | end = p + reply_len; |
| 5086 | ret = decode_parent_image_spec(&p, end, pii); |
| 5087 | if (ret) |
| 5088 | return ret; |
| 5089 | |
| 5090 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 5091 | "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ, |
| 5092 | req_page, sizeof(u64), reply_page, &reply_len); |
| 5093 | if (ret) |
| 5094 | return ret; |
| 5095 | |
| 5096 | p = page_address(reply_page); |
| 5097 | end = p + reply_len; |
| 5098 | ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval); |
| 5099 | if (pii->has_overlap) |
| 5100 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 5101 | |
| 5102 | return 0; |
| 5103 | |
| 5104 | e_inval: |
| 5105 | return -EINVAL; |
| 5106 | } |
| 5107 | |
| 5108 | /* |
| 5109 | * The caller is responsible for @pii. |
| 5110 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5111 | static int __get_parent_info_legacy(struct rbd_device *rbd_dev, |
| 5112 | struct page *req_page, |
| 5113 | struct page *reply_page, |
| 5114 | struct parent_image_info *pii) |
| 5115 | { |
| 5116 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5117 | size_t reply_len = PAGE_SIZE; |
| 5118 | void *p, *end; |
| 5119 | int ret; |
| 5120 | |
| 5121 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 5122 | "rbd", "get_parent", CEPH_OSD_FLAG_READ, |
| 5123 | req_page, sizeof(u64), reply_page, &reply_len); |
| 5124 | if (ret) |
| 5125 | return ret; |
| 5126 | |
| 5127 | p = page_address(reply_page); |
| 5128 | end = p + reply_len; |
| 5129 | ceph_decode_64_safe(&p, end, pii->pool_id, e_inval); |
| 5130 | pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
| 5131 | if (IS_ERR(pii->image_id)) { |
| 5132 | ret = PTR_ERR(pii->image_id); |
| 5133 | pii->image_id = NULL; |
| 5134 | return ret; |
| 5135 | } |
| 5136 | ceph_decode_64_safe(&p, end, pii->snap_id, e_inval); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5137 | pii->has_overlap = true; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5138 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 5139 | |
| 5140 | return 0; |
| 5141 | |
| 5142 | e_inval: |
| 5143 | return -EINVAL; |
| 5144 | } |
| 5145 | |
| 5146 | static int get_parent_info(struct rbd_device *rbd_dev, |
| 5147 | struct parent_image_info *pii) |
| 5148 | { |
| 5149 | struct page *req_page, *reply_page; |
| 5150 | void *p; |
| 5151 | int ret; |
| 5152 | |
| 5153 | req_page = alloc_page(GFP_KERNEL); |
| 5154 | if (!req_page) |
| 5155 | return -ENOMEM; |
| 5156 | |
| 5157 | reply_page = alloc_page(GFP_KERNEL); |
| 5158 | if (!reply_page) { |
| 5159 | __free_page(req_page); |
| 5160 | return -ENOMEM; |
| 5161 | } |
| 5162 | |
| 5163 | p = page_address(req_page); |
| 5164 | ceph_encode_64(&p, rbd_dev->spec->snap_id); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5165 | ret = __get_parent_info(rbd_dev, req_page, reply_page, pii); |
| 5166 | if (ret > 0) |
| 5167 | ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page, |
| 5168 | pii); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5169 | |
| 5170 | __free_page(req_page); |
| 5171 | __free_page(reply_page); |
| 5172 | return ret; |
| 5173 | } |
| 5174 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5175 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 5176 | { |
| 5177 | struct rbd_spec *parent_spec; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5178 | struct parent_image_info pii = { 0 }; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5179 | int ret; |
| 5180 | |
| 5181 | parent_spec = rbd_spec_alloc(); |
| 5182 | if (!parent_spec) |
| 5183 | return -ENOMEM; |
| 5184 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5185 | ret = get_parent_info(rbd_dev, &pii); |
| 5186 | if (ret) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5187 | goto out_err; |
| 5188 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5189 | dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n", |
| 5190 | __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id, |
| 5191 | pii.has_overlap, pii.overlap); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5192 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5193 | if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) { |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5194 | /* |
| 5195 | * Either the parent never existed, or we have |
| 5196 | * record of it but the image got flattened so it no |
| 5197 | * longer has a parent. When the parent of a |
| 5198 | * layered image disappears we immediately set the |
| 5199 | * overlap to 0. The effect of this is that all new |
| 5200 | * requests will be treated as if the image had no |
| 5201 | * parent. |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5202 | * |
| 5203 | * If !pii.has_overlap, the parent image spec is not |
| 5204 | * applicable. It's there to avoid duplication in each |
| 5205 | * snapshot record. |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5206 | */ |
| 5207 | if (rbd_dev->parent_overlap) { |
| 5208 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5209 | rbd_dev_parent_put(rbd_dev); |
| 5210 | pr_info("%s: clone image has been flattened\n", |
| 5211 | rbd_dev->disk->disk_name); |
| 5212 | } |
| 5213 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5214 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5215 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5216 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5217 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5218 | |
| 5219 | ret = -EIO; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5220 | if (pii.pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5221 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5222 | (unsigned long long)pii.pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5223 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5224 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5225 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5226 | /* |
| 5227 | * The parent won't change (except when the clone is |
| 5228 | * flattened, already handled that). So we only need to |
| 5229 | * record the parent spec we have not already done so. |
| 5230 | */ |
| 5231 | if (!rbd_dev->parent_spec) { |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5232 | parent_spec->pool_id = pii.pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5233 | if (pii.pool_ns && *pii.pool_ns) { |
| 5234 | parent_spec->pool_ns = pii.pool_ns; |
| 5235 | pii.pool_ns = NULL; |
| 5236 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5237 | parent_spec->image_id = pii.image_id; |
| 5238 | pii.image_id = NULL; |
| 5239 | parent_spec->snap_id = pii.snap_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5240 | |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5241 | rbd_dev->parent_spec = parent_spec; |
| 5242 | parent_spec = NULL; /* rbd_dev now owns this */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5246 | * We always update the parent overlap. If it's zero we issue |
| 5247 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5248 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5249 | if (!pii.overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5250 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5251 | /* refresh, careful to warn just once */ |
| 5252 | if (rbd_dev->parent_overlap) |
| 5253 | rbd_warn(rbd_dev, |
| 5254 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5255 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5256 | /* initial probe */ |
| 5257 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5258 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5259 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5260 | rbd_dev->parent_overlap = pii.overlap; |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5261 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5262 | out: |
| 5263 | ret = 0; |
| 5264 | out_err: |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5265 | kfree(pii.pool_ns); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5266 | kfree(pii.image_id); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5267 | rbd_spec_put(parent_spec); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5268 | return ret; |
| 5269 | } |
| 5270 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5271 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5272 | { |
| 5273 | struct { |
| 5274 | __le64 stripe_unit; |
| 5275 | __le64 stripe_count; |
| 5276 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5277 | size_t size = sizeof (striping_info_buf); |
| 5278 | void *p; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5279 | int ret; |
| 5280 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5281 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5282 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5283 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5284 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5285 | if (ret < 0) |
| 5286 | return ret; |
| 5287 | if (ret < size) |
| 5288 | return -ERANGE; |
| 5289 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5290 | p = &striping_info_buf; |
Ilya Dryomov | b133185 | 2018-02-07 12:09:12 +0100 | [diff] [blame] | 5291 | rbd_dev->header.stripe_unit = ceph_decode_64(&p); |
| 5292 | rbd_dev->header.stripe_count = ceph_decode_64(&p); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5293 | return 0; |
| 5294 | } |
| 5295 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5296 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5297 | { |
| 5298 | __le64 data_pool_id; |
| 5299 | int ret; |
| 5300 | |
| 5301 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5302 | &rbd_dev->header_oloc, "get_data_pool", |
| 5303 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5304 | if (ret < 0) |
| 5305 | return ret; |
| 5306 | if (ret < sizeof(data_pool_id)) |
| 5307 | return -EBADMSG; |
| 5308 | |
| 5309 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5310 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5311 | return 0; |
| 5312 | } |
| 5313 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5314 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5315 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5316 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5317 | size_t image_id_size; |
| 5318 | char *image_id; |
| 5319 | void *p; |
| 5320 | void *end; |
| 5321 | size_t size; |
| 5322 | void *reply_buf = NULL; |
| 5323 | size_t len = 0; |
| 5324 | char *image_name = NULL; |
| 5325 | int ret; |
| 5326 | |
| 5327 | rbd_assert(!rbd_dev->spec->image_name); |
| 5328 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5329 | len = strlen(rbd_dev->spec->image_id); |
| 5330 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5331 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5332 | if (!image_id) |
| 5333 | return NULL; |
| 5334 | |
| 5335 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5336 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5337 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5338 | |
| 5339 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5340 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5341 | if (!reply_buf) |
| 5342 | goto out; |
| 5343 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5344 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5345 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5346 | "dir_get_name", image_id, image_id_size, |
| 5347 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5348 | if (ret < 0) |
| 5349 | goto out; |
| 5350 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5351 | end = reply_buf + ret; |
| 5352 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5353 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5354 | if (IS_ERR(image_name)) |
| 5355 | image_name = NULL; |
| 5356 | else |
| 5357 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5358 | out: |
| 5359 | kfree(reply_buf); |
| 5360 | kfree(image_id); |
| 5361 | |
| 5362 | return image_name; |
| 5363 | } |
| 5364 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5365 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5366 | { |
| 5367 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5368 | const char *snap_name; |
| 5369 | u32 which = 0; |
| 5370 | |
| 5371 | /* Skip over names until we find the one we are looking for */ |
| 5372 | |
| 5373 | snap_name = rbd_dev->header.snap_names; |
| 5374 | while (which < snapc->num_snaps) { |
| 5375 | if (!strcmp(name, snap_name)) |
| 5376 | return snapc->snaps[which]; |
| 5377 | snap_name += strlen(snap_name) + 1; |
| 5378 | which++; |
| 5379 | } |
| 5380 | return CEPH_NOSNAP; |
| 5381 | } |
| 5382 | |
| 5383 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5384 | { |
| 5385 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5386 | u32 which; |
| 5387 | bool found = false; |
| 5388 | u64 snap_id; |
| 5389 | |
| 5390 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5391 | const char *snap_name; |
| 5392 | |
| 5393 | snap_id = snapc->snaps[which]; |
| 5394 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5395 | if (IS_ERR(snap_name)) { |
| 5396 | /* ignore no-longer existing snapshots */ |
| 5397 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5398 | continue; |
| 5399 | else |
| 5400 | break; |
| 5401 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5402 | found = !strcmp(name, snap_name); |
| 5403 | kfree(snap_name); |
| 5404 | } |
| 5405 | return found ? snap_id : CEPH_NOSNAP; |
| 5406 | } |
| 5407 | |
| 5408 | /* |
| 5409 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5410 | * no snapshot by that name is found, or if an error occurs. |
| 5411 | */ |
| 5412 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5413 | { |
| 5414 | if (rbd_dev->image_format == 1) |
| 5415 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5416 | |
| 5417 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5418 | } |
| 5419 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5420 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5421 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5422 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5423 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5424 | { |
| 5425 | struct rbd_spec *spec = rbd_dev->spec; |
| 5426 | |
| 5427 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5428 | rbd_assert(spec->image_id && spec->image_name); |
| 5429 | rbd_assert(spec->snap_name); |
| 5430 | |
| 5431 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5432 | u64 snap_id; |
| 5433 | |
| 5434 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5435 | if (snap_id == CEPH_NOSNAP) |
| 5436 | return -ENOENT; |
| 5437 | |
| 5438 | spec->snap_id = snap_id; |
| 5439 | } else { |
| 5440 | spec->snap_id = CEPH_NOSNAP; |
| 5441 | } |
| 5442 | |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
| 5446 | /* |
| 5447 | * A parent image will have all ids but none of the names. |
| 5448 | * |
| 5449 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5450 | * can't figure out the name for an image id. |
| 5451 | */ |
| 5452 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5453 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5454 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5455 | struct rbd_spec *spec = rbd_dev->spec; |
| 5456 | const char *pool_name; |
| 5457 | const char *image_name; |
| 5458 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5459 | int ret; |
| 5460 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5461 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5462 | rbd_assert(spec->image_id); |
| 5463 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5464 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5465 | /* 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] | 5466 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5467 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5468 | if (!pool_name) { |
| 5469 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5470 | return -EIO; |
| 5471 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5472 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5473 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5474 | return -ENOMEM; |
| 5475 | |
| 5476 | /* Fetch the image name; tolerate failure here */ |
| 5477 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5478 | image_name = rbd_dev_image_name(rbd_dev); |
| 5479 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5480 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5481 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5482 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5483 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5484 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5485 | if (IS_ERR(snap_name)) { |
| 5486 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5487 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5488 | } |
| 5489 | |
| 5490 | spec->pool_name = pool_name; |
| 5491 | spec->image_name = image_name; |
| 5492 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5493 | |
| 5494 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5495 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5496 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5497 | kfree(image_name); |
| 5498 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5499 | return ret; |
| 5500 | } |
| 5501 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5502 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5503 | { |
| 5504 | size_t size; |
| 5505 | int ret; |
| 5506 | void *reply_buf; |
| 5507 | void *p; |
| 5508 | void *end; |
| 5509 | u64 seq; |
| 5510 | u32 snap_count; |
| 5511 | struct ceph_snap_context *snapc; |
| 5512 | u32 i; |
| 5513 | |
| 5514 | /* |
| 5515 | * We'll need room for the seq value (maximum snapshot id), |
| 5516 | * snapshot count, and array of that many snapshot ids. |
| 5517 | * For now we have a fixed upper limit on the number we're |
| 5518 | * prepared to receive. |
| 5519 | */ |
| 5520 | size = sizeof (__le64) + sizeof (__le32) + |
| 5521 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5522 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5523 | if (!reply_buf) |
| 5524 | return -ENOMEM; |
| 5525 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5526 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5527 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5528 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5529 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5530 | if (ret < 0) |
| 5531 | goto out; |
| 5532 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5533 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5534 | end = reply_buf + ret; |
| 5535 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5536 | ceph_decode_64_safe(&p, end, seq, out); |
| 5537 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5538 | |
| 5539 | /* |
| 5540 | * Make sure the reported number of snapshot ids wouldn't go |
| 5541 | * beyond the end of our buffer. But before checking that, |
| 5542 | * make sure the computed size of the snapshot context we |
| 5543 | * allocate is representable in a size_t. |
| 5544 | */ |
| 5545 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5546 | / sizeof (u64)) { |
| 5547 | ret = -EINVAL; |
| 5548 | goto out; |
| 5549 | } |
| 5550 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5551 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5552 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5553 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5554 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5555 | if (!snapc) { |
| 5556 | ret = -ENOMEM; |
| 5557 | goto out; |
| 5558 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5559 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5560 | for (i = 0; i < snap_count; i++) |
| 5561 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5562 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5563 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5564 | rbd_dev->header.snapc = snapc; |
| 5565 | |
| 5566 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5567 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5568 | out: |
| 5569 | kfree(reply_buf); |
| 5570 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5571 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5572 | } |
| 5573 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5574 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5575 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5576 | { |
| 5577 | size_t size; |
| 5578 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5579 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5580 | int ret; |
| 5581 | void *p; |
| 5582 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5583 | char *snap_name; |
| 5584 | |
| 5585 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5586 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5587 | if (!reply_buf) |
| 5588 | return ERR_PTR(-ENOMEM); |
| 5589 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5590 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5591 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5592 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5593 | &snapid, sizeof(snapid), reply_buf, size); |
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 | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5595 | if (ret < 0) { |
| 5596 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5597 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5598 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5599 | |
| 5600 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5601 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5602 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5603 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5604 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5605 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5606 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5607 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5608 | out: |
| 5609 | kfree(reply_buf); |
| 5610 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5611 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5612 | } |
| 5613 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5614 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5615 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5616 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5617 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5618 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5619 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5620 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5621 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5622 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5623 | if (first_time) { |
| 5624 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5625 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5626 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5627 | } |
| 5628 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5629 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5630 | if (ret && first_time) { |
| 5631 | kfree(rbd_dev->header.object_prefix); |
| 5632 | rbd_dev->header.object_prefix = NULL; |
| 5633 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5634 | |
| 5635 | return ret; |
| 5636 | } |
| 5637 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5638 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5639 | { |
| 5640 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5641 | |
| 5642 | if (rbd_dev->image_format == 1) |
| 5643 | return rbd_dev_v1_header_info(rbd_dev); |
| 5644 | |
| 5645 | return rbd_dev_v2_header_info(rbd_dev); |
| 5646 | } |
| 5647 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5648 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5649 | * Skips over white space at *buf, and updates *buf to point to the |
| 5650 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5651 | * the token (string of non-white space characters) found. Note |
| 5652 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5653 | */ |
| 5654 | static inline size_t next_token(const char **buf) |
| 5655 | { |
| 5656 | /* |
| 5657 | * These are the characters that produce nonzero for |
| 5658 | * isspace() in the "C" and "POSIX" locales. |
| 5659 | */ |
| 5660 | const char *spaces = " \f\n\r\t\v"; |
| 5661 | |
| 5662 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5663 | |
| 5664 | return strcspn(*buf, spaces); /* Return token length */ |
| 5665 | } |
| 5666 | |
| 5667 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5668 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5669 | * enough to hold a copy of it, and copies the token into the new |
| 5670 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5671 | * that a duplicate buffer is created even for a zero-length token. |
| 5672 | * |
| 5673 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5674 | * pointer if memory for the duplicate was not available. If |
| 5675 | * the lenp argument is a non-null pointer, the length of the token |
| 5676 | * (not including the '\0') is returned in *lenp. |
| 5677 | * |
| 5678 | * If successful, the *buf pointer will be updated to point beyond |
| 5679 | * the end of the found token. |
| 5680 | * |
| 5681 | * Note: uses GFP_KERNEL for allocation. |
| 5682 | */ |
| 5683 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5684 | { |
| 5685 | char *dup; |
| 5686 | size_t len; |
| 5687 | |
| 5688 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5689 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5690 | if (!dup) |
| 5691 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5692 | *(dup + len) = '\0'; |
| 5693 | *buf += len; |
| 5694 | |
| 5695 | if (lenp) |
| 5696 | *lenp = len; |
| 5697 | |
| 5698 | return dup; |
| 5699 | } |
| 5700 | |
| 5701 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5702 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5703 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5704 | * and the data written is passed here via a NUL-terminated buffer. |
| 5705 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5706 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5707 | * The information extracted from these options is recorded in |
| 5708 | * the other parameters which return dynamically-allocated |
| 5709 | * structures: |
| 5710 | * ceph_opts |
| 5711 | * The address of a pointer that will refer to a ceph options |
| 5712 | * structure. Caller must release the returned pointer using |
| 5713 | * ceph_destroy_options() when it is no longer needed. |
| 5714 | * rbd_opts |
| 5715 | * Address of an rbd options pointer. Fully initialized by |
| 5716 | * this function; caller must release with kfree(). |
| 5717 | * spec |
| 5718 | * Address of an rbd image specification pointer. Fully |
| 5719 | * initialized by this function based on parsed options. |
| 5720 | * Caller must release with rbd_spec_put(). |
| 5721 | * |
| 5722 | * The options passed take this form: |
| 5723 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5724 | * where: |
| 5725 | * <mon_addrs> |
| 5726 | * A comma-separated list of one or more monitor addresses. |
| 5727 | * A monitor address is an ip address, optionally followed |
| 5728 | * by a port number (separated by a colon). |
| 5729 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5730 | * <options> |
| 5731 | * A comma-separated list of ceph and/or rbd options. |
| 5732 | * <pool_name> |
| 5733 | * The name of the rados pool containing the rbd image. |
| 5734 | * <image_name> |
| 5735 | * The name of the image in that pool to map. |
| 5736 | * <snap_id> |
| 5737 | * An optional snapshot id. If provided, the mapping will |
| 5738 | * present data from the image at the time that snapshot was |
| 5739 | * created. The image head is used if no snapshot id is |
| 5740 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5741 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5742 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5743 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5744 | struct rbd_options **opts, |
| 5745 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5746 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5747 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5748 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5749 | const char *mon_addrs; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5750 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5751 | size_t mon_addrs_size; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5752 | struct parse_rbd_opts_ctx pctx = { 0 }; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5753 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5754 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5755 | |
| 5756 | /* The first four tokens are required */ |
| 5757 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5758 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5759 | if (!len) { |
| 5760 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5761 | return -EINVAL; |
| 5762 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5763 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5764 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5765 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5766 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5767 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5768 | options = dup_token(&buf, NULL); |
| 5769 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5770 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5771 | if (!*options) { |
| 5772 | rbd_warn(NULL, "no options provided"); |
| 5773 | goto out_err; |
| 5774 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5775 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5776 | pctx.spec = rbd_spec_alloc(); |
| 5777 | if (!pctx.spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5778 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5779 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5780 | pctx.spec->pool_name = dup_token(&buf, NULL); |
| 5781 | if (!pctx.spec->pool_name) |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5782 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5783 | if (!*pctx.spec->pool_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5784 | rbd_warn(NULL, "no pool name provided"); |
| 5785 | goto out_err; |
| 5786 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5787 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5788 | pctx.spec->image_name = dup_token(&buf, NULL); |
| 5789 | if (!pctx.spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5790 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5791 | if (!*pctx.spec->image_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5792 | rbd_warn(NULL, "no image name provided"); |
| 5793 | goto out_err; |
| 5794 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5795 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5796 | /* |
| 5797 | * Snapshot name is optional; default is to use "-" |
| 5798 | * (indicating the head/no snapshot). |
| 5799 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5800 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5801 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5802 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5803 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5804 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5805 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5806 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5807 | } |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5808 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5809 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5810 | goto out_mem; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5811 | *(snap_name + len) = '\0'; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5812 | pctx.spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5813 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5814 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5815 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5816 | pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL); |
| 5817 | if (!pctx.opts) |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5818 | goto out_mem; |
| 5819 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5820 | pctx.opts->read_only = RBD_READ_ONLY_DEFAULT; |
| 5821 | pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 5822 | pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5823 | pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT; |
| 5824 | pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
| 5825 | pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
| 5826 | pctx.opts->trim = RBD_TRIM_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5827 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5828 | copts = ceph_parse_options(options, mon_addrs, |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5829 | mon_addrs + mon_addrs_size - 1, |
| 5830 | parse_rbd_opts_token, &pctx); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5831 | if (IS_ERR(copts)) { |
| 5832 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5833 | goto out_err; |
| 5834 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5835 | kfree(options); |
| 5836 | |
| 5837 | *ceph_opts = copts; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5838 | *opts = pctx.opts; |
| 5839 | *rbd_spec = pctx.spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5840 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5841 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5842 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5843 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5844 | out_err: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5845 | kfree(pctx.opts); |
| 5846 | rbd_spec_put(pctx.spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5847 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5848 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5849 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5850 | } |
| 5851 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5852 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5853 | { |
| 5854 | down_write(&rbd_dev->lock_rwsem); |
| 5855 | if (__rbd_is_lock_owner(rbd_dev)) |
Ilya Dryomov | e1fddc8 | 2019-05-30 16:07:48 +0200 | [diff] [blame^] | 5856 | __rbd_release_lock(rbd_dev); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5857 | up_write(&rbd_dev->lock_rwsem); |
| 5858 | } |
| 5859 | |
| 5860 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5861 | { |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5862 | int ret; |
| 5863 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5864 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5865 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5866 | return -EINVAL; |
| 5867 | } |
| 5868 | |
| 5869 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5870 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5871 | ret = rbd_wait_state_locked(rbd_dev, true); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5872 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5873 | if (ret) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5874 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5875 | return -EROFS; |
| 5876 | } |
| 5877 | |
| 5878 | return 0; |
| 5879 | } |
| 5880 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5881 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5882 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5883 | * name given to it by the user. Internally, that identifier is |
| 5884 | * what's used to specify the names of objects related to the image. |
| 5885 | * |
| 5886 | * A special "rbd id" object is used to map an rbd image name to its |
| 5887 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5888 | * with the supplied name. |
| 5889 | * |
| 5890 | * This function will record the given rbd_dev's image_id field if |
| 5891 | * it can be determined, and in that case will return 0. If any |
| 5892 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5893 | * image_id field will be unchanged (and should be NULL). |
| 5894 | */ |
| 5895 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5896 | { |
| 5897 | int ret; |
| 5898 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5899 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5900 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5901 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5902 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5903 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5904 | * When probing a parent image, the image id is already |
| 5905 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5906 | * need to fetch the image id again in this case. We |
| 5907 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5908 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5909 | if (rbd_dev->spec->image_id) { |
| 5910 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5911 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5912 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5913 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5914 | |
| 5915 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5916 | * First, see if the format 2 image id file exists, and if |
| 5917 | * so, get the image's persistent id from it. |
| 5918 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5919 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5920 | rbd_dev->spec->image_name); |
| 5921 | if (ret) |
| 5922 | return ret; |
| 5923 | |
| 5924 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5925 | |
| 5926 | /* Response will be an encoded string, which includes a length */ |
| 5927 | |
| 5928 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5929 | response = kzalloc(size, GFP_NOIO); |
| 5930 | if (!response) { |
| 5931 | ret = -ENOMEM; |
| 5932 | goto out; |
| 5933 | } |
| 5934 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5935 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5936 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5937 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5938 | "get_id", NULL, 0, |
| 5939 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5940 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5941 | if (ret == -ENOENT) { |
| 5942 | image_id = kstrdup("", GFP_KERNEL); |
| 5943 | ret = image_id ? 0 : -ENOMEM; |
| 5944 | if (!ret) |
| 5945 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5946 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5947 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5948 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5949 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5950 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5951 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5952 | if (!ret) |
| 5953 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5954 | } |
| 5955 | |
| 5956 | if (!ret) { |
| 5957 | rbd_dev->spec->image_id = image_id; |
| 5958 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5959 | } |
| 5960 | out: |
| 5961 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5962 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5963 | return ret; |
| 5964 | } |
| 5965 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5966 | /* |
| 5967 | * Undo whatever state changes are made by v1 or v2 header info |
| 5968 | * call. |
| 5969 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5970 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5971 | { |
| 5972 | struct rbd_image_header *header; |
| 5973 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5974 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5975 | |
| 5976 | /* Free dynamic fields from the header, then zero it out */ |
| 5977 | |
| 5978 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5979 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5980 | kfree(header->snap_sizes); |
| 5981 | kfree(header->snap_names); |
| 5982 | kfree(header->object_prefix); |
| 5983 | memset(header, 0, sizeof (*header)); |
| 5984 | } |
| 5985 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5986 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5987 | { |
| 5988 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5989 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5990 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5991 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5992 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5993 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5994 | /* |
| 5995 | * Get the and check features for the image. Currently the |
| 5996 | * features are assumed to never change. |
| 5997 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5998 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5999 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 6000 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 6001 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 6002 | /* If the image supports fancy striping, get its parameters */ |
| 6003 | |
| 6004 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 6005 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 6006 | if (ret < 0) |
| 6007 | goto out_err; |
| 6008 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6009 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 6010 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 6011 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 6012 | if (ret) |
| 6013 | goto out_err; |
| 6014 | } |
| 6015 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 6016 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 6017 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 6018 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 6019 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6020 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 6021 | kfree(rbd_dev->header.object_prefix); |
| 6022 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 6023 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6024 | } |
| 6025 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6026 | /* |
| 6027 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 6028 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 6029 | * length of the already discovered part of the parent chain. |
| 6030 | */ |
| 6031 | 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] | 6032 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 6033 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6034 | int ret; |
| 6035 | |
| 6036 | if (!rbd_dev->parent_spec) |
| 6037 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6038 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6039 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 6040 | pr_info("parent chain is too long (%d)\n", depth); |
| 6041 | ret = -EINVAL; |
| 6042 | goto out_err; |
| 6043 | } |
| 6044 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6045 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6046 | if (!parent) { |
| 6047 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6048 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | /* |
| 6052 | * Images related by parent/child relationships always share |
| 6053 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 6054 | */ |
| 6055 | __rbd_get_client(rbd_dev->rbd_client); |
| 6056 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6057 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6058 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6059 | if (ret < 0) |
| 6060 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6061 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6062 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 6063 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6064 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6065 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6066 | out_err: |
| 6067 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 6068 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6069 | return ret; |
| 6070 | } |
| 6071 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6072 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 6073 | { |
| 6074 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 6075 | rbd_dev_mapping_clear(rbd_dev); |
| 6076 | rbd_free_disk(rbd_dev); |
| 6077 | if (!single_major) |
| 6078 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 6079 | } |
| 6080 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6081 | /* |
| 6082 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 6083 | * upon return. |
| 6084 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6085 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6086 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6087 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6088 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6089 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6090 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6091 | if (!single_major) { |
| 6092 | ret = register_blkdev(0, rbd_dev->name); |
| 6093 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6094 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6095 | |
| 6096 | rbd_dev->major = ret; |
| 6097 | rbd_dev->minor = 0; |
| 6098 | } else { |
| 6099 | rbd_dev->major = rbd_major; |
| 6100 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 6101 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6102 | |
| 6103 | /* Set up the blkdev mapping. */ |
| 6104 | |
| 6105 | ret = rbd_init_disk(rbd_dev); |
| 6106 | if (ret) |
| 6107 | goto err_out_blkdev; |
| 6108 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6109 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6110 | if (ret) |
| 6111 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 6112 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6113 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 6114 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6115 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6116 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6117 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6118 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6119 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6120 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6121 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6122 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 6123 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6124 | err_out_mapping: |
| 6125 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6126 | err_out_disk: |
| 6127 | rbd_free_disk(rbd_dev); |
| 6128 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6129 | if (!single_major) |
| 6130 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6131 | err_out_unlock: |
| 6132 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6133 | return ret; |
| 6134 | } |
| 6135 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6136 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 6137 | { |
| 6138 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6139 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6140 | |
| 6141 | /* Record the header object name for this rbd image. */ |
| 6142 | |
| 6143 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6144 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6145 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6146 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6147 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6148 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6149 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6150 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6151 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6152 | } |
| 6153 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6154 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6155 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6156 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6157 | if (rbd_dev->opts) |
| 6158 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6159 | rbd_dev->image_format = 0; |
| 6160 | kfree(rbd_dev->spec->image_id); |
| 6161 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6162 | } |
| 6163 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6164 | /* |
| 6165 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6166 | * device. If this image is the one being mapped (i.e., not a |
| 6167 | * parent), initiate a watch on its header object before using that |
| 6168 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6169 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6170 | 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] | 6171 | { |
| 6172 | int ret; |
| 6173 | |
| 6174 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6175 | * Get the id from the image id object. Unless there's an |
| 6176 | * error, rbd_dev->spec->image_id will be filled in with |
| 6177 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6178 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6179 | */ |
| 6180 | ret = rbd_dev_image_id(rbd_dev); |
| 6181 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6182 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6183 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6184 | ret = rbd_dev_header_name(rbd_dev); |
| 6185 | if (ret) |
| 6186 | goto err_out_format; |
| 6187 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6188 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6189 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6190 | if (ret) { |
| 6191 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 6192 | pr_info("image %s/%s%s%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6193 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 6194 | rbd_dev->spec->pool_ns ?: "", |
| 6195 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6196 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6197 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6198 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6199 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6200 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6201 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6202 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6203 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6204 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6205 | /* |
| 6206 | * If this image is the one being mapped, we have pool name and |
| 6207 | * id, image name and id, and snap name - need to fill snap id. |
| 6208 | * Otherwise this is a parent image, identified by pool, image |
| 6209 | * and snap ids - need to fill in names for those ids. |
| 6210 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6211 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6212 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6213 | else |
| 6214 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6215 | if (ret) { |
| 6216 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 6217 | pr_info("snap %s/%s%s%s@%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6218 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 6219 | rbd_dev->spec->pool_ns ?: "", |
| 6220 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6221 | rbd_dev->spec->image_name, |
| 6222 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6223 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6224 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6225 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6226 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6227 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6228 | if (ret) |
| 6229 | goto err_out_probe; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6230 | } |
| 6231 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6232 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6233 | if (ret) |
| 6234 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6235 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6236 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6237 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6238 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6239 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6240 | err_out_probe: |
| 6241 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6242 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6243 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6244 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6245 | err_out_format: |
| 6246 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6247 | kfree(rbd_dev->spec->image_id); |
| 6248 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6249 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6250 | } |
| 6251 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6252 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6253 | const char *buf, |
| 6254 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6255 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6256 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6257 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6258 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6259 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6260 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6261 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6262 | |
| 6263 | if (!try_module_get(THIS_MODULE)) |
| 6264 | return -ENODEV; |
| 6265 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6266 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6267 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6268 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6269 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6270 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6271 | rbdc = rbd_get_client(ceph_opts); |
| 6272 | if (IS_ERR(rbdc)) { |
| 6273 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6274 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6275 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6276 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6277 | /* pick the pool */ |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 6278 | 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] | 6279 | if (rc < 0) { |
| 6280 | if (rc == -ENOENT) |
| 6281 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6282 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6283 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6284 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6285 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6286 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6287 | if (!rbd_dev) { |
| 6288 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6289 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6290 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6291 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6292 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6293 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6294 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6295 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6296 | if (!rbd_dev->config_info) { |
| 6297 | rc = -ENOMEM; |
| 6298 | goto err_out_rbd_dev; |
| 6299 | } |
| 6300 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6301 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6302 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6303 | if (rc < 0) { |
| 6304 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6305 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6306 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6307 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6308 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6309 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 6310 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6311 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 6312 | if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) { |
| 6313 | rbd_warn(rbd_dev, "alloc_size adjusted to %u", |
| 6314 | rbd_dev->layout.object_size); |
| 6315 | rbd_dev->opts->alloc_size = rbd_dev->layout.object_size; |
| 6316 | } |
| 6317 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6318 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6319 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6320 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6321 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6322 | if (rbd_dev->opts->exclusive) { |
| 6323 | rc = rbd_add_acquire_lock(rbd_dev); |
| 6324 | if (rc) |
| 6325 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6326 | } |
| 6327 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6328 | /* Everything's ready. Announce the disk to the world. */ |
| 6329 | |
| 6330 | rc = device_add(&rbd_dev->dev); |
| 6331 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6332 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6333 | |
| 6334 | add_disk(rbd_dev->disk); |
| 6335 | /* see rbd_init_disk() */ |
| 6336 | blk_put_queue(rbd_dev->disk->queue); |
| 6337 | |
| 6338 | spin_lock(&rbd_dev_list_lock); |
| 6339 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6340 | spin_unlock(&rbd_dev_list_lock); |
| 6341 | |
| 6342 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6343 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6344 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6345 | rc = count; |
| 6346 | out: |
| 6347 | module_put(THIS_MODULE); |
| 6348 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6349 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6350 | err_out_image_lock: |
| 6351 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6352 | err_out_device_setup: |
| 6353 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6354 | err_out_image_probe: |
| 6355 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6356 | err_out_rbd_dev: |
| 6357 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6358 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6359 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6360 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6361 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6362 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6363 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6364 | } |
| 6365 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6366 | static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6367 | { |
| 6368 | if (single_major) |
| 6369 | return -EINVAL; |
| 6370 | |
| 6371 | return do_rbd_add(bus, buf, count); |
| 6372 | } |
| 6373 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6374 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, |
| 6375 | size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6376 | { |
| 6377 | return do_rbd_add(bus, buf, count); |
| 6378 | } |
| 6379 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6380 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6381 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6382 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6383 | struct rbd_device *first = rbd_dev; |
| 6384 | struct rbd_device *second = first->parent; |
| 6385 | struct rbd_device *third; |
| 6386 | |
| 6387 | /* |
| 6388 | * Follow to the parent with no grandparent and |
| 6389 | * remove it. |
| 6390 | */ |
| 6391 | while (second && (third = second->parent)) { |
| 6392 | first = second; |
| 6393 | second = third; |
| 6394 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6395 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6396 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6397 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6398 | first->parent = NULL; |
| 6399 | first->parent_overlap = 0; |
| 6400 | |
| 6401 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6402 | rbd_spec_put(first->parent_spec); |
| 6403 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6404 | } |
| 6405 | } |
| 6406 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6407 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6408 | const char *buf, |
| 6409 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6410 | { |
| 6411 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6412 | struct list_head *tmp; |
| 6413 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6414 | char opt_buf[6]; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6415 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6416 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6417 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6418 | dev_id = -1; |
| 6419 | opt_buf[0] = '\0'; |
| 6420 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6421 | if (dev_id < 0) { |
| 6422 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6423 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6424 | } |
| 6425 | if (opt_buf[0] != '\0') { |
| 6426 | if (!strcmp(opt_buf, "force")) { |
| 6427 | force = true; |
| 6428 | } else { |
| 6429 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6430 | return -EINVAL; |
| 6431 | } |
| 6432 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6433 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6434 | ret = -ENOENT; |
| 6435 | spin_lock(&rbd_dev_list_lock); |
| 6436 | list_for_each(tmp, &rbd_dev_list) { |
| 6437 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6438 | if (rbd_dev->dev_id == dev_id) { |
| 6439 | ret = 0; |
| 6440 | break; |
| 6441 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6442 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6443 | if (!ret) { |
| 6444 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6445 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6446 | ret = -EBUSY; |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6447 | else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6448 | &rbd_dev->flags)) |
| 6449 | ret = -EINPROGRESS; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6450 | spin_unlock_irq(&rbd_dev->lock); |
| 6451 | } |
| 6452 | spin_unlock(&rbd_dev_list_lock); |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6453 | if (ret) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6454 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6455 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6456 | if (force) { |
| 6457 | /* |
| 6458 | * Prevent new IO from being queued and wait for existing |
| 6459 | * IO to complete/fail. |
| 6460 | */ |
| 6461 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6462 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6463 | } |
| 6464 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6465 | del_gendisk(rbd_dev->disk); |
| 6466 | spin_lock(&rbd_dev_list_lock); |
| 6467 | list_del_init(&rbd_dev->node); |
| 6468 | spin_unlock(&rbd_dev_list_lock); |
| 6469 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6470 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6471 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6472 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6473 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6474 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6475 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6476 | } |
| 6477 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6478 | static ssize_t remove_store(struct bus_type *bus, const char *buf, size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6479 | { |
| 6480 | if (single_major) |
| 6481 | return -EINVAL; |
| 6482 | |
| 6483 | return do_rbd_remove(bus, buf, count); |
| 6484 | } |
| 6485 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6486 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, |
| 6487 | size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6488 | { |
| 6489 | return do_rbd_remove(bus, buf, count); |
| 6490 | } |
| 6491 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6492 | /* |
| 6493 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6494 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6495 | */ |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6496 | static int __init rbd_sysfs_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6497 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6498 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6499 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6500 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6501 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6502 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6503 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6504 | ret = bus_register(&rbd_bus_type); |
| 6505 | if (ret < 0) |
| 6506 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6507 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6508 | return ret; |
| 6509 | } |
| 6510 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6511 | static void __exit rbd_sysfs_cleanup(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6512 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6513 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6514 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6515 | } |
| 6516 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6517 | static int __init rbd_slab_init(void) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6518 | { |
| 6519 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6520 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6521 | if (!rbd_img_request_cache) |
| 6522 | return -ENOMEM; |
| 6523 | |
| 6524 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6525 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6526 | if (!rbd_obj_request_cache) |
| 6527 | goto out_err; |
| 6528 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6529 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6530 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6531 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6532 | kmem_cache_destroy(rbd_img_request_cache); |
| 6533 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6534 | return -ENOMEM; |
| 6535 | } |
| 6536 | |
| 6537 | static void rbd_slab_exit(void) |
| 6538 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6539 | rbd_assert(rbd_obj_request_cache); |
| 6540 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6541 | rbd_obj_request_cache = NULL; |
| 6542 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6543 | rbd_assert(rbd_img_request_cache); |
| 6544 | kmem_cache_destroy(rbd_img_request_cache); |
| 6545 | rbd_img_request_cache = NULL; |
| 6546 | } |
| 6547 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6548 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6549 | { |
| 6550 | int rc; |
| 6551 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6552 | if (!libceph_compatible(NULL)) { |
| 6553 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6554 | return -EINVAL; |
| 6555 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6556 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6557 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6558 | if (rc) |
| 6559 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6560 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6561 | /* |
| 6562 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6563 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6564 | */ |
| 6565 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6566 | if (!rbd_wq) { |
| 6567 | rc = -ENOMEM; |
| 6568 | goto err_out_slab; |
| 6569 | } |
| 6570 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6571 | if (single_major) { |
| 6572 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6573 | if (rbd_major < 0) { |
| 6574 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6575 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6576 | } |
| 6577 | } |
| 6578 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6579 | rc = rbd_sysfs_init(); |
| 6580 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6581 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6582 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6583 | if (single_major) |
| 6584 | pr_info("loaded (major %d)\n", rbd_major); |
| 6585 | else |
| 6586 | pr_info("loaded\n"); |
| 6587 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6588 | return 0; |
| 6589 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6590 | err_out_blkdev: |
| 6591 | if (single_major) |
| 6592 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6593 | err_out_wq: |
| 6594 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6595 | err_out_slab: |
| 6596 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6597 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6598 | } |
| 6599 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6600 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6601 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6602 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6603 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6604 | if (single_major) |
| 6605 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6606 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6607 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6608 | } |
| 6609 | |
| 6610 | module_init(rbd_init); |
| 6611 | module_exit(rbd_exit); |
| 6612 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6613 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6614 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6615 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6616 | /* following authorship retained from original osdblk.c */ |
| 6617 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6618 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6619 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6620 | MODULE_LICENSE("GPL"); |