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 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 206 | struct rbd_img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 207 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 208 | enum obj_request_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 209 | OBJ_REQUEST_NODATA = 1, |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 210 | OBJ_REQUEST_BIO, /* pointer into provided bio (list) */ |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 211 | OBJ_REQUEST_BVECS, /* pointer into provided bio_vec array */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 212 | OBJ_REQUEST_OWN_BVECS, /* private bio_vec array, doesn't own pages */ |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 213 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 214 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 215 | enum obj_operation_type { |
Ilya Dryomov | a1fbb5e | 2018-01-16 12:15:02 +0100 | [diff] [blame] | 216 | OBJ_OP_READ = 1, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 217 | OBJ_OP_WRITE, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 218 | OBJ_OP_DISCARD, |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 219 | OBJ_OP_ZEROOUT, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 220 | }; |
| 221 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 222 | /* |
| 223 | * Writes go through the following state machine to deal with |
| 224 | * layering: |
| 225 | * |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 226 | * . . . . . RBD_OBJ_WRITE_GUARD. . . . . . . . . . . . . . |
| 227 | * . | . |
| 228 | * . v . |
| 229 | * . RBD_OBJ_WRITE_READ_FROM_PARENT. . . . |
| 230 | * . | . . |
| 231 | * . v v (deep-copyup . |
| 232 | * (image . RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC . not needed) . |
| 233 | * flattened) v | . . |
| 234 | * . v . . |
| 235 | * . . . .RBD_OBJ_WRITE_COPYUP_OPS. . . . . (copyup . |
| 236 | * | not needed) v |
| 237 | * v . |
| 238 | * done . . . . . . . . . . . . . . . . . . |
| 239 | * ^ |
| 240 | * | |
| 241 | * RBD_OBJ_WRITE_FLAT |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 242 | * |
| 243 | * Writes start in RBD_OBJ_WRITE_GUARD or _FLAT, depending on whether |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 244 | * assert_exists guard is needed or not (in some cases it's not needed |
| 245 | * even if there is a parent). |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 246 | */ |
| 247 | enum rbd_obj_write_state { |
| 248 | RBD_OBJ_WRITE_FLAT = 1, |
| 249 | RBD_OBJ_WRITE_GUARD, |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 250 | RBD_OBJ_WRITE_READ_FROM_PARENT, |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 251 | RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC, |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 252 | RBD_OBJ_WRITE_COPYUP_OPS, |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 253 | }; |
| 254 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 255 | struct rbd_obj_request { |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 256 | struct ceph_object_extent ex; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 257 | union { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 258 | bool tried_parent; /* for reads */ |
| 259 | enum rbd_obj_write_state write_state; /* for writes */ |
| 260 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 261 | |
Ilya Dryomov | 51c3509 | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 262 | struct rbd_img_request *img_request; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 263 | struct ceph_file_extent *img_extents; |
| 264 | u32 num_img_extents; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 265 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 266 | union { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 267 | struct ceph_bio_iter bio_pos; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 268 | struct { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 269 | struct ceph_bvec_iter bvec_pos; |
| 270 | u32 bvec_count; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 271 | u32 bvec_idx; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 272 | }; |
| 273 | }; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 274 | struct bio_vec *copyup_bvecs; |
| 275 | u32 copyup_bvec_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 276 | |
| 277 | struct ceph_osd_request *osd_req; |
| 278 | |
| 279 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 280 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 281 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 282 | struct kref kref; |
| 283 | }; |
| 284 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 285 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 286 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 287 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 288 | }; |
| 289 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 290 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 291 | struct rbd_device *rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 292 | enum obj_operation_type op_type; |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 293 | enum obj_request_type data_type; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 294 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 295 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 296 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 297 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 298 | }; |
| 299 | union { |
| 300 | struct request *rq; /* block request */ |
| 301 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 302 | }; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 303 | spinlock_t completion_lock; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 304 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 305 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 306 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 307 | struct list_head object_extents; /* obj_req.ex structs */ |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 308 | u32 pending_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 309 | |
| 310 | struct kref kref; |
| 311 | }; |
| 312 | |
| 313 | #define for_each_obj_request(ireq, oreq) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 314 | list_for_each_entry(oreq, &(ireq)->object_extents, ex.oe_item) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 315 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 316 | 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] | 317 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 318 | enum rbd_watch_state { |
| 319 | RBD_WATCH_STATE_UNREGISTERED, |
| 320 | RBD_WATCH_STATE_REGISTERED, |
| 321 | RBD_WATCH_STATE_ERROR, |
| 322 | }; |
| 323 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 324 | enum rbd_lock_state { |
| 325 | RBD_LOCK_STATE_UNLOCKED, |
| 326 | RBD_LOCK_STATE_LOCKED, |
| 327 | RBD_LOCK_STATE_RELEASING, |
| 328 | }; |
| 329 | |
| 330 | /* WatchNotify::ClientId */ |
| 331 | struct rbd_client_id { |
| 332 | u64 gid; |
| 333 | u64 handle; |
| 334 | }; |
| 335 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 336 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 337 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 338 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 339 | }; |
| 340 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 341 | /* |
| 342 | * a single device |
| 343 | */ |
| 344 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 345 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 346 | |
| 347 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 348 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 349 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 350 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 351 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 352 | struct rbd_client *rbd_client; |
| 353 | |
| 354 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 355 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 356 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 357 | |
| 358 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 359 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 360 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 361 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 362 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 363 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 364 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 365 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 366 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 367 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 368 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 369 | struct mutex watch_mutex; |
| 370 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 371 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 372 | u64 watch_cookie; |
| 373 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 374 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 375 | struct rw_semaphore lock_rwsem; |
| 376 | enum rbd_lock_state lock_state; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 377 | char lock_cookie[32]; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 378 | struct rbd_client_id owner_cid; |
| 379 | struct work_struct acquired_lock_work; |
| 380 | struct work_struct released_lock_work; |
| 381 | struct delayed_work lock_dwork; |
| 382 | struct work_struct unlock_work; |
| 383 | wait_queue_head_t lock_waitq; |
| 384 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 385 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 386 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 387 | struct rbd_spec *parent_spec; |
| 388 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 389 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 390 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 391 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 392 | /* Block layer tags. */ |
| 393 | struct blk_mq_tag_set tag_set; |
| 394 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 395 | /* protects updating the header */ |
| 396 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 397 | |
| 398 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 399 | |
| 400 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 401 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 402 | /* sysfs related */ |
| 403 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 404 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 405 | }; |
| 406 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 407 | /* |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 408 | * Flag bits for rbd_dev->flags: |
| 409 | * - REMOVING (which is coupled with rbd_dev->open_count) is protected |
| 410 | * by rbd_dev->lock |
| 411 | * - BLACKLISTED is protected by rbd_dev->lock_rwsem |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 412 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 413 | enum rbd_dev_flags { |
| 414 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 415 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 416 | RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 417 | }; |
| 418 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 419 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 420 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 421 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 422 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 423 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 424 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 425 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 426 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 427 | /* Slab caches for frequently-allocated structures */ |
| 428 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 429 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 430 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 431 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 432 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 433 | static DEFINE_IDA(rbd_dev_id_ida); |
| 434 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 435 | static struct workqueue_struct *rbd_wq; |
| 436 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 437 | static struct ceph_snap_context rbd_empty_snapc = { |
| 438 | .nref = REFCOUNT_INIT(1), |
| 439 | }; |
| 440 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 441 | /* |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 442 | * single-major requires >= 0.75 version of userspace rbd utility. |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 443 | */ |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 444 | static bool single_major = true; |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 445 | module_param(single_major, bool, 0444); |
Ilya Dryomov | 3cfa3b1 | 2017-11-13 10:35:40 +0100 | [diff] [blame] | 446 | 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] | 447 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 448 | static ssize_t add_store(struct bus_type *bus, const char *buf, size_t count); |
| 449 | static ssize_t remove_store(struct bus_type *bus, const char *buf, |
| 450 | size_t count); |
| 451 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, |
| 452 | size_t count); |
| 453 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, |
| 454 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 455 | 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] | 456 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 457 | static int rbd_dev_id_to_minor(int dev_id) |
| 458 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 459 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static int minor_to_rbd_dev_id(int minor) |
| 463 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 464 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 465 | } |
| 466 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 467 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 468 | { |
| 469 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 470 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 471 | } |
| 472 | |
| 473 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 474 | { |
| 475 | bool is_lock_owner; |
| 476 | |
| 477 | down_read(&rbd_dev->lock_rwsem); |
| 478 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 479 | up_read(&rbd_dev->lock_rwsem); |
| 480 | return is_lock_owner; |
| 481 | } |
| 482 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 483 | static ssize_t supported_features_show(struct bus_type *bus, char *buf) |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 484 | { |
| 485 | return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED); |
| 486 | } |
| 487 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 488 | static BUS_ATTR_WO(add); |
| 489 | static BUS_ATTR_WO(remove); |
| 490 | static BUS_ATTR_WO(add_single_major); |
| 491 | static BUS_ATTR_WO(remove_single_major); |
| 492 | static BUS_ATTR_RO(supported_features); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 493 | |
| 494 | static struct attribute *rbd_bus_attrs[] = { |
| 495 | &bus_attr_add.attr, |
| 496 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 497 | &bus_attr_add_single_major.attr, |
| 498 | &bus_attr_remove_single_major.attr, |
Ilya Dryomov | 8767b29 | 2017-03-02 19:56:57 +0100 | [diff] [blame] | 499 | &bus_attr_supported_features.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 500 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 501 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 502 | |
| 503 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 504 | struct attribute *attr, int index) |
| 505 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 506 | if (!single_major && |
| 507 | (attr == &bus_attr_add_single_major.attr || |
| 508 | attr == &bus_attr_remove_single_major.attr)) |
| 509 | return 0; |
| 510 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 511 | return attr->mode; |
| 512 | } |
| 513 | |
| 514 | static const struct attribute_group rbd_bus_group = { |
| 515 | .attrs = rbd_bus_attrs, |
| 516 | .is_visible = rbd_bus_is_visible, |
| 517 | }; |
| 518 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 519 | |
| 520 | static struct bus_type rbd_bus_type = { |
| 521 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 522 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | static void rbd_root_dev_release(struct device *dev) |
| 526 | { |
| 527 | } |
| 528 | |
| 529 | static struct device rbd_root_dev = { |
| 530 | .init_name = "rbd", |
| 531 | .release = rbd_root_dev_release, |
| 532 | }; |
| 533 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 534 | static __printf(2, 3) |
| 535 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 536 | { |
| 537 | struct va_format vaf; |
| 538 | va_list args; |
| 539 | |
| 540 | va_start(args, fmt); |
| 541 | vaf.fmt = fmt; |
| 542 | vaf.va = &args; |
| 543 | |
| 544 | if (!rbd_dev) |
| 545 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 546 | else if (rbd_dev->disk) |
| 547 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 548 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 549 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 550 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 551 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 552 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 553 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 554 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 555 | else /* punt */ |
| 556 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 557 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 558 | va_end(args); |
| 559 | } |
| 560 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 561 | #ifdef RBD_DEBUG |
| 562 | #define rbd_assert(expr) \ |
| 563 | if (unlikely(!(expr))) { \ |
| 564 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 565 | "at line %d:\n\n" \ |
| 566 | "\trbd_assert(%s);\n\n", \ |
| 567 | __func__, __LINE__, #expr); \ |
| 568 | BUG(); \ |
| 569 | } |
| 570 | #else /* !RBD_DEBUG */ |
| 571 | # define rbd_assert(expr) ((void) 0) |
| 572 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 573 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 574 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 575 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 576 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 577 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 578 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 579 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 580 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 581 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 582 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 583 | u8 *order, u64 *snap_size); |
| 584 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 585 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 586 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 587 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 588 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 589 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 590 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 591 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 592 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 593 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 594 | removing = true; |
| 595 | else |
| 596 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 597 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 598 | if (removing) |
| 599 | return -ENOENT; |
| 600 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 601 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 602 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 603 | return 0; |
| 604 | } |
| 605 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 606 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 607 | { |
| 608 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 609 | unsigned long open_count_before; |
| 610 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 611 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 612 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 613 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 614 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 615 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 616 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 619 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 620 | { |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 621 | int ro; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 622 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 623 | if (get_user(ro, (int __user *)arg)) |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 624 | return -EFAULT; |
| 625 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 626 | /* Snapshots can't be marked read-write */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 627 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 628 | return -EROFS; |
| 629 | |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 630 | /* Let blkdev_roset() handle it */ |
| 631 | return -ENOTTY; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 635 | unsigned int cmd, unsigned long arg) |
| 636 | { |
| 637 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Ilya Dryomov | 1de797b | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 638 | int ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 639 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 640 | switch (cmd) { |
| 641 | case BLKROSET: |
| 642 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 643 | break; |
| 644 | default: |
| 645 | ret = -ENOTTY; |
| 646 | } |
| 647 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 648 | return ret; |
| 649 | } |
| 650 | |
| 651 | #ifdef CONFIG_COMPAT |
| 652 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 653 | unsigned int cmd, unsigned long arg) |
| 654 | { |
| 655 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 656 | } |
| 657 | #endif /* CONFIG_COMPAT */ |
| 658 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 659 | static const struct block_device_operations rbd_bd_ops = { |
| 660 | .owner = THIS_MODULE, |
| 661 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 662 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 663 | .ioctl = rbd_ioctl, |
| 664 | #ifdef CONFIG_COMPAT |
| 665 | .compat_ioctl = rbd_compat_ioctl, |
| 666 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 667 | }; |
| 668 | |
| 669 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 670 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 671 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 672 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 673 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 674 | { |
| 675 | struct rbd_client *rbdc; |
| 676 | int ret = -ENOMEM; |
| 677 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 678 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 679 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 680 | if (!rbdc) |
| 681 | goto out_opt; |
| 682 | |
| 683 | kref_init(&rbdc->kref); |
| 684 | INIT_LIST_HEAD(&rbdc->node); |
| 685 | |
Ilya Dryomov | 74da4a0f | 2017-03-03 18:16:07 +0100 | [diff] [blame] | 686 | rbdc->client = ceph_create_client(ceph_opts, rbdc); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 687 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 688 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 689 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 690 | |
| 691 | ret = ceph_open_session(rbdc->client); |
| 692 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 693 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 694 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 695 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 696 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 697 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 698 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 699 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 700 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 701 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 702 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 703 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 704 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 705 | kfree(rbdc); |
| 706 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 707 | if (ceph_opts) |
| 708 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 709 | dout("%s: error %d\n", __func__, ret); |
| 710 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 711 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 714 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 715 | { |
| 716 | kref_get(&rbdc->kref); |
| 717 | |
| 718 | return rbdc; |
| 719 | } |
| 720 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 721 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 722 | * Find a ceph client with specific addr and configuration. If |
| 723 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 724 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 725 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 726 | { |
| 727 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 728 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 729 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 730 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 731 | return NULL; |
| 732 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 733 | spin_lock(&rbd_client_list_lock); |
| 734 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 735 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 736 | __rbd_get_client(client_node); |
| 737 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 738 | found = true; |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | spin_unlock(&rbd_client_list_lock); |
| 743 | |
| 744 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 748 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 749 | */ |
| 750 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 751 | Opt_queue_depth, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 752 | Opt_alloc_size, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 753 | Opt_lock_timeout, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 754 | Opt_last_int, |
| 755 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 756 | Opt_pool_ns, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 757 | Opt_last_string, |
| 758 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 759 | Opt_read_only, |
| 760 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 761 | Opt_lock_on_read, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 762 | Opt_exclusive, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 763 | Opt_notrim, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 764 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 765 | }; |
| 766 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 767 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 768 | {Opt_queue_depth, "queue_depth=%d"}, |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 769 | {Opt_alloc_size, "alloc_size=%d"}, |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 770 | {Opt_lock_timeout, "lock_timeout=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 771 | /* int args above */ |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 772 | {Opt_pool_ns, "_pool_ns=%s"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 773 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 774 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 775 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 776 | {Opt_read_write, "read_write"}, |
| 777 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 778 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 779 | {Opt_exclusive, "exclusive"}, |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 780 | {Opt_notrim, "notrim"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 781 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 784 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 785 | int queue_depth; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 786 | int alloc_size; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 787 | unsigned long lock_timeout; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 788 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 789 | bool lock_on_read; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 790 | bool exclusive; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 791 | bool trim; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 792 | }; |
| 793 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 794 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 795 | #define RBD_ALLOC_SIZE_DEFAULT (64 * 1024) |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 796 | #define RBD_LOCK_TIMEOUT_DEFAULT 0 /* no timeout */ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 797 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 798 | #define RBD_LOCK_ON_READ_DEFAULT false |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 799 | #define RBD_EXCLUSIVE_DEFAULT false |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 800 | #define RBD_TRIM_DEFAULT true |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 801 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 802 | struct parse_rbd_opts_ctx { |
| 803 | struct rbd_spec *spec; |
| 804 | struct rbd_options *opts; |
| 805 | }; |
| 806 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 807 | static int parse_rbd_opts_token(char *c, void *private) |
| 808 | { |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 809 | struct parse_rbd_opts_ctx *pctx = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 810 | substring_t argstr[MAX_OPT_ARGS]; |
| 811 | int token, intval, ret; |
| 812 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 813 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 814 | if (token < Opt_last_int) { |
| 815 | ret = match_int(&argstr[0], &intval); |
| 816 | if (ret < 0) { |
Ilya Dryomov | 2f56b6b | 2018-06-27 16:38:13 +0200 | [diff] [blame] | 817 | pr_err("bad option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 818 | return ret; |
| 819 | } |
| 820 | dout("got int token %d val %d\n", token, intval); |
| 821 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 822 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 823 | } else { |
| 824 | dout("got token %d\n", token); |
| 825 | } |
| 826 | |
| 827 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 828 | case Opt_queue_depth: |
| 829 | if (intval < 1) { |
| 830 | pr_err("queue_depth out of range\n"); |
| 831 | return -EINVAL; |
| 832 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 833 | pctx->opts->queue_depth = intval; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 834 | break; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 835 | case Opt_alloc_size: |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 836 | if (intval < SECTOR_SIZE) { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 837 | pr_err("alloc_size out of range\n"); |
| 838 | return -EINVAL; |
| 839 | } |
| 840 | if (!is_power_of_2(intval)) { |
| 841 | pr_err("alloc_size must be a power of 2\n"); |
| 842 | return -EINVAL; |
| 843 | } |
| 844 | pctx->opts->alloc_size = intval; |
| 845 | break; |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 846 | case Opt_lock_timeout: |
| 847 | /* 0 is "wait forever" (i.e. infinite timeout) */ |
| 848 | if (intval < 0 || intval > INT_MAX / 1000) { |
| 849 | pr_err("lock_timeout out of range\n"); |
| 850 | return -EINVAL; |
| 851 | } |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 852 | pctx->opts->lock_timeout = msecs_to_jiffies(intval * 1000); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 853 | break; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 854 | case Opt_pool_ns: |
| 855 | kfree(pctx->spec->pool_ns); |
| 856 | pctx->spec->pool_ns = match_strdup(argstr); |
| 857 | if (!pctx->spec->pool_ns) |
| 858 | return -ENOMEM; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 859 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 860 | case Opt_read_only: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 861 | pctx->opts->read_only = true; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 862 | break; |
| 863 | case Opt_read_write: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 864 | pctx->opts->read_only = false; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 865 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 866 | case Opt_lock_on_read: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 867 | pctx->opts->lock_on_read = true; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 868 | break; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 869 | case Opt_exclusive: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 870 | pctx->opts->exclusive = true; |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 871 | break; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 872 | case Opt_notrim: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 873 | pctx->opts->trim = false; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 874 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 875 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 876 | /* libceph prints "bad option" msg */ |
| 877 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 878 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 879 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 880 | return 0; |
| 881 | } |
| 882 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 883 | static char* obj_op_name(enum obj_operation_type op_type) |
| 884 | { |
| 885 | switch (op_type) { |
| 886 | case OBJ_OP_READ: |
| 887 | return "read"; |
| 888 | case OBJ_OP_WRITE: |
| 889 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 890 | case OBJ_OP_DISCARD: |
| 891 | return "discard"; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 892 | case OBJ_OP_ZEROOUT: |
| 893 | return "zeroout"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 894 | default: |
| 895 | return "???"; |
| 896 | } |
| 897 | } |
| 898 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 899 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 900 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 901 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 902 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 903 | */ |
| 904 | static void rbd_client_release(struct kref *kref) |
| 905 | { |
| 906 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 907 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 908 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 909 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 910 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 911 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 912 | |
| 913 | ceph_destroy_client(rbdc->client); |
| 914 | kfree(rbdc); |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 919 | * it. |
| 920 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 921 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 922 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 923 | if (rbdc) |
| 924 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 927 | /* |
| 928 | * Get a ceph client with specific addr and configuration, if one does |
| 929 | * not exist create it. Either way, ceph_opts is consumed by this |
| 930 | * function. |
| 931 | */ |
| 932 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
| 933 | { |
| 934 | struct rbd_client *rbdc; |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 935 | int ret; |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 936 | |
| 937 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
| 938 | rbdc = rbd_client_find(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 939 | if (rbdc) { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 940 | ceph_destroy_options(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 941 | |
| 942 | /* |
| 943 | * Using an existing client. Make sure ->pg_pools is up to |
| 944 | * date before we look up the pool id in do_rbd_add(). |
| 945 | */ |
Ilya Dryomov | 9d4a227 | 2019-03-20 10:58:05 +0100 | [diff] [blame] | 946 | ret = ceph_wait_for_latest_osdmap(rbdc->client, |
| 947 | rbdc->client->options->mount_timeout); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 948 | if (ret) { |
| 949 | rbd_warn(NULL, "failed to get latest osdmap: %d", ret); |
| 950 | rbd_put_client(rbdc); |
| 951 | rbdc = ERR_PTR(ret); |
| 952 | } |
| 953 | } else { |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 954 | rbdc = rbd_client_create(ceph_opts); |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 955 | } |
Ilya Dryomov | 5feb0d8d | 2018-02-22 13:19:04 +0100 | [diff] [blame] | 956 | mutex_unlock(&client_mutex); |
| 957 | |
| 958 | return rbdc; |
| 959 | } |
| 960 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 961 | static bool rbd_image_format_valid(u32 image_format) |
| 962 | { |
| 963 | return image_format == 1 || image_format == 2; |
| 964 | } |
| 965 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 966 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 967 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 968 | size_t size; |
| 969 | u32 snap_count; |
| 970 | |
| 971 | /* The header has to start with the magic rbd header text */ |
| 972 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 973 | return false; |
| 974 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 975 | /* The bio layer requires at least sector-sized I/O */ |
| 976 | |
| 977 | if (ondisk->options.order < SECTOR_SHIFT) |
| 978 | return false; |
| 979 | |
| 980 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 981 | |
| 982 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 983 | return false; |
| 984 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 985 | /* |
| 986 | * The size of a snapshot header has to fit in a size_t, and |
| 987 | * that limits the number of snapshots. |
| 988 | */ |
| 989 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 990 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 991 | if (snap_count > size / sizeof (__le64)) |
| 992 | return false; |
| 993 | |
| 994 | /* |
| 995 | * Not only that, but the size of the entire the snapshot |
| 996 | * header must also be representable in a size_t. |
| 997 | */ |
| 998 | size -= snap_count * sizeof (__le64); |
| 999 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 1000 | return false; |
| 1001 | |
| 1002 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 1003 | } |
| 1004 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1005 | /* |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1006 | * returns the size of an object in the image |
| 1007 | */ |
| 1008 | static u32 rbd_obj_bytes(struct rbd_image_header *header) |
| 1009 | { |
| 1010 | return 1U << header->obj_order; |
| 1011 | } |
| 1012 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1013 | static void rbd_init_layout(struct rbd_device *rbd_dev) |
| 1014 | { |
| 1015 | if (rbd_dev->header.stripe_unit == 0 || |
| 1016 | rbd_dev->header.stripe_count == 0) { |
| 1017 | rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header); |
| 1018 | rbd_dev->header.stripe_count = 1; |
| 1019 | } |
| 1020 | |
| 1021 | rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit; |
| 1022 | rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count; |
| 1023 | rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header); |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1024 | rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ? |
| 1025 | rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1026 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
| 1027 | } |
| 1028 | |
Ilya Dryomov | 5bc3fb1 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1029 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1030 | * Fill an rbd image header with information from the given format 1 |
| 1031 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1032 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1033 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 1034 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1035 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1036 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | bool first_time = header->object_prefix == NULL; |
| 1038 | struct ceph_snap_context *snapc; |
| 1039 | char *object_prefix = NULL; |
| 1040 | char *snap_names = NULL; |
| 1041 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1042 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1043 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1044 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1045 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1046 | /* Allocate this now to avoid having to handle failure below */ |
| 1047 | |
| 1048 | if (first_time) { |
Ilya Dryomov | 848d796 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 1049 | object_prefix = kstrndup(ondisk->object_prefix, |
| 1050 | sizeof(ondisk->object_prefix), |
| 1051 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1052 | if (!object_prefix) |
| 1053 | return -ENOMEM; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1057 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1058 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1059 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1060 | if (!snapc) |
| 1061 | goto out_err; |
| 1062 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1063 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1064 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1065 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1066 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1067 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1068 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | if (snap_names_len > (u64)SIZE_MAX) |
| 1070 | goto out_2big; |
| 1071 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1072 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1073 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | |
| 1075 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1076 | snap_sizes = kmalloc_array(snap_count, |
| 1077 | sizeof(*header->snap_sizes), |
| 1078 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1079 | if (!snap_sizes) |
| 1080 | goto out_err; |
| 1081 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1082 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1083 | * Copy the names, and fill in each snapshot's id |
| 1084 | * and size. |
| 1085 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1086 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1087 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1088 | * snap_names_len bytes beyond the end of the |
| 1089 | * snapshot id array, this memcpy() is safe. |
| 1090 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1091 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1092 | snaps = ondisk->snaps; |
| 1093 | for (i = 0; i < snap_count; i++) { |
| 1094 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1095 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1096 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1097 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1098 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1099 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1100 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1101 | if (first_time) { |
| 1102 | header->object_prefix = object_prefix; |
| 1103 | header->obj_order = ondisk->options.order; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1104 | rbd_init_layout(rbd_dev); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1105 | } else { |
| 1106 | ceph_put_snap_context(header->snapc); |
| 1107 | kfree(header->snap_names); |
| 1108 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1112 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1113 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1114 | header->snapc = snapc; |
| 1115 | header->snap_names = snap_names; |
| 1116 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1117 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1118 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1119 | out_2big: |
| 1120 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1121 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1122 | kfree(snap_sizes); |
| 1123 | kfree(snap_names); |
| 1124 | ceph_put_snap_context(snapc); |
| 1125 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1126 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1127 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1130 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1131 | { |
| 1132 | const char *snap_name; |
| 1133 | |
| 1134 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1135 | |
| 1136 | /* Skip over names until we find the one we are looking for */ |
| 1137 | |
| 1138 | snap_name = rbd_dev->header.snap_names; |
| 1139 | while (which--) |
| 1140 | snap_name += strlen(snap_name) + 1; |
| 1141 | |
| 1142 | return kstrdup(snap_name, GFP_KERNEL); |
| 1143 | } |
| 1144 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1145 | /* |
| 1146 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1147 | * Note that result is for snapshots in *descending* order. |
| 1148 | */ |
| 1149 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1150 | { |
| 1151 | u64 snap_id1 = *(u64 *)s1; |
| 1152 | u64 snap_id2 = *(u64 *)s2; |
| 1153 | |
| 1154 | if (snap_id1 < snap_id2) |
| 1155 | return 1; |
| 1156 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | * Search a snapshot context to see if the given snapshot id is |
| 1161 | * present. |
| 1162 | * |
| 1163 | * Returns the position of the snapshot id in the array if it's found, |
| 1164 | * or BAD_SNAP_INDEX otherwise. |
| 1165 | * |
| 1166 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1167 | * reverse order, highest snapshot id first. |
| 1168 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1169 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1170 | { |
| 1171 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1172 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1173 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1174 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1175 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1176 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1177 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1178 | } |
| 1179 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1180 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1181 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1182 | { |
| 1183 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1184 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1185 | |
| 1186 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1187 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1188 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1189 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1190 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1191 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1192 | } |
| 1193 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1194 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1195 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1196 | if (snap_id == CEPH_NOSNAP) |
| 1197 | return RBD_SNAP_HEAD_NAME; |
| 1198 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1199 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1200 | if (rbd_dev->image_format == 1) |
| 1201 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1202 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1203 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1204 | } |
| 1205 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1206 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1207 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1208 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1209 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1210 | if (snap_id == CEPH_NOSNAP) { |
| 1211 | *snap_size = rbd_dev->header.image_size; |
| 1212 | } else if (rbd_dev->image_format == 1) { |
| 1213 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1214 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1215 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1216 | if (which == BAD_SNAP_INDEX) |
| 1217 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1218 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1219 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1220 | } else { |
| 1221 | u64 size = 0; |
| 1222 | int ret; |
| 1223 | |
| 1224 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
| 1228 | *snap_size = size; |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
| 1233 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1234 | u64 *snap_features) |
| 1235 | { |
| 1236 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1237 | if (snap_id == CEPH_NOSNAP) { |
| 1238 | *snap_features = rbd_dev->header.features; |
| 1239 | } else if (rbd_dev->image_format == 1) { |
| 1240 | *snap_features = 0; /* No features for format 1 */ |
| 1241 | } else { |
| 1242 | u64 features = 0; |
| 1243 | int ret; |
| 1244 | |
| 1245 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1246 | if (ret) |
| 1247 | return ret; |
| 1248 | |
| 1249 | *snap_features = features; |
| 1250 | } |
| 1251 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1254 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1255 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1256 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1257 | u64 size = 0; |
| 1258 | u64 features = 0; |
| 1259 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1260 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1261 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1262 | if (ret) |
| 1263 | return ret; |
| 1264 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1265 | if (ret) |
| 1266 | return ret; |
| 1267 | |
| 1268 | rbd_dev->mapping.size = size; |
| 1269 | rbd_dev->mapping.features = features; |
| 1270 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1271 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1272 | } |
| 1273 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1274 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1275 | { |
| 1276 | rbd_dev->mapping.size = 0; |
| 1277 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1278 | } |
| 1279 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1280 | static void zero_bvec(struct bio_vec *bv) |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1281 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1282 | void *buf; |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1283 | unsigned long flags; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1284 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1285 | buf = bvec_kmap_irq(bv, &flags); |
| 1286 | memset(buf, 0, bv->bv_len); |
| 1287 | flush_dcache_page(bv->bv_page); |
| 1288 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1291 | static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes) |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1292 | { |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1293 | struct ceph_bio_iter it = *bio_pos; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1294 | |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1295 | ceph_bio_iter_advance(&it, off); |
| 1296 | ceph_bio_iter_advance_step(&it, bytes, ({ |
| 1297 | zero_bvec(&bv); |
| 1298 | })); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1299 | } |
| 1300 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1301 | static void zero_bvecs(struct ceph_bvec_iter *bvec_pos, u32 off, u32 bytes) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1302 | { |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1303 | struct ceph_bvec_iter it = *bvec_pos; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1304 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1305 | ceph_bvec_iter_advance(&it, off); |
| 1306 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 1307 | zero_bvec(&bv); |
| 1308 | })); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1309 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1310 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1311 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1312 | * Zero a range in @obj_req data buffer defined by a bio (list) or |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1313 | * (private) bio_vec array. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1314 | * |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1315 | * @off is relative to the start of the data buffer. |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1316 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1317 | static void rbd_obj_zero_range(struct rbd_obj_request *obj_req, u32 off, |
| 1318 | u32 bytes) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1319 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1320 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1321 | case OBJ_REQUEST_BIO: |
| 1322 | zero_bios(&obj_req->bio_pos, off, bytes); |
| 1323 | break; |
| 1324 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1325 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1326 | zero_bvecs(&obj_req->bvec_pos, off, bytes); |
| 1327 | break; |
| 1328 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 1329 | BUG(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1330 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1334 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1335 | { |
| 1336 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1337 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1338 | kref_read(&obj_request->kref)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1339 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1340 | } |
| 1341 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1342 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1343 | { |
| 1344 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1345 | kref_read(&img_request->kref)); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1346 | kref_get(&img_request->kref); |
| 1347 | } |
| 1348 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1349 | static void rbd_img_request_destroy(struct kref *kref); |
| 1350 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1351 | { |
| 1352 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1353 | dout("%s: img %p (was %d)\n", __func__, img_request, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 1354 | kref_read(&img_request->kref)); |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1355 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1359 | struct rbd_obj_request *obj_request) |
| 1360 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1361 | rbd_assert(obj_request->img_request == NULL); |
| 1362 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1363 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1364 | obj_request->img_request = img_request; |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1365 | img_request->pending_count++; |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1366 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1370 | struct rbd_obj_request *obj_request) |
| 1371 | { |
Ilya Dryomov | 15961b4 | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1372 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1373 | list_del(&obj_request->ex.oe_item); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1374 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1375 | rbd_obj_request_put(obj_request); |
| 1376 | } |
| 1377 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1378 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1379 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1380 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1381 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1382 | dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1383 | obj_request, obj_request->ex.oe_objno, obj_request->ex.oe_off, |
| 1384 | obj_request->ex.oe_len, osd_req); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1385 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1386 | } |
| 1387 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1388 | /* |
| 1389 | * The default/initial value for all image request flags is 0. Each |
| 1390 | * is conditionally set to 1 at image request initialization time |
| 1391 | * and currently never change thereafter. |
| 1392 | */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1393 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1394 | { |
| 1395 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1396 | smp_mb(); |
| 1397 | } |
| 1398 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1399 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1400 | { |
| 1401 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1402 | smp_mb(); |
| 1403 | } |
| 1404 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1405 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1406 | { |
| 1407 | smp_mb(); |
| 1408 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1409 | } |
| 1410 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1411 | static bool rbd_obj_is_entire(struct rbd_obj_request *obj_req) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1412 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1413 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1414 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1415 | return !obj_req->ex.oe_off && |
| 1416 | obj_req->ex.oe_len == rbd_dev->layout.object_size; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1419 | static bool rbd_obj_is_tail(struct rbd_obj_request *obj_req) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1420 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1421 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1422 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1423 | return obj_req->ex.oe_off + obj_req->ex.oe_len == |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1424 | rbd_dev->layout.object_size; |
| 1425 | } |
| 1426 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1427 | /* |
| 1428 | * Must be called after rbd_obj_calc_img_extents(). |
| 1429 | */ |
| 1430 | static bool rbd_obj_copyup_enabled(struct rbd_obj_request *obj_req) |
| 1431 | { |
| 1432 | if (!obj_req->num_img_extents || |
Ilya Dryomov | 9b17eb2 | 2019-02-28 15:51:39 +0100 | [diff] [blame] | 1433 | (rbd_obj_is_entire(obj_req) && |
| 1434 | !obj_req->img_request->snapc->num_snaps)) |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1435 | return false; |
| 1436 | |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1440 | static u64 rbd_obj_img_extents_bytes(struct rbd_obj_request *obj_req) |
| 1441 | { |
| 1442 | return ceph_file_extents_bytes(obj_req->img_extents, |
| 1443 | obj_req->num_img_extents); |
| 1444 | } |
| 1445 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1446 | static bool rbd_img_is_write(struct rbd_img_request *img_req) |
| 1447 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1448 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1449 | case OBJ_OP_READ: |
| 1450 | return false; |
| 1451 | case OBJ_OP_WRITE: |
| 1452 | case OBJ_OP_DISCARD: |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1453 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1454 | return true; |
| 1455 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 1456 | BUG(); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1457 | } |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1460 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req); |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1461 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1462 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1463 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1464 | struct rbd_obj_request *obj_req = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1465 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1466 | dout("%s osd_req %p result %d for obj_req %p\n", __func__, osd_req, |
| 1467 | osd_req->r_result, obj_req); |
| 1468 | rbd_assert(osd_req == obj_req->osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1469 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1470 | obj_req->result = osd_req->r_result < 0 ? osd_req->r_result : 0; |
| 1471 | if (!obj_req->result && !rbd_img_is_write(obj_req->img_request)) |
| 1472 | obj_req->xferred = osd_req->r_result; |
| 1473 | else |
| 1474 | /* |
| 1475 | * Writes aren't allowed to return a data payload. In some |
| 1476 | * guarded write cases (e.g. stat + zero on an empty object) |
| 1477 | * a stat response makes it through, but we don't care. |
| 1478 | */ |
| 1479 | obj_req->xferred = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1480 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1481 | rbd_obj_handle_request(obj_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1482 | } |
| 1483 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1484 | static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request) |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1485 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1486 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1487 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1488 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1489 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1493 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1494 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1495 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1496 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE; |
Arnd Bergmann | fac02dd | 2018-07-13 22:18:37 +0200 | [diff] [blame] | 1497 | ktime_get_real_ts64(&osd_req->r_mtime); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1498 | osd_req->r_data_offset = obj_request->ex.oe_off; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1499 | } |
| 1500 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1501 | static struct ceph_osd_request * |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1502 | __rbd_osd_req_create(struct rbd_obj_request *obj_req, |
| 1503 | struct ceph_snap_context *snapc, unsigned int num_ops) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1504 | { |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1505 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1506 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 1507 | struct ceph_osd_request *req; |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1508 | const char *name_format = rbd_dev->image_format == 1 ? |
| 1509 | RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1510 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1511 | req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1512 | if (!req) |
| 1513 | return NULL; |
| 1514 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1515 | req->r_callback = rbd_osd_req_callback; |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1516 | req->r_priv = obj_req; |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1517 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1518 | /* |
| 1519 | * Data objects may be stored in a separate pool, but always in |
| 1520 | * the same namespace in that pool as the header in its pool. |
| 1521 | */ |
| 1522 | ceph_oloc_copy(&req->r_base_oloc, &rbd_dev->header_oloc); |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1523 | req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 1524 | |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1525 | if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1526 | rbd_dev->header.object_prefix, obj_req->ex.oe_objno)) |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1527 | goto err_req; |
| 1528 | |
Ilya Dryomov | bc81207 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1529 | return req; |
| 1530 | |
| 1531 | err_req: |
| 1532 | ceph_osdc_put_request(req); |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1536 | static struct ceph_osd_request * |
| 1537 | rbd_osd_req_create(struct rbd_obj_request *obj_req, unsigned int num_ops) |
| 1538 | { |
| 1539 | return __rbd_osd_req_create(obj_req, obj_req->img_request->snapc, |
| 1540 | num_ops); |
| 1541 | } |
| 1542 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1543 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 1544 | { |
| 1545 | ceph_osdc_put_request(osd_req); |
| 1546 | } |
| 1547 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1548 | static struct rbd_obj_request *rbd_obj_request_create(void) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1549 | { |
| 1550 | struct rbd_obj_request *obj_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1551 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 1552 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1553 | if (!obj_request) |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1554 | return NULL; |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1555 | |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1556 | ceph_object_extent_init(&obj_request->ex); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1557 | kref_init(&obj_request->kref); |
| 1558 | |
Ilya Dryomov | 67e2b65 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 1559 | dout("%s %p\n", __func__, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1560 | return obj_request; |
| 1561 | } |
| 1562 | |
| 1563 | static void rbd_obj_request_destroy(struct kref *kref) |
| 1564 | { |
| 1565 | struct rbd_obj_request *obj_request; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1566 | u32 i; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1567 | |
| 1568 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 1569 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1570 | dout("%s: obj %p\n", __func__, obj_request); |
| 1571 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1572 | if (obj_request->osd_req) |
| 1573 | rbd_osd_req_destroy(obj_request->osd_req); |
| 1574 | |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1575 | switch (obj_request->img_request->data_type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1576 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1577 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1578 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | 5359a17 | 2018-01-20 10:30:10 +0100 | [diff] [blame] | 1579 | break; /* Nothing to do */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1580 | case OBJ_REQUEST_OWN_BVECS: |
| 1581 | kfree(obj_request->bvec_pos.bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1582 | break; |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1583 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 1584 | BUG(); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1585 | } |
| 1586 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1587 | kfree(obj_request->img_extents); |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 1588 | if (obj_request->copyup_bvecs) { |
| 1589 | for (i = 0; i < obj_request->copyup_bvec_count; i++) { |
| 1590 | if (obj_request->copyup_bvecs[i].bv_page) |
| 1591 | __free_page(obj_request->copyup_bvecs[i].bv_page); |
| 1592 | } |
| 1593 | kfree(obj_request->copyup_bvecs); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1594 | } |
| 1595 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1596 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1597 | } |
| 1598 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1599 | /* It's OK to call this for a device with no parent */ |
| 1600 | |
| 1601 | static void rbd_spec_put(struct rbd_spec *spec); |
| 1602 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 1603 | { |
| 1604 | rbd_dev_remove_parent(rbd_dev); |
| 1605 | rbd_spec_put(rbd_dev->parent_spec); |
| 1606 | rbd_dev->parent_spec = NULL; |
| 1607 | rbd_dev->parent_overlap = 0; |
| 1608 | } |
| 1609 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1610 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1611 | * Parent image reference counting is used to determine when an |
| 1612 | * image's parent fields can be safely torn down--after there are no |
| 1613 | * more in-flight requests to the parent image. When the last |
| 1614 | * reference is dropped, cleaning them up is safe. |
| 1615 | */ |
| 1616 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 1617 | { |
| 1618 | int counter; |
| 1619 | |
| 1620 | if (!rbd_dev->parent_spec) |
| 1621 | return; |
| 1622 | |
| 1623 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 1624 | if (counter > 0) |
| 1625 | return; |
| 1626 | |
| 1627 | /* Last reference; clean up parent data structures */ |
| 1628 | |
| 1629 | if (!counter) |
| 1630 | rbd_dev_unparent(rbd_dev); |
| 1631 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1632 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /* |
| 1636 | * If an image has a non-zero parent overlap, get a reference to its |
| 1637 | * parent. |
| 1638 | * |
| 1639 | * Returns true if the rbd device has a parent with a non-zero |
| 1640 | * overlap and a reference for it was successfully taken, or |
| 1641 | * false otherwise. |
| 1642 | */ |
| 1643 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 1644 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1645 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1646 | |
| 1647 | if (!rbd_dev->parent_spec) |
| 1648 | return false; |
| 1649 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1650 | down_read(&rbd_dev->header_rwsem); |
| 1651 | if (rbd_dev->parent_overlap) |
| 1652 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 1653 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1654 | |
| 1655 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1656 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1657 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 1658 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1659 | } |
| 1660 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1661 | /* |
| 1662 | * Caller is responsible for filling in the list of object requests |
| 1663 | * that comprises the image request, and the Linux request pointer |
| 1664 | * (if there is one). |
| 1665 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 1666 | static struct rbd_img_request *rbd_img_request_create( |
| 1667 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1668 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 1669 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1670 | { |
| 1671 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1672 | |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1673 | img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1674 | if (!img_request) |
| 1675 | return NULL; |
| 1676 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1677 | img_request->rbd_dev = rbd_dev; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1678 | img_request->op_type = op_type; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1679 | if (!rbd_img_is_write(img_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1680 | img_request->snap_id = rbd_dev->spec->snap_id; |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1681 | else |
| 1682 | img_request->snapc = snapc; |
| 1683 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1684 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1685 | img_request_layered_set(img_request); |
Ilya Dryomov | a0c5895 | 2018-01-22 16:03:06 +0100 | [diff] [blame] | 1686 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1687 | spin_lock_init(&img_request->completion_lock); |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1688 | INIT_LIST_HEAD(&img_request->object_extents); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1689 | kref_init(&img_request->kref); |
| 1690 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1691 | dout("%s: rbd_dev %p %s -> img %p\n", __func__, rbd_dev, |
| 1692 | obj_op_name(op_type), img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1693 | return img_request; |
| 1694 | } |
| 1695 | |
| 1696 | static void rbd_img_request_destroy(struct kref *kref) |
| 1697 | { |
| 1698 | struct rbd_img_request *img_request; |
| 1699 | struct rbd_obj_request *obj_request; |
| 1700 | struct rbd_obj_request *next_obj_request; |
| 1701 | |
| 1702 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 1703 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1704 | dout("%s: img %p\n", __func__, img_request); |
| 1705 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1706 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 1707 | rbd_img_obj_request_del(img_request, obj_request); |
| 1708 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1709 | if (img_request_layered_test(img_request)) { |
| 1710 | img_request_layered_clear(img_request); |
| 1711 | rbd_dev_parent_put(img_request->rbd_dev); |
| 1712 | } |
| 1713 | |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1714 | if (rbd_img_is_write(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 1715 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1716 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1717 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1718 | } |
| 1719 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1720 | static void prune_extents(struct ceph_file_extent *img_extents, |
| 1721 | u32 *num_img_extents, u64 overlap) |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1722 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1723 | u32 cnt = *num_img_extents; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1724 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1725 | /* drop extents completely beyond the overlap */ |
| 1726 | while (cnt && img_extents[cnt - 1].fe_off >= overlap) |
| 1727 | cnt--; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1728 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1729 | if (cnt) { |
| 1730 | struct ceph_file_extent *ex = &img_extents[cnt - 1]; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1731 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1732 | /* trim final overlapping extent */ |
| 1733 | if (ex->fe_off + ex->fe_len > overlap) |
| 1734 | ex->fe_len = overlap - ex->fe_off; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1735 | } |
| 1736 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1737 | *num_img_extents = cnt; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 1738 | } |
| 1739 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1740 | /* |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1741 | * Determine the byte range(s) covered by either just the object extent |
| 1742 | * or the entire object in the parent image. |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1743 | */ |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1744 | static int rbd_obj_calc_img_extents(struct rbd_obj_request *obj_req, |
| 1745 | bool entire) |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1746 | { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1747 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1748 | int ret; |
| 1749 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1750 | if (!rbd_dev->parent_overlap) |
| 1751 | return 0; |
| 1752 | |
| 1753 | ret = ceph_extent_to_file(&rbd_dev->layout, obj_req->ex.oe_objno, |
| 1754 | entire ? 0 : obj_req->ex.oe_off, |
| 1755 | entire ? rbd_dev->layout.object_size : |
| 1756 | obj_req->ex.oe_len, |
| 1757 | &obj_req->img_extents, |
| 1758 | &obj_req->num_img_extents); |
| 1759 | if (ret) |
| 1760 | return ret; |
| 1761 | |
| 1762 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 1763 | rbd_dev->parent_overlap); |
| 1764 | return 0; |
| 1765 | } |
| 1766 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1767 | static void rbd_osd_req_setup_data(struct rbd_obj_request *obj_req, u32 which) |
| 1768 | { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 1769 | switch (obj_req->img_request->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1770 | case OBJ_REQUEST_BIO: |
| 1771 | osd_req_op_extent_osd_data_bio(obj_req->osd_req, which, |
| 1772 | &obj_req->bio_pos, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1773 | obj_req->ex.oe_len); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1774 | break; |
| 1775 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1776 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1777 | rbd_assert(obj_req->bvec_pos.iter.bi_size == |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1778 | obj_req->ex.oe_len); |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 1779 | rbd_assert(obj_req->bvec_idx == obj_req->bvec_count); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1780 | osd_req_op_extent_osd_data_bvec_pos(obj_req->osd_req, which, |
| 1781 | &obj_req->bvec_pos); |
| 1782 | break; |
| 1783 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 1784 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
| 1788 | static int rbd_obj_setup_read(struct rbd_obj_request *obj_req) |
| 1789 | { |
Ilya Dryomov | e28eded | 2019-02-25 11:42:26 +0100 | [diff] [blame] | 1790 | obj_req->osd_req = __rbd_osd_req_create(obj_req, NULL, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1791 | if (!obj_req->osd_req) |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1792 | return -ENOMEM; |
| 1793 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1794 | osd_req_op_extent_init(obj_req->osd_req, 0, CEPH_OSD_OP_READ, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1795 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1796 | rbd_osd_req_setup_data(obj_req, 0); |
Ilya Dryomov | a90bb0c | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 1797 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1798 | rbd_osd_req_format_read(obj_req); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
| 1802 | static int __rbd_obj_setup_stat(struct rbd_obj_request *obj_req, |
| 1803 | unsigned int which) |
| 1804 | { |
| 1805 | struct page **pages; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 1806 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1807 | /* |
| 1808 | * The response data for a STAT call consists of: |
| 1809 | * le64 length; |
| 1810 | * struct { |
| 1811 | * le32 tv_sec; |
| 1812 | * le32 tv_nsec; |
| 1813 | * } mtime; |
| 1814 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1815 | pages = ceph_alloc_page_vector(1, GFP_NOIO); |
| 1816 | if (IS_ERR(pages)) |
| 1817 | return PTR_ERR(pages); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1818 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1819 | osd_req_op_init(obj_req->osd_req, which, CEPH_OSD_OP_STAT, 0); |
| 1820 | osd_req_op_raw_data_in_pages(obj_req->osd_req, which, pages, |
| 1821 | 8 + sizeof(struct ceph_timespec), |
| 1822 | 0, false, true); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1823 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1824 | } |
| 1825 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1826 | static int count_write_ops(struct rbd_obj_request *obj_req) |
| 1827 | { |
| 1828 | return 2; /* setallochint + write/writefull */ |
| 1829 | } |
| 1830 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1831 | static void __rbd_obj_setup_write(struct rbd_obj_request *obj_req, |
| 1832 | unsigned int which) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1833 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1834 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1835 | u16 opcode; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1836 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1837 | osd_req_op_alloc_hint_init(obj_req->osd_req, which++, |
| 1838 | rbd_dev->layout.object_size, |
| 1839 | rbd_dev->layout.object_size); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1840 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1841 | if (rbd_obj_is_entire(obj_req)) |
| 1842 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 1843 | else |
| 1844 | opcode = CEPH_OSD_OP_WRITE; |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1845 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1846 | osd_req_op_extent_init(obj_req->osd_req, which, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1847 | obj_req->ex.oe_off, obj_req->ex.oe_len, 0, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1848 | rbd_osd_req_setup_data(obj_req, which++); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1849 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1850 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1851 | rbd_osd_req_format_write(obj_req); |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1852 | } |
| 1853 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1854 | static int rbd_obj_setup_write(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 1855 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1856 | unsigned int num_osd_ops, which = 0; |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1857 | bool need_guard; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1858 | int ret; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 1859 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1860 | /* reverse map the entire object onto the parent */ |
| 1861 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1862 | if (ret) |
| 1863 | return ret; |
| 1864 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1865 | need_guard = rbd_obj_copyup_enabled(obj_req); |
| 1866 | num_osd_ops = need_guard + count_write_ops(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1867 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1868 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1869 | if (!obj_req->osd_req) |
| 1870 | return -ENOMEM; |
| 1871 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1872 | if (need_guard) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1873 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 1874 | if (ret) |
| 1875 | return ret; |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1876 | |
| 1877 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 1878 | } else { |
| 1879 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | __rbd_obj_setup_write(obj_req, which); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1886 | static u16 truncate_or_zero_opcode(struct rbd_obj_request *obj_req) |
| 1887 | { |
| 1888 | return rbd_obj_is_tail(obj_req) ? CEPH_OSD_OP_TRUNCATE : |
| 1889 | CEPH_OSD_OP_ZERO; |
| 1890 | } |
| 1891 | |
| 1892 | static int rbd_obj_setup_discard(struct rbd_obj_request *obj_req) |
| 1893 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1894 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 1895 | u64 off = obj_req->ex.oe_off; |
| 1896 | u64 next_off = obj_req->ex.oe_off + obj_req->ex.oe_len; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1897 | int ret; |
| 1898 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1899 | /* |
| 1900 | * Align the range to alloc_size boundary and punt on discards |
| 1901 | * that are too small to free up any space. |
| 1902 | * |
| 1903 | * alloc_size == object_size && is_tail() is a special case for |
| 1904 | * filestore with filestore_punch_hole = false, needed to allow |
| 1905 | * truncate (in addition to delete). |
| 1906 | */ |
| 1907 | if (rbd_dev->opts->alloc_size != rbd_dev->layout.object_size || |
| 1908 | !rbd_obj_is_tail(obj_req)) { |
| 1909 | off = round_up(off, rbd_dev->opts->alloc_size); |
| 1910 | next_off = round_down(next_off, rbd_dev->opts->alloc_size); |
| 1911 | if (off >= next_off) |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1915 | /* reverse map the entire object onto the parent */ |
| 1916 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1917 | if (ret) |
| 1918 | return ret; |
| 1919 | |
| 1920 | obj_req->osd_req = rbd_osd_req_create(obj_req, 1); |
| 1921 | if (!obj_req->osd_req) |
| 1922 | return -ENOMEM; |
| 1923 | |
| 1924 | if (rbd_obj_is_entire(obj_req) && !obj_req->num_img_extents) { |
| 1925 | osd_req_op_init(obj_req->osd_req, 0, CEPH_OSD_OP_DELETE, 0); |
| 1926 | } else { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1927 | dout("%s %p %llu~%llu -> %llu~%llu\n", __func__, |
| 1928 | obj_req, obj_req->ex.oe_off, obj_req->ex.oe_len, |
| 1929 | off, next_off - off); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1930 | osd_req_op_extent_init(obj_req->osd_req, 0, |
| 1931 | truncate_or_zero_opcode(obj_req), |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 1932 | off, next_off - off, 0, 0); |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
| 1936 | rbd_osd_req_format_write(obj_req); |
| 1937 | return 0; |
| 1938 | } |
| 1939 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1940 | static int count_zeroout_ops(struct rbd_obj_request *obj_req) |
| 1941 | { |
| 1942 | int num_osd_ops; |
| 1943 | |
Ilya Dryomov | 9b17eb2 | 2019-02-28 15:51:39 +0100 | [diff] [blame] | 1944 | if (rbd_obj_is_entire(obj_req) && obj_req->num_img_extents && |
| 1945 | !rbd_obj_copyup_enabled(obj_req)) |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1946 | num_osd_ops = 2; /* create + truncate */ |
| 1947 | else |
| 1948 | num_osd_ops = 1; /* delete/truncate/zero */ |
| 1949 | |
| 1950 | return num_osd_ops; |
| 1951 | } |
| 1952 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1953 | static void __rbd_obj_setup_zeroout(struct rbd_obj_request *obj_req, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1954 | unsigned int which) |
| 1955 | { |
| 1956 | u16 opcode; |
| 1957 | |
| 1958 | if (rbd_obj_is_entire(obj_req)) { |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1959 | if (obj_req->num_img_extents) { |
Ilya Dryomov | 9b17eb2 | 2019-02-28 15:51:39 +0100 | [diff] [blame] | 1960 | if (!rbd_obj_copyup_enabled(obj_req)) |
| 1961 | osd_req_op_init(obj_req->osd_req, which++, |
| 1962 | CEPH_OSD_OP_CREATE, 0); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1963 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 1964 | } else { |
| 1965 | osd_req_op_init(obj_req->osd_req, which++, |
| 1966 | CEPH_OSD_OP_DELETE, 0); |
| 1967 | opcode = 0; |
| 1968 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1969 | } else { |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1970 | opcode = truncate_or_zero_opcode(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | if (opcode) |
| 1974 | osd_req_op_extent_init(obj_req->osd_req, which++, opcode, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 1975 | obj_req->ex.oe_off, obj_req->ex.oe_len, |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1976 | 0, 0); |
| 1977 | |
| 1978 | rbd_assert(which == obj_req->osd_req->r_num_ops); |
| 1979 | rbd_osd_req_format_write(obj_req); |
| 1980 | } |
| 1981 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 1982 | static int rbd_obj_setup_zeroout(struct rbd_obj_request *obj_req) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1983 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1984 | unsigned int num_osd_ops, which = 0; |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1985 | bool need_guard; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1986 | int ret; |
| 1987 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 1988 | /* reverse map the entire object onto the parent */ |
| 1989 | ret = rbd_obj_calc_img_extents(obj_req, true); |
| 1990 | if (ret) |
| 1991 | return ret; |
| 1992 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 1993 | need_guard = rbd_obj_copyup_enabled(obj_req); |
| 1994 | num_osd_ops = need_guard + count_zeroout_ops(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1995 | |
Ilya Dryomov | a162b30 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 1996 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 1997 | if (!obj_req->osd_req) |
| 1998 | return -ENOMEM; |
| 1999 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2000 | if (need_guard) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2001 | ret = __rbd_obj_setup_stat(obj_req, which++); |
| 2002 | if (ret) |
| 2003 | return ret; |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2004 | |
| 2005 | obj_req->write_state = RBD_OBJ_WRITE_GUARD; |
| 2006 | } else { |
| 2007 | obj_req->write_state = RBD_OBJ_WRITE_FLAT; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2008 | } |
| 2009 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2010 | __rbd_obj_setup_zeroout(obj_req, which); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | /* |
| 2015 | * For each object request in @img_req, allocate an OSD request, add |
| 2016 | * individual OSD ops and prepare them for submission. The number of |
| 2017 | * OSD ops depends on op_type and the overlap point (if any). |
| 2018 | */ |
| 2019 | static int __rbd_img_fill_request(struct rbd_img_request *img_req) |
| 2020 | { |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2021 | struct rbd_obj_request *obj_req, *next_obj_req; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2022 | int ret; |
| 2023 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2024 | for_each_obj_request_safe(img_req, obj_req, next_obj_req) { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2025 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2026 | case OBJ_OP_READ: |
| 2027 | ret = rbd_obj_setup_read(obj_req); |
| 2028 | break; |
| 2029 | case OBJ_OP_WRITE: |
| 2030 | ret = rbd_obj_setup_write(obj_req); |
| 2031 | break; |
| 2032 | case OBJ_OP_DISCARD: |
| 2033 | ret = rbd_obj_setup_discard(obj_req); |
| 2034 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2035 | case OBJ_OP_ZEROOUT: |
| 2036 | ret = rbd_obj_setup_zeroout(obj_req); |
| 2037 | break; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2038 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 2039 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2040 | } |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2041 | if (ret < 0) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2042 | return ret; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 2043 | if (ret > 0) { |
| 2044 | img_req->xferred += obj_req->ex.oe_len; |
| 2045 | img_req->pending_count--; |
| 2046 | rbd_img_obj_request_del(img_req, obj_req); |
| 2047 | continue; |
| 2048 | } |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 2049 | |
| 2050 | ret = ceph_osdc_alloc_messages(obj_req->osd_req, GFP_NOIO); |
| 2051 | if (ret) |
| 2052 | return ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | return 0; |
| 2056 | } |
| 2057 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2058 | union rbd_img_fill_iter { |
| 2059 | struct ceph_bio_iter bio_iter; |
| 2060 | struct ceph_bvec_iter bvec_iter; |
| 2061 | }; |
| 2062 | |
| 2063 | struct rbd_img_fill_ctx { |
| 2064 | enum obj_request_type pos_type; |
| 2065 | union rbd_img_fill_iter *pos; |
| 2066 | union rbd_img_fill_iter iter; |
| 2067 | ceph_object_extent_fn_t set_pos_fn; |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2068 | ceph_object_extent_fn_t count_fn; |
| 2069 | ceph_object_extent_fn_t copy_fn; |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2070 | }; |
| 2071 | |
| 2072 | static struct ceph_object_extent *alloc_object_extent(void *arg) |
| 2073 | { |
| 2074 | struct rbd_img_request *img_req = arg; |
| 2075 | struct rbd_obj_request *obj_req; |
| 2076 | |
| 2077 | obj_req = rbd_obj_request_create(); |
| 2078 | if (!obj_req) |
| 2079 | return NULL; |
| 2080 | |
| 2081 | rbd_img_obj_request_add(img_req, obj_req); |
| 2082 | return &obj_req->ex; |
| 2083 | } |
| 2084 | |
| 2085 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2086 | * While su != os && sc == 1 is technically not fancy (it's the same |
| 2087 | * layout as su == os && sc == 1), we can't use the nocopy path for it |
| 2088 | * because ->set_pos_fn() should be called only once per object. |
| 2089 | * ceph_file_to_extents() invokes action_fn once per stripe unit, so |
| 2090 | * treat su != os && sc == 1 as fancy. |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2091 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2092 | static bool rbd_layout_is_fancy(struct ceph_file_layout *l) |
| 2093 | { |
| 2094 | return l->stripe_unit != l->object_size; |
| 2095 | } |
| 2096 | |
| 2097 | static int rbd_img_fill_request_nocopy(struct rbd_img_request *img_req, |
| 2098 | struct ceph_file_extent *img_extents, |
| 2099 | u32 num_img_extents, |
| 2100 | struct rbd_img_fill_ctx *fctx) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2101 | { |
| 2102 | u32 i; |
| 2103 | int ret; |
| 2104 | |
| 2105 | img_req->data_type = fctx->pos_type; |
| 2106 | |
| 2107 | /* |
| 2108 | * Create object requests and set each object request's starting |
| 2109 | * position in the provided bio (list) or bio_vec array. |
| 2110 | */ |
| 2111 | fctx->iter = *fctx->pos; |
| 2112 | for (i = 0; i < num_img_extents; i++) { |
| 2113 | ret = ceph_file_to_extents(&img_req->rbd_dev->layout, |
| 2114 | img_extents[i].fe_off, |
| 2115 | img_extents[i].fe_len, |
| 2116 | &img_req->object_extents, |
| 2117 | alloc_object_extent, img_req, |
| 2118 | fctx->set_pos_fn, &fctx->iter); |
| 2119 | if (ret) |
| 2120 | return ret; |
| 2121 | } |
| 2122 | |
| 2123 | return __rbd_img_fill_request(img_req); |
| 2124 | } |
| 2125 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2126 | /* |
| 2127 | * Map a list of image extents to a list of object extents, create the |
| 2128 | * corresponding object requests (normally each to a different object, |
| 2129 | * but not always) and add them to @img_req. For each object request, |
| 2130 | * set up its data descriptor to point to the corresponding chunk(s) of |
| 2131 | * @fctx->pos data buffer. |
| 2132 | * |
| 2133 | * Because ceph_file_to_extents() will merge adjacent object extents |
| 2134 | * together, each object request's data descriptor may point to multiple |
| 2135 | * different chunks of @fctx->pos data buffer. |
| 2136 | * |
| 2137 | * @fctx->pos data buffer is assumed to be large enough. |
| 2138 | */ |
| 2139 | static int rbd_img_fill_request(struct rbd_img_request *img_req, |
| 2140 | struct ceph_file_extent *img_extents, |
| 2141 | u32 num_img_extents, |
| 2142 | struct rbd_img_fill_ctx *fctx) |
| 2143 | { |
| 2144 | struct rbd_device *rbd_dev = img_req->rbd_dev; |
| 2145 | struct rbd_obj_request *obj_req; |
| 2146 | u32 i; |
| 2147 | int ret; |
| 2148 | |
| 2149 | if (fctx->pos_type == OBJ_REQUEST_NODATA || |
| 2150 | !rbd_layout_is_fancy(&rbd_dev->layout)) |
| 2151 | return rbd_img_fill_request_nocopy(img_req, img_extents, |
| 2152 | num_img_extents, fctx); |
| 2153 | |
| 2154 | img_req->data_type = OBJ_REQUEST_OWN_BVECS; |
| 2155 | |
| 2156 | /* |
| 2157 | * Create object requests and determine ->bvec_count for each object |
| 2158 | * request. Note that ->bvec_count sum over all object requests may |
| 2159 | * be greater than the number of bio_vecs in the provided bio (list) |
| 2160 | * or bio_vec array because when mapped, those bio_vecs can straddle |
| 2161 | * stripe unit boundaries. |
| 2162 | */ |
| 2163 | fctx->iter = *fctx->pos; |
| 2164 | for (i = 0; i < num_img_extents; i++) { |
| 2165 | ret = ceph_file_to_extents(&rbd_dev->layout, |
| 2166 | img_extents[i].fe_off, |
| 2167 | img_extents[i].fe_len, |
| 2168 | &img_req->object_extents, |
| 2169 | alloc_object_extent, img_req, |
| 2170 | fctx->count_fn, &fctx->iter); |
| 2171 | if (ret) |
| 2172 | return ret; |
| 2173 | } |
| 2174 | |
| 2175 | for_each_obj_request(img_req, obj_req) { |
| 2176 | obj_req->bvec_pos.bvecs = kmalloc_array(obj_req->bvec_count, |
| 2177 | sizeof(*obj_req->bvec_pos.bvecs), |
| 2178 | GFP_NOIO); |
| 2179 | if (!obj_req->bvec_pos.bvecs) |
| 2180 | return -ENOMEM; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | /* |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2184 | * Fill in each object request's private bio_vec array, splitting and |
| 2185 | * rearranging the provided bio_vecs in stripe unit chunks as needed. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2186 | */ |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2187 | fctx->iter = *fctx->pos; |
| 2188 | for (i = 0; i < num_img_extents; i++) { |
| 2189 | ret = ceph_iterate_extents(&rbd_dev->layout, |
| 2190 | img_extents[i].fe_off, |
| 2191 | img_extents[i].fe_len, |
| 2192 | &img_req->object_extents, |
| 2193 | fctx->copy_fn, &fctx->iter); |
| 2194 | if (ret) |
| 2195 | return ret; |
| 2196 | } |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2197 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2198 | return __rbd_img_fill_request(img_req); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2199 | } |
| 2200 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2201 | static int rbd_img_fill_nodata(struct rbd_img_request *img_req, |
| 2202 | u64 off, u64 len) |
| 2203 | { |
| 2204 | struct ceph_file_extent ex = { off, len }; |
| 2205 | union rbd_img_fill_iter dummy; |
| 2206 | struct rbd_img_fill_ctx fctx = { |
| 2207 | .pos_type = OBJ_REQUEST_NODATA, |
| 2208 | .pos = &dummy, |
| 2209 | }; |
| 2210 | |
| 2211 | return rbd_img_fill_request(img_req, &ex, 1, &fctx); |
| 2212 | } |
| 2213 | |
| 2214 | static void set_bio_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2215 | { |
| 2216 | struct rbd_obj_request *obj_req = |
| 2217 | container_of(ex, struct rbd_obj_request, ex); |
| 2218 | struct ceph_bio_iter *it = arg; |
| 2219 | |
| 2220 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2221 | obj_req->bio_pos = *it; |
| 2222 | ceph_bio_iter_advance(it, bytes); |
| 2223 | } |
| 2224 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2225 | static void count_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2226 | { |
| 2227 | struct rbd_obj_request *obj_req = |
| 2228 | container_of(ex, struct rbd_obj_request, ex); |
| 2229 | struct ceph_bio_iter *it = arg; |
| 2230 | |
| 2231 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2232 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2233 | obj_req->bvec_count++; |
| 2234 | })); |
| 2235 | |
| 2236 | } |
| 2237 | |
| 2238 | static void copy_bio_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2239 | { |
| 2240 | struct rbd_obj_request *obj_req = |
| 2241 | container_of(ex, struct rbd_obj_request, ex); |
| 2242 | struct ceph_bio_iter *it = arg; |
| 2243 | |
| 2244 | dout("%s objno %llu bytes %u\n", __func__, ex->oe_objno, bytes); |
| 2245 | ceph_bio_iter_advance_step(it, bytes, ({ |
| 2246 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2247 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2248 | })); |
| 2249 | } |
| 2250 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2251 | static int __rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2252 | struct ceph_file_extent *img_extents, |
| 2253 | u32 num_img_extents, |
| 2254 | struct ceph_bio_iter *bio_pos) |
| 2255 | { |
| 2256 | struct rbd_img_fill_ctx fctx = { |
| 2257 | .pos_type = OBJ_REQUEST_BIO, |
| 2258 | .pos = (union rbd_img_fill_iter *)bio_pos, |
| 2259 | .set_pos_fn = set_bio_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2260 | .count_fn = count_bio_bvecs, |
| 2261 | .copy_fn = copy_bio_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2262 | }; |
| 2263 | |
| 2264 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2265 | &fctx); |
| 2266 | } |
| 2267 | |
| 2268 | static int rbd_img_fill_from_bio(struct rbd_img_request *img_req, |
| 2269 | u64 off, u64 len, struct bio *bio) |
| 2270 | { |
| 2271 | struct ceph_file_extent ex = { off, len }; |
| 2272 | struct ceph_bio_iter it = { .bio = bio, .iter = bio->bi_iter }; |
| 2273 | |
| 2274 | return __rbd_img_fill_from_bio(img_req, &ex, 1, &it); |
| 2275 | } |
| 2276 | |
| 2277 | static void set_bvec_pos(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2278 | { |
| 2279 | struct rbd_obj_request *obj_req = |
| 2280 | container_of(ex, struct rbd_obj_request, ex); |
| 2281 | struct ceph_bvec_iter *it = arg; |
| 2282 | |
| 2283 | obj_req->bvec_pos = *it; |
| 2284 | ceph_bvec_iter_shorten(&obj_req->bvec_pos, bytes); |
| 2285 | ceph_bvec_iter_advance(it, bytes); |
| 2286 | } |
| 2287 | |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2288 | static void count_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2289 | { |
| 2290 | struct rbd_obj_request *obj_req = |
| 2291 | container_of(ex, struct rbd_obj_request, ex); |
| 2292 | struct ceph_bvec_iter *it = arg; |
| 2293 | |
| 2294 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2295 | obj_req->bvec_count++; |
| 2296 | })); |
| 2297 | } |
| 2298 | |
| 2299 | static void copy_bvecs(struct ceph_object_extent *ex, u32 bytes, void *arg) |
| 2300 | { |
| 2301 | struct rbd_obj_request *obj_req = |
| 2302 | container_of(ex, struct rbd_obj_request, ex); |
| 2303 | struct ceph_bvec_iter *it = arg; |
| 2304 | |
| 2305 | ceph_bvec_iter_advance_step(it, bytes, ({ |
| 2306 | obj_req->bvec_pos.bvecs[obj_req->bvec_idx++] = bv; |
| 2307 | obj_req->bvec_pos.iter.bi_size += bv.bv_len; |
| 2308 | })); |
| 2309 | } |
| 2310 | |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2311 | static int __rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2312 | struct ceph_file_extent *img_extents, |
| 2313 | u32 num_img_extents, |
| 2314 | struct ceph_bvec_iter *bvec_pos) |
| 2315 | { |
| 2316 | struct rbd_img_fill_ctx fctx = { |
| 2317 | .pos_type = OBJ_REQUEST_BVECS, |
| 2318 | .pos = (union rbd_img_fill_iter *)bvec_pos, |
| 2319 | .set_pos_fn = set_bvec_pos, |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2320 | .count_fn = count_bvecs, |
| 2321 | .copy_fn = copy_bvecs, |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2322 | }; |
| 2323 | |
| 2324 | return rbd_img_fill_request(img_req, img_extents, num_img_extents, |
| 2325 | &fctx); |
| 2326 | } |
| 2327 | |
| 2328 | static int rbd_img_fill_from_bvecs(struct rbd_img_request *img_req, |
| 2329 | struct ceph_file_extent *img_extents, |
| 2330 | u32 num_img_extents, |
| 2331 | struct bio_vec *bvecs) |
| 2332 | { |
| 2333 | struct ceph_bvec_iter it = { |
| 2334 | .bvecs = bvecs, |
| 2335 | .iter = { .bi_size = ceph_file_extents_bytes(img_extents, |
| 2336 | num_img_extents) }, |
| 2337 | }; |
| 2338 | |
| 2339 | return __rbd_img_fill_from_bvecs(img_req, img_extents, num_img_extents, |
| 2340 | &it); |
| 2341 | } |
| 2342 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2343 | static void rbd_img_request_submit(struct rbd_img_request *img_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2344 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2345 | struct rbd_obj_request *obj_request; |
| 2346 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2347 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2348 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2349 | rbd_img_request_get(img_request); |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 2350 | for_each_obj_request(img_request, obj_request) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2351 | rbd_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2352 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 2353 | rbd_img_request_put(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2354 | } |
| 2355 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2356 | 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] | 2357 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2358 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2359 | struct rbd_img_request *child_img_req; |
| 2360 | int ret; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2361 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2362 | child_img_req = rbd_img_request_create(img_req->rbd_dev->parent, |
| 2363 | OBJ_OP_READ, NULL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2364 | if (!child_img_req) |
| 2365 | return -ENOMEM; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2366 | |
Ilya Dryomov | e93aca0 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2367 | __set_bit(IMG_REQ_CHILD, &child_img_req->flags); |
| 2368 | child_img_req->obj_request = obj_req; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2369 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2370 | if (!rbd_img_is_write(img_req)) { |
Ilya Dryomov | ecc633c | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2371 | switch (img_req->data_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2372 | case OBJ_REQUEST_BIO: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2373 | ret = __rbd_img_fill_from_bio(child_img_req, |
| 2374 | obj_req->img_extents, |
| 2375 | obj_req->num_img_extents, |
| 2376 | &obj_req->bio_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2377 | break; |
| 2378 | case OBJ_REQUEST_BVECS: |
Ilya Dryomov | afb9788 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 2379 | case OBJ_REQUEST_OWN_BVECS: |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2380 | ret = __rbd_img_fill_from_bvecs(child_img_req, |
| 2381 | obj_req->img_extents, |
| 2382 | obj_req->num_img_extents, |
| 2383 | &obj_req->bvec_pos); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2384 | break; |
| 2385 | default: |
Arnd Bergmann | d342a15 | 2019-03-22 15:36:37 +0100 | [diff] [blame] | 2386 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2387 | } |
| 2388 | } else { |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 2389 | ret = rbd_img_fill_from_bvecs(child_img_req, |
| 2390 | obj_req->img_extents, |
| 2391 | obj_req->num_img_extents, |
| 2392 | obj_req->copyup_bvecs); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2393 | } |
| 2394 | if (ret) { |
| 2395 | rbd_img_request_put(child_img_req); |
| 2396 | return ret; |
| 2397 | } |
| 2398 | |
| 2399 | rbd_img_request_submit(child_img_req); |
| 2400 | return 0; |
| 2401 | } |
| 2402 | |
| 2403 | static bool rbd_obj_handle_read(struct rbd_obj_request *obj_req) |
| 2404 | { |
| 2405 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
| 2406 | int ret; |
| 2407 | |
| 2408 | if (obj_req->result == -ENOENT && |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2409 | rbd_dev->parent_overlap && !obj_req->tried_parent) { |
| 2410 | /* reverse map this object extent onto the parent */ |
| 2411 | ret = rbd_obj_calc_img_extents(obj_req, false); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2412 | if (ret) { |
| 2413 | obj_req->result = ret; |
| 2414 | return true; |
| 2415 | } |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2416 | |
| 2417 | if (obj_req->num_img_extents) { |
| 2418 | obj_req->tried_parent = true; |
| 2419 | ret = rbd_obj_read_from_parent(obj_req); |
| 2420 | if (ret) { |
| 2421 | obj_req->result = ret; |
| 2422 | return true; |
| 2423 | } |
| 2424 | return false; |
| 2425 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2426 | } |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2427 | |
| 2428 | /* |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2429 | * -ENOENT means a hole in the image -- zero-fill the entire |
| 2430 | * length of the request. A short read also implies zero-fill |
| 2431 | * to the end of the request. In both cases we update xferred |
| 2432 | * count to indicate the whole request was satisfied. |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2433 | */ |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2434 | if (obj_req->result == -ENOENT || |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2435 | (!obj_req->result && obj_req->xferred < obj_req->ex.oe_len)) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2436 | rbd_assert(!obj_req->xferred || !obj_req->result); |
| 2437 | rbd_obj_zero_range(obj_req, obj_req->xferred, |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2438 | obj_req->ex.oe_len - obj_req->xferred); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2439 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2440 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
| 2443 | return true; |
| 2444 | } |
| 2445 | |
| 2446 | /* |
| 2447 | * copyup_bvecs pages are never highmem pages |
| 2448 | */ |
| 2449 | static bool is_zero_bvecs(struct bio_vec *bvecs, u32 bytes) |
| 2450 | { |
| 2451 | struct ceph_bvec_iter it = { |
| 2452 | .bvecs = bvecs, |
| 2453 | .iter = { .bi_size = bytes }, |
| 2454 | }; |
| 2455 | |
| 2456 | ceph_bvec_iter_advance_step(&it, bytes, ({ |
| 2457 | if (memchr_inv(page_address(bv.bv_page) + bv.bv_offset, 0, |
| 2458 | bv.bv_len)) |
| 2459 | return false; |
| 2460 | })); |
| 2461 | return true; |
| 2462 | } |
| 2463 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2464 | #define MODS_ONLY U32_MAX |
| 2465 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2466 | static int rbd_obj_issue_copyup_empty_snapc(struct rbd_obj_request *obj_req, |
| 2467 | u32 bytes) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2468 | { |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2469 | int ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2470 | |
| 2471 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
| 2472 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2473 | rbd_assert(bytes > 0 && bytes != MODS_ONLY); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2474 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2475 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2476 | obj_req->osd_req = __rbd_osd_req_create(obj_req, &rbd_empty_snapc, 1); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2477 | if (!obj_req->osd_req) |
| 2478 | return -ENOMEM; |
| 2479 | |
Ilya Dryomov | 24639ce56 | 2018-09-26 19:12:07 +0200 | [diff] [blame] | 2480 | ret = osd_req_op_cls_init(obj_req->osd_req, 0, "rbd", "copyup"); |
Chengguang Xu | fe943d5 | 2018-04-12 12:04:55 +0800 | [diff] [blame] | 2481 | if (ret) |
| 2482 | return ret; |
| 2483 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2484 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, 0, |
Ilya Dryomov | 0010f70 | 2018-05-04 16:57:30 +0200 | [diff] [blame] | 2485 | obj_req->copyup_bvecs, |
| 2486 | obj_req->copyup_bvec_count, |
| 2487 | bytes); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2488 | rbd_osd_req_format_write(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2489 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2490 | ret = ceph_osdc_alloc_messages(obj_req->osd_req, GFP_NOIO); |
| 2491 | if (ret) |
| 2492 | return ret; |
| 2493 | |
| 2494 | rbd_obj_request_submit(obj_req); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2498 | static int rbd_obj_issue_copyup_ops(struct rbd_obj_request *obj_req, u32 bytes) |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2499 | { |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2500 | struct rbd_img_request *img_req = obj_req->img_request; |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2501 | unsigned int num_osd_ops = (bytes != MODS_ONLY); |
| 2502 | unsigned int which = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2503 | int ret; |
| 2504 | |
| 2505 | dout("%s obj_req %p bytes %u\n", __func__, obj_req, bytes); |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2506 | rbd_assert(obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_STAT || |
| 2507 | obj_req->osd_req->r_ops[0].op == CEPH_OSD_OP_CALL); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2508 | rbd_osd_req_destroy(obj_req->osd_req); |
| 2509 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2510 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2511 | case OBJ_OP_WRITE: |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2512 | num_osd_ops += count_write_ops(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2513 | break; |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2514 | case OBJ_OP_ZEROOUT: |
| 2515 | num_osd_ops += count_zeroout_ops(obj_req); |
| 2516 | break; |
| 2517 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 2518 | BUG(); |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2519 | } |
| 2520 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2521 | obj_req->osd_req = rbd_osd_req_create(obj_req, num_osd_ops); |
| 2522 | if (!obj_req->osd_req) |
| 2523 | return -ENOMEM; |
| 2524 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2525 | if (bytes != MODS_ONLY) { |
| 2526 | ret = osd_req_op_cls_init(obj_req->osd_req, which, "rbd", |
| 2527 | "copyup"); |
| 2528 | if (ret) |
| 2529 | return ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2530 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2531 | osd_req_op_cls_request_data_bvecs(obj_req->osd_req, which++, |
| 2532 | obj_req->copyup_bvecs, |
| 2533 | obj_req->copyup_bvec_count, |
| 2534 | bytes); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2535 | } |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2536 | |
Ilya Dryomov | 13488d5 | 2019-02-25 12:37:50 +0100 | [diff] [blame] | 2537 | switch (img_req->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2538 | case OBJ_OP_WRITE: |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2539 | __rbd_obj_setup_write(obj_req, which); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2540 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2541 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2542 | __rbd_obj_setup_zeroout(obj_req, which); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2543 | break; |
| 2544 | default: |
Arnd Bergmann | 1680937 | 2019-03-22 17:53:56 +0100 | [diff] [blame^] | 2545 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2546 | } |
| 2547 | |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 2548 | ret = ceph_osdc_alloc_messages(obj_req->osd_req, GFP_NOIO); |
| 2549 | if (ret) |
| 2550 | return ret; |
| 2551 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2552 | rbd_obj_request_submit(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2553 | return 0; |
| 2554 | } |
| 2555 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2556 | static int rbd_obj_issue_copyup(struct rbd_obj_request *obj_req, u32 bytes) |
| 2557 | { |
| 2558 | /* |
| 2559 | * Only send non-zero copyup data to save some I/O and network |
| 2560 | * bandwidth -- zero copyup data is equivalent to the object not |
| 2561 | * existing. |
| 2562 | */ |
| 2563 | if (is_zero_bvecs(obj_req->copyup_bvecs, bytes)) { |
| 2564 | dout("%s obj_req %p detected zeroes\n", __func__, obj_req); |
| 2565 | bytes = 0; |
| 2566 | } |
| 2567 | |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2568 | if (obj_req->img_request->snapc->num_snaps && bytes > 0) { |
| 2569 | /* |
| 2570 | * Send a copyup request with an empty snapshot context to |
| 2571 | * deep-copyup the object through all existing snapshots. |
| 2572 | * A second request with the current snapshot context will be |
| 2573 | * sent for the actual modification. |
| 2574 | */ |
| 2575 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC; |
| 2576 | return rbd_obj_issue_copyup_empty_snapc(obj_req, bytes); |
| 2577 | } |
| 2578 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2579 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP_OPS; |
| 2580 | return rbd_obj_issue_copyup_ops(obj_req, bytes); |
| 2581 | } |
| 2582 | |
Ilya Dryomov | 7e07efb | 2018-01-20 10:30:11 +0100 | [diff] [blame] | 2583 | static int setup_copyup_bvecs(struct rbd_obj_request *obj_req, u64 obj_overlap) |
| 2584 | { |
| 2585 | u32 i; |
| 2586 | |
| 2587 | rbd_assert(!obj_req->copyup_bvecs); |
| 2588 | obj_req->copyup_bvec_count = calc_pages_for(0, obj_overlap); |
| 2589 | obj_req->copyup_bvecs = kcalloc(obj_req->copyup_bvec_count, |
| 2590 | sizeof(*obj_req->copyup_bvecs), |
| 2591 | GFP_NOIO); |
| 2592 | if (!obj_req->copyup_bvecs) |
| 2593 | return -ENOMEM; |
| 2594 | |
| 2595 | for (i = 0; i < obj_req->copyup_bvec_count; i++) { |
| 2596 | unsigned int len = min(obj_overlap, (u64)PAGE_SIZE); |
| 2597 | |
| 2598 | obj_req->copyup_bvecs[i].bv_page = alloc_page(GFP_NOIO); |
| 2599 | if (!obj_req->copyup_bvecs[i].bv_page) |
| 2600 | return -ENOMEM; |
| 2601 | |
| 2602 | obj_req->copyup_bvecs[i].bv_offset = 0; |
| 2603 | obj_req->copyup_bvecs[i].bv_len = len; |
| 2604 | obj_overlap -= len; |
| 2605 | } |
| 2606 | |
| 2607 | rbd_assert(!obj_overlap); |
| 2608 | return 0; |
| 2609 | } |
| 2610 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2611 | static int rbd_obj_handle_write_guard(struct rbd_obj_request *obj_req) |
| 2612 | { |
| 2613 | struct rbd_device *rbd_dev = obj_req->img_request->rbd_dev; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2614 | int ret; |
| 2615 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2616 | rbd_assert(obj_req->num_img_extents); |
| 2617 | prune_extents(obj_req->img_extents, &obj_req->num_img_extents, |
| 2618 | rbd_dev->parent_overlap); |
| 2619 | if (!obj_req->num_img_extents) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2620 | /* |
| 2621 | * The overlap has become 0 (most likely because the |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2622 | * image has been flattened). Re-submit the original write |
| 2623 | * request -- pass MODS_ONLY since the copyup isn't needed |
| 2624 | * anymore. |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2625 | */ |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2626 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP_OPS; |
| 2627 | return rbd_obj_issue_copyup_ops(obj_req, MODS_ONLY); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2628 | } |
| 2629 | |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2630 | 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] | 2631 | if (ret) |
| 2632 | return ret; |
| 2633 | |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2634 | obj_req->write_state = RBD_OBJ_WRITE_READ_FROM_PARENT; |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2635 | return rbd_obj_read_from_parent(obj_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static bool rbd_obj_handle_write(struct rbd_obj_request *obj_req) |
| 2639 | { |
| 2640 | int ret; |
| 2641 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2642 | switch (obj_req->write_state) { |
| 2643 | case RBD_OBJ_WRITE_GUARD: |
| 2644 | rbd_assert(!obj_req->xferred); |
| 2645 | if (obj_req->result == -ENOENT) { |
| 2646 | /* |
| 2647 | * The target object doesn't exist. Read the data for |
| 2648 | * the entire target object up to the overlap point (if |
| 2649 | * any) from the parent, so we can use it for a copyup. |
| 2650 | */ |
| 2651 | ret = rbd_obj_handle_write_guard(obj_req); |
| 2652 | if (ret) { |
| 2653 | obj_req->result = ret; |
| 2654 | return true; |
| 2655 | } |
| 2656 | return false; |
| 2657 | } |
| 2658 | /* fall through */ |
| 2659 | case RBD_OBJ_WRITE_FLAT: |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2660 | case RBD_OBJ_WRITE_COPYUP_OPS: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2661 | if (!obj_req->result) |
| 2662 | /* |
| 2663 | * There is no such thing as a successful short |
| 2664 | * write -- indicate the whole request was satisfied. |
| 2665 | */ |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2666 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2667 | return true; |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2668 | case RBD_OBJ_WRITE_READ_FROM_PARENT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2669 | if (obj_req->result) |
Ilya Dryomov | 3a48250 | 2019-02-28 10:49:12 +0100 | [diff] [blame] | 2670 | return true; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2671 | |
| 2672 | rbd_assert(obj_req->xferred); |
| 2673 | ret = rbd_obj_issue_copyup(obj_req, obj_req->xferred); |
| 2674 | if (ret) { |
| 2675 | obj_req->result = ret; |
Ilya Dryomov | 356889c | 2019-03-01 12:06:24 +0100 | [diff] [blame] | 2676 | obj_req->xferred = 0; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2677 | return true; |
| 2678 | } |
| 2679 | return false; |
Ilya Dryomov | 89a59c1 | 2019-02-28 14:20:28 +0100 | [diff] [blame] | 2680 | case RBD_OBJ_WRITE_COPYUP_EMPTY_SNAPC: |
| 2681 | if (obj_req->result) |
| 2682 | return true; |
| 2683 | |
| 2684 | obj_req->write_state = RBD_OBJ_WRITE_COPYUP_OPS; |
| 2685 | ret = rbd_obj_issue_copyup_ops(obj_req, MODS_ONLY); |
| 2686 | if (ret) { |
| 2687 | obj_req->result = ret; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2688 | return true; |
| 2689 | } |
| 2690 | return false; |
| 2691 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2692 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | /* |
| 2697 | * Returns true if @obj_req is completed, or false otherwise. |
| 2698 | */ |
| 2699 | static bool __rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2700 | { |
Ilya Dryomov | 9bb0248 | 2018-01-30 17:52:10 +0100 | [diff] [blame] | 2701 | switch (obj_req->img_request->op_type) { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2702 | case OBJ_OP_READ: |
| 2703 | return rbd_obj_handle_read(obj_req); |
| 2704 | case OBJ_OP_WRITE: |
| 2705 | return rbd_obj_handle_write(obj_req); |
| 2706 | case OBJ_OP_DISCARD: |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 2707 | case OBJ_OP_ZEROOUT: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2708 | if (rbd_obj_handle_write(obj_req)) { |
| 2709 | /* |
| 2710 | * Hide -ENOENT from delete/truncate/zero -- discarding |
| 2711 | * a non-existent object is not a problem. |
| 2712 | */ |
| 2713 | if (obj_req->result == -ENOENT) { |
| 2714 | obj_req->result = 0; |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2715 | obj_req->xferred = obj_req->ex.oe_len; |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2716 | } |
| 2717 | return true; |
| 2718 | } |
| 2719 | return false; |
| 2720 | default: |
Arnd Bergmann | c6244b3 | 2018-04-04 14:53:39 +0200 | [diff] [blame] | 2721 | BUG(); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2722 | } |
| 2723 | } |
| 2724 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2725 | static void rbd_obj_end_request(struct rbd_obj_request *obj_req) |
| 2726 | { |
| 2727 | struct rbd_img_request *img_req = obj_req->img_request; |
| 2728 | |
| 2729 | rbd_assert((!obj_req->result && |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2730 | obj_req->xferred == obj_req->ex.oe_len) || |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2731 | (obj_req->result < 0 && !obj_req->xferred)); |
| 2732 | if (!obj_req->result) { |
| 2733 | img_req->xferred += obj_req->xferred; |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2734 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2735 | } |
| 2736 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2737 | rbd_warn(img_req->rbd_dev, |
| 2738 | "%s at objno %llu %llu~%llu result %d xferred %llu", |
Ilya Dryomov | 43df3d3 | 2018-02-02 15:23:22 +0100 | [diff] [blame] | 2739 | obj_op_name(img_req->op_type), obj_req->ex.oe_objno, |
| 2740 | obj_req->ex.oe_off, obj_req->ex.oe_len, obj_req->result, |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2741 | obj_req->xferred); |
| 2742 | if (!img_req->result) { |
| 2743 | img_req->result = obj_req->result; |
| 2744 | img_req->xferred = 0; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2745 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2746 | } |
| 2747 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2748 | static void rbd_img_end_child_request(struct rbd_img_request *img_req) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2749 | { |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2750 | struct rbd_obj_request *obj_req = img_req->obj_request; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2751 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2752 | rbd_assert(test_bit(IMG_REQ_CHILD, &img_req->flags)); |
Ilya Dryomov | 86bd799 | 2018-02-06 19:26:33 +0100 | [diff] [blame] | 2753 | rbd_assert((!img_req->result && |
| 2754 | img_req->xferred == rbd_obj_img_extents_bytes(obj_req)) || |
| 2755 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2756 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2757 | obj_req->result = img_req->result; |
| 2758 | obj_req->xferred = img_req->xferred; |
| 2759 | rbd_img_request_put(img_req); |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2760 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2761 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2762 | static void rbd_img_end_request(struct rbd_img_request *img_req) |
| 2763 | { |
| 2764 | rbd_assert(!test_bit(IMG_REQ_CHILD, &img_req->flags)); |
| 2765 | rbd_assert((!img_req->result && |
| 2766 | img_req->xferred == blk_rq_bytes(img_req->rq)) || |
| 2767 | (img_req->result < 0 && !img_req->xferred)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2768 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2769 | blk_mq_end_request(img_req->rq, |
| 2770 | errno_to_blk_status(img_req->result)); |
| 2771 | rbd_img_request_put(img_req); |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2772 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2773 | |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2774 | static void rbd_obj_handle_request(struct rbd_obj_request *obj_req) |
| 2775 | { |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2776 | struct rbd_img_request *img_req; |
| 2777 | |
| 2778 | again: |
Ilya Dryomov | 3da691b | 2018-01-29 14:04:08 +0100 | [diff] [blame] | 2779 | if (!__rbd_obj_handle_request(obj_req)) |
| 2780 | return; |
| 2781 | |
Ilya Dryomov | 7114eda | 2018-02-01 11:50:47 +0100 | [diff] [blame] | 2782 | img_req = obj_req->img_request; |
| 2783 | spin_lock(&img_req->completion_lock); |
| 2784 | rbd_obj_end_request(obj_req); |
| 2785 | rbd_assert(img_req->pending_count); |
| 2786 | if (--img_req->pending_count) { |
| 2787 | spin_unlock(&img_req->completion_lock); |
| 2788 | return; |
| 2789 | } |
| 2790 | |
| 2791 | spin_unlock(&img_req->completion_lock); |
| 2792 | if (test_bit(IMG_REQ_CHILD, &img_req->flags)) { |
| 2793 | obj_req = img_req->obj_request; |
| 2794 | rbd_img_end_child_request(img_req); |
| 2795 | goto again; |
| 2796 | } |
| 2797 | rbd_img_end_request(img_req); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2798 | } |
| 2799 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2800 | static const struct rbd_client_id rbd_empty_cid; |
| 2801 | |
| 2802 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 2803 | const struct rbd_client_id *rhs) |
| 2804 | { |
| 2805 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 2806 | } |
| 2807 | |
| 2808 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 2809 | { |
| 2810 | struct rbd_client_id cid; |
| 2811 | |
| 2812 | mutex_lock(&rbd_dev->watch_mutex); |
| 2813 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 2814 | cid.handle = rbd_dev->watch_cookie; |
| 2815 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2816 | return cid; |
| 2817 | } |
| 2818 | |
| 2819 | /* |
| 2820 | * lock_rwsem must be held for write |
| 2821 | */ |
| 2822 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 2823 | const struct rbd_client_id *cid) |
| 2824 | { |
| 2825 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 2826 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 2827 | cid->gid, cid->handle); |
| 2828 | rbd_dev->owner_cid = *cid; /* struct */ |
| 2829 | } |
| 2830 | |
| 2831 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 2832 | { |
| 2833 | mutex_lock(&rbd_dev->watch_mutex); |
| 2834 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 2835 | mutex_unlock(&rbd_dev->watch_mutex); |
| 2836 | } |
| 2837 | |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2838 | static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie) |
| 2839 | { |
| 2840 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 2841 | |
| 2842 | strcpy(rbd_dev->lock_cookie, cookie); |
| 2843 | rbd_set_owner_cid(rbd_dev, &cid); |
| 2844 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 2845 | } |
| 2846 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2847 | /* |
| 2848 | * lock_rwsem must be held for write |
| 2849 | */ |
| 2850 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 2851 | { |
| 2852 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2853 | char cookie[32]; |
| 2854 | int ret; |
| 2855 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2856 | WARN_ON(__rbd_is_lock_owner(rbd_dev) || |
| 2857 | rbd_dev->lock_cookie[0] != '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2858 | |
| 2859 | format_lock_cookie(rbd_dev, cookie); |
| 2860 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 2861 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 2862 | RBD_LOCK_TAG, "", 0); |
| 2863 | if (ret) |
| 2864 | return ret; |
| 2865 | |
| 2866 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 2867 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2868 | return 0; |
| 2869 | } |
| 2870 | |
| 2871 | /* |
| 2872 | * lock_rwsem must be held for write |
| 2873 | */ |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2874 | static void rbd_unlock(struct rbd_device *rbd_dev) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2875 | { |
| 2876 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2877 | int ret; |
| 2878 | |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2879 | WARN_ON(!__rbd_is_lock_owner(rbd_dev) || |
| 2880 | rbd_dev->lock_cookie[0] == '\0'); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2881 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2882 | 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] | 2883 | RBD_LOCK_NAME, rbd_dev->lock_cookie); |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2884 | if (ret && ret != -ENOENT) |
| 2885 | rbd_warn(rbd_dev, "failed to unlock: %d", ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2886 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2887 | /* treat errors as the image is unlocked */ |
| 2888 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
Ilya Dryomov | cbbfb0f | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 2889 | rbd_dev->lock_cookie[0] = '\0'; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2890 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 2891 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2895 | enum rbd_notify_op notify_op, |
| 2896 | struct page ***preply_pages, |
| 2897 | size_t *preply_len) |
| 2898 | { |
| 2899 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 2900 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 2901 | char buf[4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN]; |
| 2902 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 2903 | void *p = buf; |
| 2904 | |
| 2905 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 2906 | |
| 2907 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 2908 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 2909 | ceph_encode_32(&p, notify_op); |
| 2910 | ceph_encode_64(&p, cid.gid); |
| 2911 | ceph_encode_64(&p, cid.handle); |
| 2912 | |
| 2913 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 2914 | &rbd_dev->header_oloc, buf, buf_size, |
| 2915 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 2916 | } |
| 2917 | |
| 2918 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 2919 | enum rbd_notify_op notify_op) |
| 2920 | { |
| 2921 | struct page **reply_pages; |
| 2922 | size_t reply_len; |
| 2923 | |
| 2924 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 2925 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 2926 | } |
| 2927 | |
| 2928 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 2929 | { |
| 2930 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2931 | acquired_lock_work); |
| 2932 | |
| 2933 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 2934 | } |
| 2935 | |
| 2936 | static void rbd_notify_released_lock(struct work_struct *work) |
| 2937 | { |
| 2938 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 2939 | released_lock_work); |
| 2940 | |
| 2941 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 2942 | } |
| 2943 | |
| 2944 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 2945 | { |
| 2946 | struct page **reply_pages; |
| 2947 | size_t reply_len; |
| 2948 | bool lock_owner_responded = false; |
| 2949 | int ret; |
| 2950 | |
| 2951 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 2952 | |
| 2953 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 2954 | &reply_pages, &reply_len); |
| 2955 | if (ret && ret != -ETIMEDOUT) { |
| 2956 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 2957 | goto out; |
| 2958 | } |
| 2959 | |
| 2960 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 2961 | void *p = page_address(reply_pages[0]); |
| 2962 | void *const end = p + reply_len; |
| 2963 | u32 n; |
| 2964 | |
| 2965 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 2966 | while (n--) { |
| 2967 | u8 struct_v; |
| 2968 | u32 len; |
| 2969 | |
| 2970 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 2971 | p += 8 + 8; /* skip gid and cookie */ |
| 2972 | |
| 2973 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 2974 | if (!len) |
| 2975 | continue; |
| 2976 | |
| 2977 | if (lock_owner_responded) { |
| 2978 | rbd_warn(rbd_dev, |
| 2979 | "duplicate lock owners detected"); |
| 2980 | ret = -EIO; |
| 2981 | goto out; |
| 2982 | } |
| 2983 | |
| 2984 | lock_owner_responded = true; |
| 2985 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 2986 | &struct_v, &len); |
| 2987 | if (ret) { |
| 2988 | rbd_warn(rbd_dev, |
| 2989 | "failed to decode ResponseMessage: %d", |
| 2990 | ret); |
| 2991 | goto e_inval; |
| 2992 | } |
| 2993 | |
| 2994 | ret = ceph_decode_32(&p); |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | if (!lock_owner_responded) { |
| 2999 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3000 | ret = -ETIMEDOUT; |
| 3001 | } |
| 3002 | |
| 3003 | out: |
| 3004 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3005 | return ret; |
| 3006 | |
| 3007 | e_inval: |
| 3008 | ret = -EINVAL; |
| 3009 | goto out; |
| 3010 | } |
| 3011 | |
| 3012 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3013 | { |
| 3014 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3015 | |
| 3016 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3017 | if (wake_all) |
| 3018 | wake_up_all(&rbd_dev->lock_waitq); |
| 3019 | else |
| 3020 | wake_up(&rbd_dev->lock_waitq); |
| 3021 | } |
| 3022 | |
| 3023 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3024 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3025 | { |
| 3026 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3027 | u8 lock_type; |
| 3028 | char *lock_tag; |
| 3029 | int ret; |
| 3030 | |
| 3031 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3032 | |
| 3033 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3034 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3035 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3036 | if (ret) |
| 3037 | return ret; |
| 3038 | |
| 3039 | if (*num_lockers == 0) { |
| 3040 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3041 | goto out; |
| 3042 | } |
| 3043 | |
| 3044 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3045 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3046 | lock_tag); |
| 3047 | ret = -EBUSY; |
| 3048 | goto out; |
| 3049 | } |
| 3050 | |
| 3051 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3052 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3053 | ret = -EBUSY; |
| 3054 | goto out; |
| 3055 | } |
| 3056 | |
| 3057 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3058 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3059 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3060 | (*lockers)[0].id.cookie); |
| 3061 | ret = -EBUSY; |
| 3062 | goto out; |
| 3063 | } |
| 3064 | |
| 3065 | out: |
| 3066 | kfree(lock_tag); |
| 3067 | return ret; |
| 3068 | } |
| 3069 | |
| 3070 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3071 | const struct ceph_locker *locker) |
| 3072 | { |
| 3073 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3074 | struct ceph_watch_item *watchers; |
| 3075 | u32 num_watchers; |
| 3076 | u64 cookie; |
| 3077 | int i; |
| 3078 | int ret; |
| 3079 | |
| 3080 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3081 | &rbd_dev->header_oloc, &watchers, |
| 3082 | &num_watchers); |
| 3083 | if (ret) |
| 3084 | return ret; |
| 3085 | |
| 3086 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3087 | for (i = 0; i < num_watchers; i++) { |
| 3088 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3089 | sizeof(locker->info.addr)) && |
| 3090 | watchers[i].cookie == cookie) { |
| 3091 | struct rbd_client_id cid = { |
| 3092 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3093 | .handle = cookie, |
| 3094 | }; |
| 3095 | |
| 3096 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3097 | rbd_dev, cid.gid, cid.handle); |
| 3098 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3099 | ret = 1; |
| 3100 | goto out; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3105 | ret = 0; |
| 3106 | out: |
| 3107 | kfree(watchers); |
| 3108 | return ret; |
| 3109 | } |
| 3110 | |
| 3111 | /* |
| 3112 | * lock_rwsem must be held for write |
| 3113 | */ |
| 3114 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3115 | { |
| 3116 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3117 | struct ceph_locker *lockers; |
| 3118 | u32 num_lockers; |
| 3119 | int ret; |
| 3120 | |
| 3121 | for (;;) { |
| 3122 | ret = rbd_lock(rbd_dev); |
| 3123 | if (ret != -EBUSY) |
| 3124 | return ret; |
| 3125 | |
| 3126 | /* determine if the current lock holder is still alive */ |
| 3127 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3128 | if (ret) |
| 3129 | return ret; |
| 3130 | |
| 3131 | if (num_lockers == 0) |
| 3132 | goto again; |
| 3133 | |
| 3134 | ret = find_watcher(rbd_dev, lockers); |
| 3135 | if (ret) { |
| 3136 | if (ret > 0) |
| 3137 | ret = 0; /* have to request lock */ |
| 3138 | goto out; |
| 3139 | } |
| 3140 | |
| 3141 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3142 | ENTITY_NAME(lockers[0].id.name)); |
| 3143 | |
| 3144 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3145 | &lockers[0].info.addr); |
| 3146 | if (ret) { |
| 3147 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3148 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3149 | goto out; |
| 3150 | } |
| 3151 | |
| 3152 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3153 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3154 | lockers[0].id.cookie, |
| 3155 | &lockers[0].id.name); |
| 3156 | if (ret && ret != -ENOENT) |
| 3157 | goto out; |
| 3158 | |
| 3159 | again: |
| 3160 | ceph_free_lockers(lockers, num_lockers); |
| 3161 | } |
| 3162 | |
| 3163 | out: |
| 3164 | ceph_free_lockers(lockers, num_lockers); |
| 3165 | return ret; |
| 3166 | } |
| 3167 | |
| 3168 | /* |
| 3169 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3170 | */ |
| 3171 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3172 | int *pret) |
| 3173 | { |
| 3174 | enum rbd_lock_state lock_state; |
| 3175 | |
| 3176 | down_read(&rbd_dev->lock_rwsem); |
| 3177 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3178 | rbd_dev->lock_state); |
| 3179 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3180 | lock_state = rbd_dev->lock_state; |
| 3181 | up_read(&rbd_dev->lock_rwsem); |
| 3182 | return lock_state; |
| 3183 | } |
| 3184 | |
| 3185 | up_read(&rbd_dev->lock_rwsem); |
| 3186 | down_write(&rbd_dev->lock_rwsem); |
| 3187 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3188 | rbd_dev->lock_state); |
| 3189 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3190 | *pret = rbd_try_lock(rbd_dev); |
| 3191 | if (*pret) |
| 3192 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3193 | } |
| 3194 | |
| 3195 | lock_state = rbd_dev->lock_state; |
| 3196 | up_write(&rbd_dev->lock_rwsem); |
| 3197 | return lock_state; |
| 3198 | } |
| 3199 | |
| 3200 | static void rbd_acquire_lock(struct work_struct *work) |
| 3201 | { |
| 3202 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3203 | struct rbd_device, lock_dwork); |
| 3204 | enum rbd_lock_state lock_state; |
Kefeng Wang | 37f1325 | 2017-07-13 15:46:35 +0800 | [diff] [blame] | 3205 | int ret = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3206 | |
| 3207 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3208 | again: |
| 3209 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3210 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3211 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3212 | wake_requests(rbd_dev, true); |
| 3213 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3214 | rbd_dev, lock_state, ret); |
| 3215 | return; |
| 3216 | } |
| 3217 | |
| 3218 | ret = rbd_request_lock(rbd_dev); |
| 3219 | if (ret == -ETIMEDOUT) { |
| 3220 | goto again; /* treat this as a dead client */ |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3221 | } else if (ret == -EROFS) { |
| 3222 | rbd_warn(rbd_dev, "peer will not release lock"); |
| 3223 | /* |
| 3224 | * If this is rbd_add_acquire_lock(), we want to fail |
| 3225 | * immediately -- reuse BLACKLISTED flag. Otherwise we |
| 3226 | * want to block. |
| 3227 | */ |
| 3228 | if (!(rbd_dev->disk->flags & GENHD_FL_UP)) { |
| 3229 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
| 3230 | /* wake "rbd map --exclusive" process */ |
| 3231 | wake_requests(rbd_dev, false); |
| 3232 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3233 | } else if (ret < 0) { |
| 3234 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3235 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3236 | RBD_RETRY_DELAY); |
| 3237 | } else { |
| 3238 | /* |
| 3239 | * lock owner acked, but resend if we don't see them |
| 3240 | * release the lock |
| 3241 | */ |
| 3242 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3243 | rbd_dev); |
| 3244 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3245 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | /* |
| 3250 | * lock_rwsem must be held for write |
| 3251 | */ |
| 3252 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3253 | { |
| 3254 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3255 | rbd_dev->lock_state); |
| 3256 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3257 | return false; |
| 3258 | |
| 3259 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3260 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3261 | /* |
| 3262 | * Ensure that all in-flight IO is flushed. |
| 3263 | * |
| 3264 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3265 | * may be shared with other devices. |
| 3266 | */ |
| 3267 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3268 | up_read(&rbd_dev->lock_rwsem); |
| 3269 | |
| 3270 | down_write(&rbd_dev->lock_rwsem); |
| 3271 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3272 | rbd_dev->lock_state); |
| 3273 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3274 | return false; |
| 3275 | |
Ilya Dryomov | bbead74 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3276 | rbd_unlock(rbd_dev); |
| 3277 | /* |
| 3278 | * Give others a chance to grab the lock - we would re-acquire |
| 3279 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3280 | * otherwise. We need to ack our own notifications, so this |
| 3281 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3282 | * after wake_requests() in rbd_handle_released_lock(). |
| 3283 | */ |
| 3284 | cancel_delayed_work(&rbd_dev->lock_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3285 | return true; |
| 3286 | } |
| 3287 | |
| 3288 | static void rbd_release_lock_work(struct work_struct *work) |
| 3289 | { |
| 3290 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3291 | unlock_work); |
| 3292 | |
| 3293 | down_write(&rbd_dev->lock_rwsem); |
| 3294 | rbd_release_lock(rbd_dev); |
| 3295 | up_write(&rbd_dev->lock_rwsem); |
| 3296 | } |
| 3297 | |
| 3298 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3299 | void **p) |
| 3300 | { |
| 3301 | struct rbd_client_id cid = { 0 }; |
| 3302 | |
| 3303 | if (struct_v >= 2) { |
| 3304 | cid.gid = ceph_decode_64(p); |
| 3305 | cid.handle = ceph_decode_64(p); |
| 3306 | } |
| 3307 | |
| 3308 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3309 | cid.handle); |
| 3310 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3311 | down_write(&rbd_dev->lock_rwsem); |
| 3312 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3313 | /* |
| 3314 | * we already know that the remote client is |
| 3315 | * the owner |
| 3316 | */ |
| 3317 | up_write(&rbd_dev->lock_rwsem); |
| 3318 | return; |
| 3319 | } |
| 3320 | |
| 3321 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3322 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3323 | } else { |
| 3324 | down_read(&rbd_dev->lock_rwsem); |
| 3325 | } |
| 3326 | |
| 3327 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3328 | wake_requests(rbd_dev, false); |
| 3329 | up_read(&rbd_dev->lock_rwsem); |
| 3330 | } |
| 3331 | |
| 3332 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3333 | void **p) |
| 3334 | { |
| 3335 | struct rbd_client_id cid = { 0 }; |
| 3336 | |
| 3337 | if (struct_v >= 2) { |
| 3338 | cid.gid = ceph_decode_64(p); |
| 3339 | cid.handle = ceph_decode_64(p); |
| 3340 | } |
| 3341 | |
| 3342 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3343 | cid.handle); |
| 3344 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3345 | down_write(&rbd_dev->lock_rwsem); |
| 3346 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3347 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3348 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3349 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3350 | up_write(&rbd_dev->lock_rwsem); |
| 3351 | return; |
| 3352 | } |
| 3353 | |
| 3354 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3355 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3356 | } else { |
| 3357 | down_read(&rbd_dev->lock_rwsem); |
| 3358 | } |
| 3359 | |
| 3360 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3361 | wake_requests(rbd_dev, false); |
| 3362 | up_read(&rbd_dev->lock_rwsem); |
| 3363 | } |
| 3364 | |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3365 | /* |
| 3366 | * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no |
| 3367 | * ResponseMessage is needed. |
| 3368 | */ |
| 3369 | static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3370 | void **p) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3371 | { |
| 3372 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3373 | struct rbd_client_id cid = { 0 }; |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3374 | int result = 1; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3375 | |
| 3376 | if (struct_v >= 2) { |
| 3377 | cid.gid = ceph_decode_64(p); |
| 3378 | cid.handle = ceph_decode_64(p); |
| 3379 | } |
| 3380 | |
| 3381 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3382 | cid.handle); |
| 3383 | if (rbd_cid_equal(&cid, &my_cid)) |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3384 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3385 | |
| 3386 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3387 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3388 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED && |
| 3389 | rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) |
| 3390 | goto out_unlock; |
| 3391 | |
| 3392 | /* |
| 3393 | * encode ResponseMessage(0) so the peer can detect |
| 3394 | * a missing owner |
| 3395 | */ |
| 3396 | result = 0; |
| 3397 | |
| 3398 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3399 | if (!rbd_dev->opts->exclusive) { |
| 3400 | dout("%s rbd_dev %p queueing unlock_work\n", |
| 3401 | __func__, rbd_dev); |
| 3402 | queue_work(rbd_dev->task_wq, |
| 3403 | &rbd_dev->unlock_work); |
| 3404 | } else { |
| 3405 | /* refuse to release the lock */ |
| 3406 | result = -EROFS; |
| 3407 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3408 | } |
| 3409 | } |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3410 | |
| 3411 | out_unlock: |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3412 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3413 | return result; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3417 | u64 notify_id, u64 cookie, s32 *result) |
| 3418 | { |
| 3419 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Kyle Spiers | 08a7910 | 2018-03-17 09:44:01 -0700 | [diff] [blame] | 3420 | char buf[4 + CEPH_ENCODING_START_BLK_LEN]; |
| 3421 | int buf_size = sizeof(buf); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3422 | int ret; |
| 3423 | |
| 3424 | if (result) { |
| 3425 | void *p = buf; |
| 3426 | |
| 3427 | /* encode ResponseMessage */ |
| 3428 | ceph_start_encoding(&p, 1, 1, |
| 3429 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3430 | ceph_encode_32(&p, *result); |
| 3431 | } else { |
| 3432 | buf_size = 0; |
| 3433 | } |
| 3434 | |
| 3435 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3436 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3437 | buf, buf_size); |
| 3438 | if (ret) |
| 3439 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3440 | } |
| 3441 | |
| 3442 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3443 | u64 cookie) |
| 3444 | { |
| 3445 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3446 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3447 | } |
| 3448 | |
| 3449 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3450 | u64 notify_id, u64 cookie, s32 result) |
| 3451 | { |
| 3452 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3453 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3454 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3455 | |
| 3456 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3457 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3458 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3459 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3460 | void *p = data; |
| 3461 | void *const end = p + data_len; |
Ilya Dryomov | d4c2269 | 2016-09-06 11:15:48 +0200 | [diff] [blame] | 3462 | u8 struct_v = 0; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3463 | u32 len; |
| 3464 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3465 | int ret; |
| 3466 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3467 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3468 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3469 | if (data_len) { |
| 3470 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3471 | &struct_v, &len); |
| 3472 | if (ret) { |
| 3473 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3474 | ret); |
| 3475 | return; |
| 3476 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3477 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3478 | notify_op = ceph_decode_32(&p); |
| 3479 | } else { |
| 3480 | /* legacy notification for header updates */ |
| 3481 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3482 | len = 0; |
| 3483 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3484 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3485 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3486 | switch (notify_op) { |
| 3487 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3488 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3489 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3490 | break; |
| 3491 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3492 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3493 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3494 | break; |
| 3495 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3496 | ret = rbd_handle_request_lock(rbd_dev, struct_v, &p); |
| 3497 | if (ret <= 0) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3498 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
Ilya Dryomov | 3b77faa | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3499 | cookie, ret); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3500 | else |
| 3501 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3502 | break; |
| 3503 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3504 | ret = rbd_dev_refresh(rbd_dev); |
| 3505 | if (ret) |
| 3506 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3507 | |
| 3508 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3509 | break; |
| 3510 | default: |
| 3511 | if (rbd_is_lock_owner(rbd_dev)) |
| 3512 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3513 | cookie, -EOPNOTSUPP); |
| 3514 | else |
| 3515 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3516 | break; |
| 3517 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3518 | } |
| 3519 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3520 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3521 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3522 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3523 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3524 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3525 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3526 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3527 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3528 | down_write(&rbd_dev->lock_rwsem); |
| 3529 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3530 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3531 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3532 | mutex_lock(&rbd_dev->watch_mutex); |
| 3533 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3534 | __rbd_unregister_watch(rbd_dev); |
| 3535 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3536 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3537 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3538 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3539 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3543 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3544 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3545 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3546 | { |
| 3547 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3548 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3549 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3550 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3551 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3552 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3553 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3554 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3555 | rbd_watch_errcb, rbd_dev); |
| 3556 | if (IS_ERR(handle)) |
| 3557 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3558 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3559 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3560 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3561 | } |
| 3562 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3563 | /* |
| 3564 | * watch_mutex must be locked |
| 3565 | */ |
| 3566 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3567 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3568 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3569 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3570 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3571 | rbd_assert(rbd_dev->watch_handle); |
| 3572 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3573 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3574 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3575 | if (ret) |
| 3576 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3577 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3578 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3579 | } |
| 3580 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3581 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3582 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3583 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3584 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3585 | mutex_lock(&rbd_dev->watch_mutex); |
| 3586 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3587 | ret = __rbd_register_watch(rbd_dev); |
| 3588 | if (ret) |
| 3589 | goto out; |
| 3590 | |
| 3591 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3592 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3593 | |
| 3594 | out: |
| 3595 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3596 | return ret; |
| 3597 | } |
| 3598 | |
| 3599 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3600 | { |
| 3601 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3602 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3603 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3604 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3605 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3606 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3610 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3611 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3612 | cancel_tasks_sync(rbd_dev); |
| 3613 | |
| 3614 | mutex_lock(&rbd_dev->watch_mutex); |
| 3615 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3616 | __rbd_unregister_watch(rbd_dev); |
| 3617 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3618 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3619 | |
Dongsheng Yang | 23edca8 | 2018-06-04 06:24:37 -0400 | [diff] [blame] | 3620 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3621 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3622 | } |
| 3623 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3624 | /* |
| 3625 | * lock_rwsem must be held for write |
| 3626 | */ |
| 3627 | static void rbd_reacquire_lock(struct rbd_device *rbd_dev) |
| 3628 | { |
| 3629 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3630 | char cookie[32]; |
| 3631 | int ret; |
| 3632 | |
| 3633 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 3634 | |
| 3635 | format_lock_cookie(rbd_dev, cookie); |
| 3636 | ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid, |
| 3637 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3638 | CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie, |
| 3639 | RBD_LOCK_TAG, cookie); |
| 3640 | if (ret) { |
| 3641 | if (ret != -EOPNOTSUPP) |
| 3642 | rbd_warn(rbd_dev, "failed to update lock cookie: %d", |
| 3643 | ret); |
| 3644 | |
| 3645 | /* |
| 3646 | * Lock cookie cannot be updated on older OSDs, so do |
| 3647 | * a manual release and queue an acquire. |
| 3648 | */ |
| 3649 | if (rbd_release_lock(rbd_dev)) |
| 3650 | queue_delayed_work(rbd_dev->task_wq, |
| 3651 | &rbd_dev->lock_dwork, 0); |
| 3652 | } else { |
Florian Margaine | edd8ca8 | 2017-12-13 16:43:59 +0100 | [diff] [blame] | 3653 | __rbd_lock(rbd_dev, cookie); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3654 | } |
| 3655 | } |
| 3656 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3657 | static void rbd_reregister_watch(struct work_struct *work) |
| 3658 | { |
| 3659 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3660 | struct rbd_device, watch_dwork); |
| 3661 | int ret; |
| 3662 | |
| 3663 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3664 | |
| 3665 | mutex_lock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3666 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) { |
| 3667 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3668 | return; |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3669 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3670 | |
| 3671 | ret = __rbd_register_watch(rbd_dev); |
| 3672 | if (ret) { |
| 3673 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
Ilya Dryomov | 4d73644 | 2016-09-29 14:23:12 +0200 | [diff] [blame] | 3674 | if (ret == -EBLACKLISTED || ret == -ENOENT) { |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3675 | set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3676 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3677 | } else { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3678 | queue_delayed_work(rbd_dev->task_wq, |
| 3679 | &rbd_dev->watch_dwork, |
| 3680 | RBD_RETRY_DELAY); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3681 | } |
| 3682 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3683 | return; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3687 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3688 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3689 | |
Ilya Dryomov | 14bb211 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3690 | down_write(&rbd_dev->lock_rwsem); |
| 3691 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3692 | rbd_reacquire_lock(rbd_dev); |
| 3693 | up_write(&rbd_dev->lock_rwsem); |
| 3694 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3695 | ret = rbd_dev_refresh(rbd_dev); |
| 3696 | if (ret) |
Colin Ian King | f6870cc | 2018-03-19 13:33:10 +0000 | [diff] [blame] | 3697 | rbd_warn(rbd_dev, "reregistration refresh failed: %d", ret); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3698 | } |
| 3699 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3700 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3701 | * Synchronous osd object method call. Returns the number of bytes |
| 3702 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3703 | */ |
| 3704 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3705 | struct ceph_object_id *oid, |
| 3706 | struct ceph_object_locator *oloc, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3707 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3708 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3709 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3710 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3711 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3712 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3713 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3714 | struct page *req_page = NULL; |
| 3715 | struct page *reply_page; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3716 | int ret; |
| 3717 | |
| 3718 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3719 | * Method calls are ultimately read operations. The result |
| 3720 | * should placed into the inbound buffer provided. They |
| 3721 | * also supply outbound data--parameters for the object |
| 3722 | * method. Currently if this is present it will be a |
| 3723 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3724 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3725 | if (outbound) { |
| 3726 | if (outbound_size > PAGE_SIZE) |
| 3727 | return -E2BIG; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3728 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3729 | req_page = alloc_page(GFP_KERNEL); |
| 3730 | if (!req_page) |
| 3731 | return -ENOMEM; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3732 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3733 | memcpy(page_address(req_page), outbound, outbound_size); |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 3734 | } |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 3735 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3736 | reply_page = alloc_page(GFP_KERNEL); |
| 3737 | if (!reply_page) { |
| 3738 | if (req_page) |
| 3739 | __free_page(req_page); |
| 3740 | return -ENOMEM; |
| 3741 | } |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3742 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3743 | ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name, |
| 3744 | CEPH_OSD_FLAG_READ, req_page, outbound_size, |
| 3745 | reply_page, &inbound_size); |
| 3746 | if (!ret) { |
| 3747 | memcpy(inbound, page_address(reply_page), inbound_size); |
| 3748 | ret = inbound_size; |
| 3749 | } |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3750 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3751 | if (req_page) |
| 3752 | __free_page(req_page); |
| 3753 | __free_page(reply_page); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3754 | return ret; |
| 3755 | } |
| 3756 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3757 | /* |
| 3758 | * lock_rwsem must be held for read |
| 3759 | */ |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3760 | 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] | 3761 | { |
| 3762 | DEFINE_WAIT(wait); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3763 | unsigned long timeout; |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3764 | int ret = 0; |
| 3765 | |
| 3766 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) |
| 3767 | return -EBLACKLISTED; |
| 3768 | |
| 3769 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3770 | return 0; |
| 3771 | |
| 3772 | if (!may_acquire) { |
| 3773 | rbd_warn(rbd_dev, "exclusive lock required"); |
| 3774 | return -EROFS; |
| 3775 | } |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3776 | |
| 3777 | do { |
| 3778 | /* |
| 3779 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 3780 | * and cancel_delayed_work() in wake_requests(). |
| 3781 | */ |
| 3782 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 3783 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 3784 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 3785 | TASK_UNINTERRUPTIBLE); |
| 3786 | up_read(&rbd_dev->lock_rwsem); |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3787 | timeout = schedule_timeout(ceph_timeout_jiffies( |
| 3788 | rbd_dev->opts->lock_timeout)); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3789 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3790 | if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) { |
| 3791 | ret = -EBLACKLISTED; |
| 3792 | break; |
| 3793 | } |
Dongsheng Yang | 34f55d0 | 2018-03-26 10:22:55 -0400 | [diff] [blame] | 3794 | if (!timeout) { |
| 3795 | rbd_warn(rbd_dev, "timed out waiting for lock"); |
| 3796 | ret = -ETIMEDOUT; |
| 3797 | break; |
| 3798 | } |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3799 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3800 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3801 | finish_wait(&rbd_dev->lock_waitq, &wait); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3802 | return ret; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3805 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3806 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3807 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 3808 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3809 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3810 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3811 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 3812 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3813 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3814 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 3815 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3816 | int result; |
| 3817 | |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3818 | switch (req_op(rq)) { |
| 3819 | case REQ_OP_DISCARD: |
| 3820 | op_type = OBJ_OP_DISCARD; |
| 3821 | break; |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 3822 | case REQ_OP_WRITE_ZEROES: |
| 3823 | op_type = OBJ_OP_ZEROOUT; |
| 3824 | break; |
Christoph Hellwig | aebf526 | 2017-01-31 16:57:31 +0100 | [diff] [blame] | 3825 | case REQ_OP_WRITE: |
| 3826 | op_type = OBJ_OP_WRITE; |
| 3827 | break; |
| 3828 | case REQ_OP_READ: |
| 3829 | op_type = OBJ_OP_READ; |
| 3830 | break; |
| 3831 | default: |
| 3832 | dout("%s: non-fs request type %d\n", __func__, req_op(rq)); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3833 | result = -EIO; |
| 3834 | goto err; |
| 3835 | } |
| 3836 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3837 | /* Ignore/skip any zero-length requests */ |
| 3838 | |
| 3839 | if (!length) { |
| 3840 | dout("%s: zero-length request\n", __func__); |
| 3841 | result = 0; |
| 3842 | goto err_rq; |
| 3843 | } |
| 3844 | |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 3845 | rbd_assert(op_type == OBJ_OP_READ || |
| 3846 | rbd_dev->spec->snap_id == CEPH_NOSNAP); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3847 | |
| 3848 | /* |
| 3849 | * Quit early if the mapped snapshot no longer exists. It's |
| 3850 | * still possible the snapshot will have disappeared by the |
| 3851 | * time our request arrives at the osd, but there's no sense in |
| 3852 | * sending it if we already know. |
| 3853 | */ |
| 3854 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 3855 | dout("request for non-existent snapshot"); |
| 3856 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 3857 | result = -ENXIO; |
| 3858 | goto err_rq; |
| 3859 | } |
| 3860 | |
| 3861 | if (offset && length > U64_MAX - offset + 1) { |
| 3862 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 3863 | length); |
| 3864 | result = -EINVAL; |
| 3865 | goto err_rq; /* Shouldn't happen */ |
| 3866 | } |
| 3867 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3868 | blk_mq_start_request(rq); |
| 3869 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3870 | down_read(&rbd_dev->header_rwsem); |
| 3871 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3872 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3873 | snapc = rbd_dev->header.snapc; |
| 3874 | ceph_get_snap_context(snapc); |
| 3875 | } |
| 3876 | up_read(&rbd_dev->header_rwsem); |
| 3877 | |
| 3878 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3879 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 3880 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3881 | result = -EIO; |
| 3882 | goto err_rq; |
| 3883 | } |
| 3884 | |
Ilya Dryomov | f9bebd5 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 3885 | must_be_locked = |
| 3886 | (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 3887 | (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3888 | if (must_be_locked) { |
| 3889 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 3890 | result = rbd_wait_state_locked(rbd_dev, |
| 3891 | !rbd_dev->opts->exclusive); |
| 3892 | if (result) |
Ilya Dryomov | 87c0fde | 2016-09-29 13:41:05 +0200 | [diff] [blame] | 3893 | goto err_unlock; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Ilya Dryomov | dfd9875 | 2018-02-06 19:26:35 +0100 | [diff] [blame] | 3896 | img_request = rbd_img_request_create(rbd_dev, op_type, snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3897 | if (!img_request) { |
| 3898 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3899 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3900 | } |
| 3901 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 3902 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3903 | |
Ilya Dryomov | 6484cbe | 2019-01-29 12:46:25 +0100 | [diff] [blame] | 3904 | if (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_ZEROOUT) |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3905 | result = rbd_img_fill_nodata(img_request, offset, length); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 3906 | else |
Ilya Dryomov | 5a23781 | 2018-02-06 19:26:34 +0100 | [diff] [blame] | 3907 | result = rbd_img_fill_from_bio(img_request, offset, length, |
| 3908 | rq->bio); |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 3909 | if (result || !img_request->pending_count) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3910 | goto err_img_request; |
| 3911 | |
Ilya Dryomov | efbd1a1 | 2018-01-30 17:52:11 +0100 | [diff] [blame] | 3912 | rbd_img_request_submit(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3913 | if (must_be_locked) |
| 3914 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3915 | return; |
| 3916 | |
| 3917 | err_img_request: |
| 3918 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3919 | err_unlock: |
| 3920 | if (must_be_locked) |
| 3921 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3922 | err_rq: |
| 3923 | if (result) |
| 3924 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 3925 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 3926 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3927 | err: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 3928 | blk_mq_end_request(rq, errno_to_blk_status(result)); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3929 | } |
| 3930 | |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3931 | 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] | 3932 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3933 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3934 | struct request *rq = bd->rq; |
| 3935 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 3936 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 3937 | queue_work(rbd_wq, work); |
Christoph Hellwig | fc17b65 | 2017-06-03 09:38:05 +0200 | [diff] [blame] | 3938 | return BLK_STS_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3939 | } |
| 3940 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3941 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 3942 | { |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 3943 | blk_cleanup_queue(rbd_dev->disk->queue); |
| 3944 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
| 3945 | put_disk(rbd_dev->disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 3946 | rbd_dev->disk = NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3947 | } |
| 3948 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3949 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3950 | struct ceph_object_id *oid, |
| 3951 | struct ceph_object_locator *oloc, |
| 3952 | void *buf, int buf_len) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3953 | |
| 3954 | { |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3955 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3956 | struct ceph_osd_request *req; |
| 3957 | struct page **pages; |
| 3958 | int num_pages = calc_pages_for(0, buf_len); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3959 | int ret; |
| 3960 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3961 | req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL); |
| 3962 | if (!req) |
| 3963 | return -ENOMEM; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3964 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3965 | ceph_oid_copy(&req->r_base_oid, oid); |
| 3966 | ceph_oloc_copy(&req->r_base_oloc, oloc); |
| 3967 | req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3968 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3969 | pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL); |
| 3970 | if (IS_ERR(pages)) { |
| 3971 | ret = PTR_ERR(pages); |
| 3972 | goto out_req; |
| 3973 | } |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 3974 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3975 | osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0); |
| 3976 | osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false, |
| 3977 | true); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3978 | |
Ilya Dryomov | 26f887e | 2018-10-15 16:11:37 +0200 | [diff] [blame] | 3979 | ret = ceph_osdc_alloc_messages(req, GFP_KERNEL); |
| 3980 | if (ret) |
| 3981 | goto out_req; |
| 3982 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 3983 | ceph_osdc_start_request(osdc, req, false); |
| 3984 | ret = ceph_osdc_wait_request(osdc, req); |
| 3985 | if (ret >= 0) |
| 3986 | ceph_copy_from_page_vector(pages, buf, 0, ret); |
| 3987 | |
| 3988 | out_req: |
| 3989 | ceph_osdc_put_request(req); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 3990 | return ret; |
| 3991 | } |
| 3992 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 3993 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 3994 | * Read the complete header for the given rbd device. On successful |
| 3995 | * return, the rbd_dev->header field will contain up-to-date |
| 3996 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3997 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 3998 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 3999 | { |
| 4000 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4001 | u32 snap_count = 0; |
| 4002 | u64 names_size = 0; |
| 4003 | u32 want_count; |
| 4004 | int ret; |
| 4005 | |
| 4006 | /* |
| 4007 | * The complete header will include an array of its 64-bit |
| 4008 | * snapshot ids, followed by the names of those snapshots as |
| 4009 | * a contiguous block of NUL-terminated strings. Note that |
| 4010 | * the number of snapshots could change by the time we read |
| 4011 | * it in, in which case we re-read it. |
| 4012 | */ |
| 4013 | do { |
| 4014 | size_t size; |
| 4015 | |
| 4016 | kfree(ondisk); |
| 4017 | |
| 4018 | size = sizeof (*ondisk); |
| 4019 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4020 | size += names_size; |
| 4021 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4022 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4023 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4024 | |
Ilya Dryomov | fe5478e | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4025 | ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid, |
| 4026 | &rbd_dev->header_oloc, ondisk, size); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4027 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4028 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4029 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4030 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4031 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4032 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4033 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4034 | } |
| 4035 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4036 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4037 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4038 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4039 | } |
| 4040 | |
| 4041 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4042 | want_count = snap_count; |
| 4043 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4044 | } while (snap_count != want_count); |
| 4045 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4046 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4047 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4048 | kfree(ondisk); |
| 4049 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4050 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4053 | /* |
| 4054 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4055 | * has disappeared from the (just updated) snapshot context. |
| 4056 | */ |
| 4057 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4058 | { |
| 4059 | u64 snap_id; |
| 4060 | |
| 4061 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4062 | return; |
| 4063 | |
| 4064 | snap_id = rbd_dev->spec->snap_id; |
| 4065 | if (snap_id == CEPH_NOSNAP) |
| 4066 | return; |
| 4067 | |
| 4068 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4069 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4070 | } |
| 4071 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4072 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4073 | { |
| 4074 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4075 | |
| 4076 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4077 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4078 | * try to update its size. If REMOVING is set, updating size |
| 4079 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4080 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4081 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4082 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4083 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4084 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4085 | set_capacity(rbd_dev->disk, size); |
| 4086 | revalidate_disk(rbd_dev->disk); |
| 4087 | } |
| 4088 | } |
| 4089 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4090 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4091 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4092 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4093 | int ret; |
| 4094 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4095 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4096 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4097 | |
| 4098 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4099 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4100 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4101 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4102 | /* |
| 4103 | * If there is a parent, see if it has disappeared due to the |
| 4104 | * mapped image getting flattened. |
| 4105 | */ |
| 4106 | if (rbd_dev->parent) { |
| 4107 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4108 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4109 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4110 | } |
| 4111 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4112 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4113 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4114 | } else { |
| 4115 | /* validate mapped snapshot's EXISTS flag */ |
| 4116 | rbd_exists_validate(rbd_dev); |
| 4117 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4118 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4119 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4120 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4121 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4122 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4123 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4124 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4125 | } |
| 4126 | |
Christoph Hellwig | d6296d39 | 2017-05-01 10:19:08 -0600 | [diff] [blame] | 4127 | static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq, |
| 4128 | unsigned int hctx_idx, unsigned int numa_node) |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4129 | { |
| 4130 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4131 | |
| 4132 | INIT_WORK(work, rbd_queue_workfn); |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 4136 | static const struct blk_mq_ops rbd_mq_ops = { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4137 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4138 | .init_request = rbd_init_request, |
| 4139 | }; |
| 4140 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4141 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4142 | { |
| 4143 | struct gendisk *disk; |
| 4144 | struct request_queue *q; |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4145 | unsigned int objset_bytes = |
| 4146 | rbd_dev->layout.object_size * rbd_dev->layout.stripe_count; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4147 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4148 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4149 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4150 | disk = alloc_disk(single_major ? |
| 4151 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4152 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4153 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4154 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4155 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4156 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4157 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4158 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4159 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4160 | if (single_major) |
| 4161 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4162 | disk->fops = &rbd_bd_ops; |
| 4163 | disk->private_data = rbd_dev; |
| 4164 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4165 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4166 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4167 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4168 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ming Lei | 56d18f6 | 2019-02-15 19:13:24 +0800 | [diff] [blame] | 4169 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4170 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4171 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4172 | |
| 4173 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4174 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4175 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4176 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4177 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4178 | if (IS_ERR(q)) { |
| 4179 | err = PTR_ERR(q); |
| 4180 | goto out_tag_set; |
| 4181 | } |
| 4182 | |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 4183 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4184 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4185 | |
Ilya Dryomov | 420efbd | 2018-04-16 09:32:18 +0200 | [diff] [blame] | 4186 | blk_queue_max_hw_sectors(q, objset_bytes >> SECTOR_SHIFT); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4187 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | 21acdf4 | 2017-12-21 15:35:11 +0100 | [diff] [blame] | 4188 | blk_queue_max_segments(q, USHRT_MAX); |
Ilya Dryomov | 24f1df6 | 2018-01-12 17:22:10 +0100 | [diff] [blame] | 4189 | blk_queue_max_segment_size(q, UINT_MAX); |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 4190 | blk_queue_io_min(q, rbd_dev->opts->alloc_size); |
| 4191 | blk_queue_io_opt(q, rbd_dev->opts->alloc_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4192 | |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4193 | if (rbd_dev->opts->trim) { |
| 4194 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); |
Ilya Dryomov | 16d80c5 | 2019-03-15 14:50:04 +0100 | [diff] [blame] | 4195 | q->limits.discard_granularity = rbd_dev->opts->alloc_size; |
Ilya Dryomov | d936054 | 2018-03-23 06:14:47 +0100 | [diff] [blame] | 4196 | blk_queue_max_discard_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4197 | blk_queue_max_write_zeroes_sectors(q, objset_bytes >> SECTOR_SHIFT); |
| 4198 | } |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4199 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4200 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4201 | q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES; |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4202 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 4203 | /* |
| 4204 | * disk_release() expects a queue ref from add_disk() and will |
| 4205 | * put it. Hold an extra ref until add_disk() is called. |
| 4206 | */ |
| 4207 | WARN_ON(!blk_get_queue(q)); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4208 | disk->queue = q; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4209 | q->queuedata = rbd_dev; |
| 4210 | |
| 4211 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4212 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4213 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4214 | out_tag_set: |
| 4215 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4216 | out_disk: |
| 4217 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4218 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4219 | } |
| 4220 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4221 | /* |
| 4222 | sysfs |
| 4223 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4224 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4225 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4226 | { |
| 4227 | return container_of(dev, struct rbd_device, dev); |
| 4228 | } |
| 4229 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4230 | static ssize_t rbd_size_show(struct device *dev, |
| 4231 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4232 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4233 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4234 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4235 | return sprintf(buf, "%llu\n", |
| 4236 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4237 | } |
| 4238 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4239 | /* |
| 4240 | * Note this shows the features for whatever's mapped, which is not |
| 4241 | * necessarily the base image. |
| 4242 | */ |
| 4243 | static ssize_t rbd_features_show(struct device *dev, |
| 4244 | struct device_attribute *attr, char *buf) |
| 4245 | { |
| 4246 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4247 | |
| 4248 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4249 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4250 | } |
| 4251 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4252 | static ssize_t rbd_major_show(struct device *dev, |
| 4253 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4254 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4255 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4256 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4257 | if (rbd_dev->major) |
| 4258 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4259 | |
| 4260 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4261 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4262 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4263 | static ssize_t rbd_minor_show(struct device *dev, |
| 4264 | struct device_attribute *attr, char *buf) |
| 4265 | { |
| 4266 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4267 | |
| 4268 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4269 | } |
| 4270 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4271 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4272 | struct device_attribute *attr, char *buf) |
| 4273 | { |
| 4274 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4275 | struct ceph_entity_addr *client_addr = |
| 4276 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4277 | |
| 4278 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4279 | le32_to_cpu(client_addr->nonce)); |
| 4280 | } |
| 4281 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4282 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4283 | struct device_attribute *attr, char *buf) |
| 4284 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4285 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4286 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4287 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4288 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4291 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4292 | struct device_attribute *attr, char *buf) |
| 4293 | { |
| 4294 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4295 | |
| 4296 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4297 | } |
| 4298 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4299 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4300 | struct device_attribute *attr, char *buf) |
| 4301 | { |
| 4302 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4303 | |
| 4304 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4305 | } |
| 4306 | |
| 4307 | static ssize_t rbd_pool_show(struct device *dev, |
| 4308 | struct device_attribute *attr, char *buf) |
| 4309 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4310 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4311 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4312 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4313 | } |
| 4314 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4315 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4316 | struct device_attribute *attr, char *buf) |
| 4317 | { |
| 4318 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4319 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4320 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4321 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4322 | } |
| 4323 | |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4324 | static ssize_t rbd_pool_ns_show(struct device *dev, |
| 4325 | struct device_attribute *attr, char *buf) |
| 4326 | { |
| 4327 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4328 | |
| 4329 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_ns ?: ""); |
| 4330 | } |
| 4331 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4332 | static ssize_t rbd_name_show(struct device *dev, |
| 4333 | struct device_attribute *attr, char *buf) |
| 4334 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4335 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4336 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4337 | if (rbd_dev->spec->image_name) |
| 4338 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4339 | |
| 4340 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4341 | } |
| 4342 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4343 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4344 | struct device_attribute *attr, char *buf) |
| 4345 | { |
| 4346 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4347 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4348 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4349 | } |
| 4350 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4351 | /* |
| 4352 | * Shows the name of the currently-mapped snapshot (or |
| 4353 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4354 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4355 | static ssize_t rbd_snap_show(struct device *dev, |
| 4356 | struct device_attribute *attr, |
| 4357 | char *buf) |
| 4358 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4359 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4360 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4361 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4362 | } |
| 4363 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4364 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4365 | struct device_attribute *attr, char *buf) |
| 4366 | { |
| 4367 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4368 | |
| 4369 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4370 | } |
| 4371 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4372 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4373 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4374 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4375 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4376 | */ |
| 4377 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4378 | struct device_attribute *attr, |
| 4379 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4380 | { |
| 4381 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4382 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4383 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4384 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4385 | return sprintf(buf, "(no parent image)\n"); |
| 4386 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4387 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4388 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4389 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4390 | count += sprintf(&buf[count], "%s" |
| 4391 | "pool_id %llu\npool_name %s\n" |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4392 | "pool_ns %s\n" |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4393 | "image_id %s\nimage_name %s\n" |
| 4394 | "snap_id %llu\nsnap_name %s\n" |
| 4395 | "overlap %llu\n", |
| 4396 | !count ? "" : "\n", /* first? */ |
| 4397 | spec->pool_id, spec->pool_name, |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4398 | spec->pool_ns ?: "", |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4399 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4400 | spec->snap_id, spec->snap_name, |
| 4401 | rbd_dev->parent_overlap); |
| 4402 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4403 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4404 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4405 | } |
| 4406 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4407 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4408 | struct device_attribute *attr, |
| 4409 | const char *buf, |
| 4410 | size_t size) |
| 4411 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4412 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4413 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4414 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4415 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4416 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4417 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4418 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4419 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4420 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4421 | |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4422 | static DEVICE_ATTR(size, 0444, rbd_size_show, NULL); |
| 4423 | static DEVICE_ATTR(features, 0444, rbd_features_show, NULL); |
| 4424 | static DEVICE_ATTR(major, 0444, rbd_major_show, NULL); |
| 4425 | static DEVICE_ATTR(minor, 0444, rbd_minor_show, NULL); |
| 4426 | static DEVICE_ATTR(client_addr, 0444, rbd_client_addr_show, NULL); |
| 4427 | static DEVICE_ATTR(client_id, 0444, rbd_client_id_show, NULL); |
| 4428 | static DEVICE_ATTR(cluster_fsid, 0444, rbd_cluster_fsid_show, NULL); |
| 4429 | static DEVICE_ATTR(config_info, 0400, rbd_config_info_show, NULL); |
| 4430 | static DEVICE_ATTR(pool, 0444, rbd_pool_show, NULL); |
| 4431 | static DEVICE_ATTR(pool_id, 0444, rbd_pool_id_show, NULL); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4432 | static DEVICE_ATTR(pool_ns, 0444, rbd_pool_ns_show, NULL); |
Joe Perches | 5657a81 | 2018-05-24 13:38:59 -0600 | [diff] [blame] | 4433 | static DEVICE_ATTR(name, 0444, rbd_name_show, NULL); |
| 4434 | static DEVICE_ATTR(image_id, 0444, rbd_image_id_show, NULL); |
| 4435 | static DEVICE_ATTR(refresh, 0200, NULL, rbd_image_refresh); |
| 4436 | static DEVICE_ATTR(current_snap, 0444, rbd_snap_show, NULL); |
| 4437 | static DEVICE_ATTR(snap_id, 0444, rbd_snap_id_show, NULL); |
| 4438 | static DEVICE_ATTR(parent, 0444, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4439 | |
| 4440 | static struct attribute *rbd_attrs[] = { |
| 4441 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4442 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4443 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4444 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4445 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4446 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4447 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4448 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4449 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4450 | &dev_attr_pool_id.attr, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4451 | &dev_attr_pool_ns.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4452 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4453 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4454 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4455 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4456 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4457 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4458 | NULL |
| 4459 | }; |
| 4460 | |
| 4461 | static struct attribute_group rbd_attr_group = { |
| 4462 | .attrs = rbd_attrs, |
| 4463 | }; |
| 4464 | |
| 4465 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4466 | &rbd_attr_group, |
| 4467 | NULL |
| 4468 | }; |
| 4469 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4470 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4471 | |
Bhumika Goyal | b9942bc | 2017-02-11 12:14:38 +0530 | [diff] [blame] | 4472 | static const struct device_type rbd_device_type = { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4473 | .name = "rbd", |
| 4474 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4475 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4476 | }; |
| 4477 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4478 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4479 | { |
| 4480 | kref_get(&spec->kref); |
| 4481 | |
| 4482 | return spec; |
| 4483 | } |
| 4484 | |
| 4485 | static void rbd_spec_free(struct kref *kref); |
| 4486 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4487 | { |
| 4488 | if (spec) |
| 4489 | kref_put(&spec->kref, rbd_spec_free); |
| 4490 | } |
| 4491 | |
| 4492 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4493 | { |
| 4494 | struct rbd_spec *spec; |
| 4495 | |
| 4496 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4497 | if (!spec) |
| 4498 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4499 | |
| 4500 | spec->pool_id = CEPH_NOPOOL; |
| 4501 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4502 | kref_init(&spec->kref); |
| 4503 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4504 | return spec; |
| 4505 | } |
| 4506 | |
| 4507 | static void rbd_spec_free(struct kref *kref) |
| 4508 | { |
| 4509 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4510 | |
| 4511 | kfree(spec->pool_name); |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4512 | kfree(spec->pool_ns); |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4513 | kfree(spec->image_id); |
| 4514 | kfree(spec->image_name); |
| 4515 | kfree(spec->snap_name); |
| 4516 | kfree(spec); |
| 4517 | } |
| 4518 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4519 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4520 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4521 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4522 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4523 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4524 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4525 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4526 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4527 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4528 | rbd_put_client(rbd_dev->rbd_client); |
| 4529 | rbd_spec_put(rbd_dev->spec); |
| 4530 | kfree(rbd_dev->opts); |
| 4531 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | static void rbd_dev_release(struct device *dev) |
| 4535 | { |
| 4536 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4537 | bool need_put = !!rbd_dev->opts; |
| 4538 | |
| 4539 | if (need_put) { |
| 4540 | destroy_workqueue(rbd_dev->task_wq); |
| 4541 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4542 | } |
| 4543 | |
| 4544 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4545 | |
| 4546 | /* |
| 4547 | * This is racy, but way better than putting module outside of |
| 4548 | * the release callback. The race window is pretty small, so |
| 4549 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4550 | */ |
| 4551 | if (need_put) |
| 4552 | module_put(THIS_MODULE); |
| 4553 | } |
| 4554 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4555 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4556 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4557 | { |
| 4558 | struct rbd_device *rbd_dev; |
| 4559 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4560 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4561 | if (!rbd_dev) |
| 4562 | return NULL; |
| 4563 | |
| 4564 | spin_lock_init(&rbd_dev->lock); |
| 4565 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4566 | init_rwsem(&rbd_dev->header_rwsem); |
| 4567 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 4568 | rbd_dev->header.data_pool_id = CEPH_NOPOOL; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4569 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 431a02c | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4570 | rbd_dev->header_oloc.pool = spec->pool_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4571 | if (spec->pool_ns) { |
| 4572 | WARN_ON(!*spec->pool_ns); |
| 4573 | rbd_dev->header_oloc.pool_ns = |
| 4574 | ceph_find_or_create_string(spec->pool_ns, |
| 4575 | strlen(spec->pool_ns)); |
| 4576 | } |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4577 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4578 | mutex_init(&rbd_dev->watch_mutex); |
| 4579 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4580 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4581 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4582 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4583 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4584 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4585 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4586 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4587 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4588 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4589 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4590 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4591 | rbd_dev->dev.type = &rbd_device_type; |
| 4592 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4593 | device_initialize(&rbd_dev->dev); |
| 4594 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4595 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4596 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4597 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4598 | return rbd_dev; |
| 4599 | } |
| 4600 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4601 | /* |
| 4602 | * Create a mapping rbd_dev. |
| 4603 | */ |
| 4604 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4605 | struct rbd_spec *spec, |
| 4606 | struct rbd_options *opts) |
| 4607 | { |
| 4608 | struct rbd_device *rbd_dev; |
| 4609 | |
| 4610 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4611 | if (!rbd_dev) |
| 4612 | return NULL; |
| 4613 | |
| 4614 | rbd_dev->opts = opts; |
| 4615 | |
| 4616 | /* get an id and fill in device name */ |
| 4617 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4618 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4619 | GFP_KERNEL); |
| 4620 | if (rbd_dev->dev_id < 0) |
| 4621 | goto fail_rbd_dev; |
| 4622 | |
| 4623 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4624 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4625 | rbd_dev->name); |
| 4626 | if (!rbd_dev->task_wq) |
| 4627 | goto fail_dev_id; |
| 4628 | |
| 4629 | /* we have a ref from do_rbd_add() */ |
| 4630 | __module_get(THIS_MODULE); |
| 4631 | |
| 4632 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4633 | return rbd_dev; |
| 4634 | |
| 4635 | fail_dev_id: |
| 4636 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4637 | fail_rbd_dev: |
| 4638 | rbd_dev_free(rbd_dev); |
| 4639 | return NULL; |
| 4640 | } |
| 4641 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4642 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4643 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4644 | if (rbd_dev) |
| 4645 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4646 | } |
| 4647 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4648 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4649 | * Get the size and object order for an image snapshot, or if |
| 4650 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4651 | * image. |
| 4652 | */ |
| 4653 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4654 | u8 *order, u64 *snap_size) |
| 4655 | { |
| 4656 | __le64 snapid = cpu_to_le64(snap_id); |
| 4657 | int ret; |
| 4658 | struct { |
| 4659 | u8 order; |
| 4660 | __le64 size; |
| 4661 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4662 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4663 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4664 | &rbd_dev->header_oloc, "get_size", |
| 4665 | &snapid, sizeof(snapid), |
| 4666 | &size_buf, sizeof(size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4667 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4668 | if (ret < 0) |
| 4669 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4670 | if (ret < sizeof (size_buf)) |
| 4671 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4672 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4673 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4674 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4675 | dout(" order %u", (unsigned int)*order); |
| 4676 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4677 | *snap_size = le64_to_cpu(size_buf.size); |
| 4678 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4679 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4680 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4681 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4682 | |
| 4683 | return 0; |
| 4684 | } |
| 4685 | |
| 4686 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4687 | { |
| 4688 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4689 | &rbd_dev->header.obj_order, |
| 4690 | &rbd_dev->header.image_size); |
| 4691 | } |
| 4692 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4693 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4694 | { |
| 4695 | void *reply_buf; |
| 4696 | int ret; |
| 4697 | void *p; |
| 4698 | |
| 4699 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4700 | if (!reply_buf) |
| 4701 | return -ENOMEM; |
| 4702 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4703 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4704 | &rbd_dev->header_oloc, "get_object_prefix", |
| 4705 | NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4706 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4707 | if (ret < 0) |
| 4708 | goto out; |
| 4709 | |
| 4710 | p = reply_buf; |
| 4711 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4712 | p + ret, NULL, GFP_NOIO); |
| 4713 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4714 | |
| 4715 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 4716 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 4717 | rbd_dev->header.object_prefix = NULL; |
| 4718 | } else { |
| 4719 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 4720 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4721 | out: |
| 4722 | kfree(reply_buf); |
| 4723 | |
| 4724 | return ret; |
| 4725 | } |
| 4726 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4727 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 4728 | u64 *snap_features) |
| 4729 | { |
| 4730 | __le64 snapid = cpu_to_le64(snap_id); |
| 4731 | struct { |
| 4732 | __le64 features; |
| 4733 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4734 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4735 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4736 | int ret; |
| 4737 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 4738 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 4739 | &rbd_dev->header_oloc, "get_features", |
| 4740 | &snapid, sizeof(snapid), |
| 4741 | &features_buf, sizeof(features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4742 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4743 | if (ret < 0) |
| 4744 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4745 | if (ret < sizeof (features_buf)) |
| 4746 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4747 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4748 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 4749 | if (unsup) { |
| 4750 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 4751 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 4752 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 4753 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 4754 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4755 | *snap_features = le64_to_cpu(features_buf.features); |
| 4756 | |
| 4757 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4758 | (unsigned long long)snap_id, |
| 4759 | (unsigned long long)*snap_features, |
| 4760 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4761 | |
| 4762 | return 0; |
| 4763 | } |
| 4764 | |
| 4765 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 4766 | { |
| 4767 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 4768 | &rbd_dev->header.features); |
| 4769 | } |
| 4770 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4771 | struct parent_image_info { |
| 4772 | u64 pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4773 | const char *pool_ns; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4774 | const char *image_id; |
| 4775 | u64 snap_id; |
| 4776 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4777 | bool has_overlap; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4778 | u64 overlap; |
| 4779 | }; |
| 4780 | |
| 4781 | /* |
| 4782 | * The caller is responsible for @pii. |
| 4783 | */ |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4784 | static int decode_parent_image_spec(void **p, void *end, |
| 4785 | struct parent_image_info *pii) |
| 4786 | { |
| 4787 | u8 struct_v; |
| 4788 | u32 struct_len; |
| 4789 | int ret; |
| 4790 | |
| 4791 | ret = ceph_start_decoding(p, end, 1, "ParentImageSpec", |
| 4792 | &struct_v, &struct_len); |
| 4793 | if (ret) |
| 4794 | return ret; |
| 4795 | |
| 4796 | ceph_decode_64_safe(p, end, pii->pool_id, e_inval); |
| 4797 | pii->pool_ns = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 4798 | if (IS_ERR(pii->pool_ns)) { |
| 4799 | ret = PTR_ERR(pii->pool_ns); |
| 4800 | pii->pool_ns = NULL; |
| 4801 | return ret; |
| 4802 | } |
| 4803 | pii->image_id = ceph_extract_encoded_string(p, end, NULL, GFP_KERNEL); |
| 4804 | if (IS_ERR(pii->image_id)) { |
| 4805 | ret = PTR_ERR(pii->image_id); |
| 4806 | pii->image_id = NULL; |
| 4807 | return ret; |
| 4808 | } |
| 4809 | ceph_decode_64_safe(p, end, pii->snap_id, e_inval); |
| 4810 | return 0; |
| 4811 | |
| 4812 | e_inval: |
| 4813 | return -EINVAL; |
| 4814 | } |
| 4815 | |
| 4816 | static int __get_parent_info(struct rbd_device *rbd_dev, |
| 4817 | struct page *req_page, |
| 4818 | struct page *reply_page, |
| 4819 | struct parent_image_info *pii) |
| 4820 | { |
| 4821 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4822 | size_t reply_len = PAGE_SIZE; |
| 4823 | void *p, *end; |
| 4824 | int ret; |
| 4825 | |
| 4826 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4827 | "rbd", "parent_get", CEPH_OSD_FLAG_READ, |
| 4828 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4829 | if (ret) |
| 4830 | return ret == -EOPNOTSUPP ? 1 : ret; |
| 4831 | |
| 4832 | p = page_address(reply_page); |
| 4833 | end = p + reply_len; |
| 4834 | ret = decode_parent_image_spec(&p, end, pii); |
| 4835 | if (ret) |
| 4836 | return ret; |
| 4837 | |
| 4838 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4839 | "rbd", "parent_overlap_get", CEPH_OSD_FLAG_READ, |
| 4840 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4841 | if (ret) |
| 4842 | return ret; |
| 4843 | |
| 4844 | p = page_address(reply_page); |
| 4845 | end = p + reply_len; |
| 4846 | ceph_decode_8_safe(&p, end, pii->has_overlap, e_inval); |
| 4847 | if (pii->has_overlap) |
| 4848 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 4849 | |
| 4850 | return 0; |
| 4851 | |
| 4852 | e_inval: |
| 4853 | return -EINVAL; |
| 4854 | } |
| 4855 | |
| 4856 | /* |
| 4857 | * The caller is responsible for @pii. |
| 4858 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4859 | static int __get_parent_info_legacy(struct rbd_device *rbd_dev, |
| 4860 | struct page *req_page, |
| 4861 | struct page *reply_page, |
| 4862 | struct parent_image_info *pii) |
| 4863 | { |
| 4864 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 4865 | size_t reply_len = PAGE_SIZE; |
| 4866 | void *p, *end; |
| 4867 | int ret; |
| 4868 | |
| 4869 | ret = ceph_osdc_call(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 4870 | "rbd", "get_parent", CEPH_OSD_FLAG_READ, |
| 4871 | req_page, sizeof(u64), reply_page, &reply_len); |
| 4872 | if (ret) |
| 4873 | return ret; |
| 4874 | |
| 4875 | p = page_address(reply_page); |
| 4876 | end = p + reply_len; |
| 4877 | ceph_decode_64_safe(&p, end, pii->pool_id, e_inval); |
| 4878 | pii->image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
| 4879 | if (IS_ERR(pii->image_id)) { |
| 4880 | ret = PTR_ERR(pii->image_id); |
| 4881 | pii->image_id = NULL; |
| 4882 | return ret; |
| 4883 | } |
| 4884 | ceph_decode_64_safe(&p, end, pii->snap_id, e_inval); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4885 | pii->has_overlap = true; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4886 | ceph_decode_64_safe(&p, end, pii->overlap, e_inval); |
| 4887 | |
| 4888 | return 0; |
| 4889 | |
| 4890 | e_inval: |
| 4891 | return -EINVAL; |
| 4892 | } |
| 4893 | |
| 4894 | static int get_parent_info(struct rbd_device *rbd_dev, |
| 4895 | struct parent_image_info *pii) |
| 4896 | { |
| 4897 | struct page *req_page, *reply_page; |
| 4898 | void *p; |
| 4899 | int ret; |
| 4900 | |
| 4901 | req_page = alloc_page(GFP_KERNEL); |
| 4902 | if (!req_page) |
| 4903 | return -ENOMEM; |
| 4904 | |
| 4905 | reply_page = alloc_page(GFP_KERNEL); |
| 4906 | if (!reply_page) { |
| 4907 | __free_page(req_page); |
| 4908 | return -ENOMEM; |
| 4909 | } |
| 4910 | |
| 4911 | p = page_address(req_page); |
| 4912 | ceph_encode_64(&p, rbd_dev->spec->snap_id); |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4913 | ret = __get_parent_info(rbd_dev, req_page, reply_page, pii); |
| 4914 | if (ret > 0) |
| 4915 | ret = __get_parent_info_legacy(rbd_dev, req_page, reply_page, |
| 4916 | pii); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4917 | |
| 4918 | __free_page(req_page); |
| 4919 | __free_page(reply_page); |
| 4920 | return ret; |
| 4921 | } |
| 4922 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4923 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 4924 | { |
| 4925 | struct rbd_spec *parent_spec; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4926 | struct parent_image_info pii = { 0 }; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4927 | int ret; |
| 4928 | |
| 4929 | parent_spec = rbd_spec_alloc(); |
| 4930 | if (!parent_spec) |
| 4931 | return -ENOMEM; |
| 4932 | |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4933 | ret = get_parent_info(rbd_dev, &pii); |
| 4934 | if (ret) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4935 | goto out_err; |
| 4936 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4937 | dout("%s pool_id %llu pool_ns %s image_id %s snap_id %llu has_overlap %d overlap %llu\n", |
| 4938 | __func__, pii.pool_id, pii.pool_ns, pii.image_id, pii.snap_id, |
| 4939 | pii.has_overlap, pii.overlap); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4940 | |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4941 | if (pii.pool_id == CEPH_NOPOOL || !pii.has_overlap) { |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4942 | /* |
| 4943 | * Either the parent never existed, or we have |
| 4944 | * record of it but the image got flattened so it no |
| 4945 | * longer has a parent. When the parent of a |
| 4946 | * layered image disappears we immediately set the |
| 4947 | * overlap to 0. The effect of this is that all new |
| 4948 | * requests will be treated as if the image had no |
| 4949 | * parent. |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4950 | * |
| 4951 | * If !pii.has_overlap, the parent image spec is not |
| 4952 | * applicable. It's there to avoid duplication in each |
| 4953 | * snapshot record. |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4954 | */ |
| 4955 | if (rbd_dev->parent_overlap) { |
| 4956 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4957 | rbd_dev_parent_put(rbd_dev); |
| 4958 | pr_info("%s: clone image has been flattened\n", |
| 4959 | rbd_dev->disk->disk_name); |
| 4960 | } |
| 4961 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4962 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4963 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4964 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4965 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 4966 | |
| 4967 | ret = -EIO; |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4968 | if (pii.pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 4969 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4970 | (unsigned long long)pii.pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4971 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4972 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4973 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4974 | /* |
| 4975 | * The parent won't change (except when the clone is |
| 4976 | * flattened, already handled that). So we only need to |
| 4977 | * record the parent spec we have not already done so. |
| 4978 | */ |
| 4979 | if (!rbd_dev->parent_spec) { |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4980 | parent_spec->pool_id = pii.pool_id; |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 4981 | if (pii.pool_ns && *pii.pool_ns) { |
| 4982 | parent_spec->pool_ns = pii.pool_ns; |
| 4983 | pii.pool_ns = NULL; |
| 4984 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4985 | parent_spec->image_id = pii.image_id; |
| 4986 | pii.image_id = NULL; |
| 4987 | parent_spec->snap_id = pii.snap_id; |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 4988 | |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 4989 | rbd_dev->parent_spec = parent_spec; |
| 4990 | parent_spec = NULL; /* rbd_dev now owns this */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4991 | } |
| 4992 | |
| 4993 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4994 | * We always update the parent overlap. If it's zero we issue |
| 4995 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4996 | */ |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 4997 | if (!pii.overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4998 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 4999 | /* refresh, careful to warn just once */ |
| 5000 | if (rbd_dev->parent_overlap) |
| 5001 | rbd_warn(rbd_dev, |
| 5002 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5003 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5004 | /* initial probe */ |
| 5005 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5006 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5007 | } |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5008 | rbd_dev->parent_overlap = pii.overlap; |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5009 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5010 | out: |
| 5011 | ret = 0; |
| 5012 | out_err: |
Ilya Dryomov | e92c0ea | 2018-08-22 17:26:10 +0200 | [diff] [blame] | 5013 | kfree(pii.pool_ns); |
Ilya Dryomov | eb3b2d6 | 2018-08-22 17:11:27 +0200 | [diff] [blame] | 5014 | kfree(pii.image_id); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5015 | rbd_spec_put(parent_spec); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5016 | return ret; |
| 5017 | } |
| 5018 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5019 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5020 | { |
| 5021 | struct { |
| 5022 | __le64 stripe_unit; |
| 5023 | __le64 stripe_count; |
| 5024 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5025 | size_t size = sizeof (striping_info_buf); |
| 5026 | void *p; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5027 | int ret; |
| 5028 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5029 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5030 | &rbd_dev->header_oloc, "get_stripe_unit_count", |
| 5031 | NULL, 0, &striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5032 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5033 | if (ret < 0) |
| 5034 | return ret; |
| 5035 | if (ret < size) |
| 5036 | return -ERANGE; |
| 5037 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5038 | p = &striping_info_buf; |
Ilya Dryomov | b133185 | 2018-02-07 12:09:12 +0100 | [diff] [blame] | 5039 | rbd_dev->header.stripe_unit = ceph_decode_64(&p); |
| 5040 | rbd_dev->header.stripe_count = ceph_decode_64(&p); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5041 | return 0; |
| 5042 | } |
| 5043 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5044 | static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev) |
| 5045 | { |
| 5046 | __le64 data_pool_id; |
| 5047 | int ret; |
| 5048 | |
| 5049 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5050 | &rbd_dev->header_oloc, "get_data_pool", |
| 5051 | NULL, 0, &data_pool_id, sizeof(data_pool_id)); |
| 5052 | if (ret < 0) |
| 5053 | return ret; |
| 5054 | if (ret < sizeof(data_pool_id)) |
| 5055 | return -EBADMSG; |
| 5056 | |
| 5057 | rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id); |
| 5058 | WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL); |
| 5059 | return 0; |
| 5060 | } |
| 5061 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5062 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5063 | { |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5064 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5065 | size_t image_id_size; |
| 5066 | char *image_id; |
| 5067 | void *p; |
| 5068 | void *end; |
| 5069 | size_t size; |
| 5070 | void *reply_buf = NULL; |
| 5071 | size_t len = 0; |
| 5072 | char *image_name = NULL; |
| 5073 | int ret; |
| 5074 | |
| 5075 | rbd_assert(!rbd_dev->spec->image_name); |
| 5076 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5077 | len = strlen(rbd_dev->spec->image_id); |
| 5078 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5079 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5080 | if (!image_id) |
| 5081 | return NULL; |
| 5082 | |
| 5083 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5084 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5085 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5086 | |
| 5087 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5088 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5089 | if (!reply_buf) |
| 5090 | goto out; |
| 5091 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5092 | ceph_oid_printf(&oid, "%s", RBD_DIRECTORY); |
| 5093 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5094 | "dir_get_name", image_id, image_id_size, |
| 5095 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5096 | if (ret < 0) |
| 5097 | goto out; |
| 5098 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5099 | end = reply_buf + ret; |
| 5100 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5101 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5102 | if (IS_ERR(image_name)) |
| 5103 | image_name = NULL; |
| 5104 | else |
| 5105 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5106 | out: |
| 5107 | kfree(reply_buf); |
| 5108 | kfree(image_id); |
| 5109 | |
| 5110 | return image_name; |
| 5111 | } |
| 5112 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5113 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5114 | { |
| 5115 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5116 | const char *snap_name; |
| 5117 | u32 which = 0; |
| 5118 | |
| 5119 | /* Skip over names until we find the one we are looking for */ |
| 5120 | |
| 5121 | snap_name = rbd_dev->header.snap_names; |
| 5122 | while (which < snapc->num_snaps) { |
| 5123 | if (!strcmp(name, snap_name)) |
| 5124 | return snapc->snaps[which]; |
| 5125 | snap_name += strlen(snap_name) + 1; |
| 5126 | which++; |
| 5127 | } |
| 5128 | return CEPH_NOSNAP; |
| 5129 | } |
| 5130 | |
| 5131 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5132 | { |
| 5133 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5134 | u32 which; |
| 5135 | bool found = false; |
| 5136 | u64 snap_id; |
| 5137 | |
| 5138 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5139 | const char *snap_name; |
| 5140 | |
| 5141 | snap_id = snapc->snaps[which]; |
| 5142 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5143 | if (IS_ERR(snap_name)) { |
| 5144 | /* ignore no-longer existing snapshots */ |
| 5145 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5146 | continue; |
| 5147 | else |
| 5148 | break; |
| 5149 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5150 | found = !strcmp(name, snap_name); |
| 5151 | kfree(snap_name); |
| 5152 | } |
| 5153 | return found ? snap_id : CEPH_NOSNAP; |
| 5154 | } |
| 5155 | |
| 5156 | /* |
| 5157 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5158 | * no snapshot by that name is found, or if an error occurs. |
| 5159 | */ |
| 5160 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5161 | { |
| 5162 | if (rbd_dev->image_format == 1) |
| 5163 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5164 | |
| 5165 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5166 | } |
| 5167 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5168 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5169 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5170 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5171 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5172 | { |
| 5173 | struct rbd_spec *spec = rbd_dev->spec; |
| 5174 | |
| 5175 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5176 | rbd_assert(spec->image_id && spec->image_name); |
| 5177 | rbd_assert(spec->snap_name); |
| 5178 | |
| 5179 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5180 | u64 snap_id; |
| 5181 | |
| 5182 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5183 | if (snap_id == CEPH_NOSNAP) |
| 5184 | return -ENOENT; |
| 5185 | |
| 5186 | spec->snap_id = snap_id; |
| 5187 | } else { |
| 5188 | spec->snap_id = CEPH_NOSNAP; |
| 5189 | } |
| 5190 | |
| 5191 | return 0; |
| 5192 | } |
| 5193 | |
| 5194 | /* |
| 5195 | * A parent image will have all ids but none of the names. |
| 5196 | * |
| 5197 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5198 | * can't figure out the name for an image id. |
| 5199 | */ |
| 5200 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5201 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5202 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5203 | struct rbd_spec *spec = rbd_dev->spec; |
| 5204 | const char *pool_name; |
| 5205 | const char *image_name; |
| 5206 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5207 | int ret; |
| 5208 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5209 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5210 | rbd_assert(spec->image_id); |
| 5211 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5212 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5213 | /* 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] | 5214 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5215 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5216 | if (!pool_name) { |
| 5217 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5218 | return -EIO; |
| 5219 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5220 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5221 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5222 | return -ENOMEM; |
| 5223 | |
| 5224 | /* Fetch the image name; tolerate failure here */ |
| 5225 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5226 | image_name = rbd_dev_image_name(rbd_dev); |
| 5227 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5228 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5229 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5230 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5231 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5232 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5233 | if (IS_ERR(snap_name)) { |
| 5234 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5235 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | spec->pool_name = pool_name; |
| 5239 | spec->image_name = image_name; |
| 5240 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5241 | |
| 5242 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5243 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5244 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5245 | kfree(image_name); |
| 5246 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5247 | return ret; |
| 5248 | } |
| 5249 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5250 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5251 | { |
| 5252 | size_t size; |
| 5253 | int ret; |
| 5254 | void *reply_buf; |
| 5255 | void *p; |
| 5256 | void *end; |
| 5257 | u64 seq; |
| 5258 | u32 snap_count; |
| 5259 | struct ceph_snap_context *snapc; |
| 5260 | u32 i; |
| 5261 | |
| 5262 | /* |
| 5263 | * We'll need room for the seq value (maximum snapshot id), |
| 5264 | * snapshot count, and array of that many snapshot ids. |
| 5265 | * For now we have a fixed upper limit on the number we're |
| 5266 | * prepared to receive. |
| 5267 | */ |
| 5268 | size = sizeof (__le64) + sizeof (__le32) + |
| 5269 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5270 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5271 | if (!reply_buf) |
| 5272 | return -ENOMEM; |
| 5273 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5274 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5275 | &rbd_dev->header_oloc, "get_snapcontext", |
| 5276 | NULL, 0, reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5277 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5278 | if (ret < 0) |
| 5279 | goto out; |
| 5280 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5281 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5282 | end = reply_buf + ret; |
| 5283 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5284 | ceph_decode_64_safe(&p, end, seq, out); |
| 5285 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5286 | |
| 5287 | /* |
| 5288 | * Make sure the reported number of snapshot ids wouldn't go |
| 5289 | * beyond the end of our buffer. But before checking that, |
| 5290 | * make sure the computed size of the snapshot context we |
| 5291 | * allocate is representable in a size_t. |
| 5292 | */ |
| 5293 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5294 | / sizeof (u64)) { |
| 5295 | ret = -EINVAL; |
| 5296 | goto out; |
| 5297 | } |
| 5298 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5299 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5300 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5301 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5302 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5303 | if (!snapc) { |
| 5304 | ret = -ENOMEM; |
| 5305 | goto out; |
| 5306 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5307 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5308 | for (i = 0; i < snap_count; i++) |
| 5309 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5310 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5311 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5312 | rbd_dev->header.snapc = snapc; |
| 5313 | |
| 5314 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5315 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5316 | out: |
| 5317 | kfree(reply_buf); |
| 5318 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5319 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5320 | } |
| 5321 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5322 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5323 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5324 | { |
| 5325 | size_t size; |
| 5326 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5327 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5328 | int ret; |
| 5329 | void *p; |
| 5330 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5331 | char *snap_name; |
| 5332 | |
| 5333 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5334 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5335 | if (!reply_buf) |
| 5336 | return ERR_PTR(-ENOMEM); |
| 5337 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5338 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5339 | ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid, |
| 5340 | &rbd_dev->header_oloc, "get_snapshot_name", |
| 5341 | &snapid, sizeof(snapid), reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5342 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5343 | if (ret < 0) { |
| 5344 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5345 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5346 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5347 | |
| 5348 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5349 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5350 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5351 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5352 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5353 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5354 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5355 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5356 | out: |
| 5357 | kfree(reply_buf); |
| 5358 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5359 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5360 | } |
| 5361 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5362 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5363 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5364 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5365 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5366 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5367 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5368 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5369 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5370 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5371 | if (first_time) { |
| 5372 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5373 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5374 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5375 | } |
| 5376 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5377 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5378 | if (ret && first_time) { |
| 5379 | kfree(rbd_dev->header.object_prefix); |
| 5380 | rbd_dev->header.object_prefix = NULL; |
| 5381 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5382 | |
| 5383 | return ret; |
| 5384 | } |
| 5385 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5386 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5387 | { |
| 5388 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5389 | |
| 5390 | if (rbd_dev->image_format == 1) |
| 5391 | return rbd_dev_v1_header_info(rbd_dev); |
| 5392 | |
| 5393 | return rbd_dev_v2_header_info(rbd_dev); |
| 5394 | } |
| 5395 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5396 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5397 | * Skips over white space at *buf, and updates *buf to point to the |
| 5398 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5399 | * the token (string of non-white space characters) found. Note |
| 5400 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5401 | */ |
| 5402 | static inline size_t next_token(const char **buf) |
| 5403 | { |
| 5404 | /* |
| 5405 | * These are the characters that produce nonzero for |
| 5406 | * isspace() in the "C" and "POSIX" locales. |
| 5407 | */ |
| 5408 | const char *spaces = " \f\n\r\t\v"; |
| 5409 | |
| 5410 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5411 | |
| 5412 | return strcspn(*buf, spaces); /* Return token length */ |
| 5413 | } |
| 5414 | |
| 5415 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5416 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5417 | * enough to hold a copy of it, and copies the token into the new |
| 5418 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5419 | * that a duplicate buffer is created even for a zero-length token. |
| 5420 | * |
| 5421 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5422 | * pointer if memory for the duplicate was not available. If |
| 5423 | * the lenp argument is a non-null pointer, the length of the token |
| 5424 | * (not including the '\0') is returned in *lenp. |
| 5425 | * |
| 5426 | * If successful, the *buf pointer will be updated to point beyond |
| 5427 | * the end of the found token. |
| 5428 | * |
| 5429 | * Note: uses GFP_KERNEL for allocation. |
| 5430 | */ |
| 5431 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5432 | { |
| 5433 | char *dup; |
| 5434 | size_t len; |
| 5435 | |
| 5436 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5437 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5438 | if (!dup) |
| 5439 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5440 | *(dup + len) = '\0'; |
| 5441 | *buf += len; |
| 5442 | |
| 5443 | if (lenp) |
| 5444 | *lenp = len; |
| 5445 | |
| 5446 | return dup; |
| 5447 | } |
| 5448 | |
| 5449 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5450 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5451 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5452 | * and the data written is passed here via a NUL-terminated buffer. |
| 5453 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5454 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5455 | * The information extracted from these options is recorded in |
| 5456 | * the other parameters which return dynamically-allocated |
| 5457 | * structures: |
| 5458 | * ceph_opts |
| 5459 | * The address of a pointer that will refer to a ceph options |
| 5460 | * structure. Caller must release the returned pointer using |
| 5461 | * ceph_destroy_options() when it is no longer needed. |
| 5462 | * rbd_opts |
| 5463 | * Address of an rbd options pointer. Fully initialized by |
| 5464 | * this function; caller must release with kfree(). |
| 5465 | * spec |
| 5466 | * Address of an rbd image specification pointer. Fully |
| 5467 | * initialized by this function based on parsed options. |
| 5468 | * Caller must release with rbd_spec_put(). |
| 5469 | * |
| 5470 | * The options passed take this form: |
| 5471 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5472 | * where: |
| 5473 | * <mon_addrs> |
| 5474 | * A comma-separated list of one or more monitor addresses. |
| 5475 | * A monitor address is an ip address, optionally followed |
| 5476 | * by a port number (separated by a colon). |
| 5477 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5478 | * <options> |
| 5479 | * A comma-separated list of ceph and/or rbd options. |
| 5480 | * <pool_name> |
| 5481 | * The name of the rados pool containing the rbd image. |
| 5482 | * <image_name> |
| 5483 | * The name of the image in that pool to map. |
| 5484 | * <snap_id> |
| 5485 | * An optional snapshot id. If provided, the mapping will |
| 5486 | * present data from the image at the time that snapshot was |
| 5487 | * created. The image head is used if no snapshot id is |
| 5488 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5489 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5490 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5491 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5492 | struct rbd_options **opts, |
| 5493 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5494 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5495 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5496 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5497 | const char *mon_addrs; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5498 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5499 | size_t mon_addrs_size; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5500 | struct parse_rbd_opts_ctx pctx = { 0 }; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5501 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5502 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5503 | |
| 5504 | /* The first four tokens are required */ |
| 5505 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5506 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5507 | if (!len) { |
| 5508 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5509 | return -EINVAL; |
| 5510 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5511 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5512 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5513 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5514 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5515 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5516 | options = dup_token(&buf, NULL); |
| 5517 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5518 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5519 | if (!*options) { |
| 5520 | rbd_warn(NULL, "no options provided"); |
| 5521 | goto out_err; |
| 5522 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5523 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5524 | pctx.spec = rbd_spec_alloc(); |
| 5525 | if (!pctx.spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5526 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5527 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5528 | pctx.spec->pool_name = dup_token(&buf, NULL); |
| 5529 | if (!pctx.spec->pool_name) |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5530 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5531 | if (!*pctx.spec->pool_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5532 | rbd_warn(NULL, "no pool name provided"); |
| 5533 | goto out_err; |
| 5534 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5535 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5536 | pctx.spec->image_name = dup_token(&buf, NULL); |
| 5537 | if (!pctx.spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5538 | goto out_mem; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5539 | if (!*pctx.spec->image_name) { |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5540 | rbd_warn(NULL, "no image name provided"); |
| 5541 | goto out_err; |
| 5542 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5543 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5544 | /* |
| 5545 | * Snapshot name is optional; default is to use "-" |
| 5546 | * (indicating the head/no snapshot). |
| 5547 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5548 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5549 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5550 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5551 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5552 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5553 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5554 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5555 | } |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5556 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5557 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5558 | goto out_mem; |
Alex Elder | ecb4dc22 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5559 | *(snap_name + len) = '\0'; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5560 | pctx.spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5561 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5562 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5563 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5564 | pctx.opts = kzalloc(sizeof(*pctx.opts), GFP_KERNEL); |
| 5565 | if (!pctx.opts) |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5566 | goto out_mem; |
| 5567 | |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5568 | pctx.opts->read_only = RBD_READ_ONLY_DEFAULT; |
| 5569 | pctx.opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 5570 | pctx.opts->alloc_size = RBD_ALLOC_SIZE_DEFAULT; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5571 | pctx.opts->lock_timeout = RBD_LOCK_TIMEOUT_DEFAULT; |
| 5572 | pctx.opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
| 5573 | pctx.opts->exclusive = RBD_EXCLUSIVE_DEFAULT; |
| 5574 | pctx.opts->trim = RBD_TRIM_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5575 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5576 | copts = ceph_parse_options(options, mon_addrs, |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5577 | mon_addrs + mon_addrs_size - 1, |
| 5578 | parse_rbd_opts_token, &pctx); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5579 | if (IS_ERR(copts)) { |
| 5580 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5581 | goto out_err; |
| 5582 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5583 | kfree(options); |
| 5584 | |
| 5585 | *ceph_opts = copts; |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5586 | *opts = pctx.opts; |
| 5587 | *rbd_spec = pctx.spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5588 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5589 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5590 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5591 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5592 | out_err: |
Ilya Dryomov | c300156 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5593 | kfree(pctx.opts); |
| 5594 | rbd_spec_put(pctx.spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5595 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5596 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5597 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5598 | } |
| 5599 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5600 | static void rbd_dev_image_unlock(struct rbd_device *rbd_dev) |
| 5601 | { |
| 5602 | down_write(&rbd_dev->lock_rwsem); |
| 5603 | if (__rbd_is_lock_owner(rbd_dev)) |
| 5604 | rbd_unlock(rbd_dev); |
| 5605 | up_write(&rbd_dev->lock_rwsem); |
| 5606 | } |
| 5607 | |
| 5608 | static int rbd_add_acquire_lock(struct rbd_device *rbd_dev) |
| 5609 | { |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5610 | int ret; |
| 5611 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5612 | if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) { |
| 5613 | rbd_warn(rbd_dev, "exclusive-lock feature is not enabled"); |
| 5614 | return -EINVAL; |
| 5615 | } |
| 5616 | |
| 5617 | /* FIXME: "rbd map --exclusive" should be in interruptible */ |
| 5618 | down_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5619 | ret = rbd_wait_state_locked(rbd_dev, true); |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5620 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 2f18d46 | 2018-04-04 10:15:38 +0200 | [diff] [blame] | 5621 | if (ret) { |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 5622 | rbd_warn(rbd_dev, "failed to acquire exclusive lock"); |
| 5623 | return -EROFS; |
| 5624 | } |
| 5625 | |
| 5626 | return 0; |
| 5627 | } |
| 5628 | |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5629 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5630 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5631 | * name given to it by the user. Internally, that identifier is |
| 5632 | * what's used to specify the names of objects related to the image. |
| 5633 | * |
| 5634 | * A special "rbd id" object is used to map an rbd image name to its |
| 5635 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5636 | * with the supplied name. |
| 5637 | * |
| 5638 | * This function will record the given rbd_dev's image_id field if |
| 5639 | * it can be determined, and in that case will return 0. If any |
| 5640 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5641 | * image_id field will be unchanged (and should be NULL). |
| 5642 | */ |
| 5643 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5644 | { |
| 5645 | int ret; |
| 5646 | size_t size; |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5647 | CEPH_DEFINE_OID_ONSTACK(oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5648 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5649 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5650 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5651 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5652 | * When probing a parent image, the image id is already |
| 5653 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5654 | * need to fetch the image id again in this case. We |
| 5655 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5656 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5657 | if (rbd_dev->spec->image_id) { |
| 5658 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5659 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5660 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5661 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5662 | |
| 5663 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5664 | * First, see if the format 2 image id file exists, and if |
| 5665 | * so, get the image's persistent id from it. |
| 5666 | */ |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5667 | ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX, |
| 5668 | rbd_dev->spec->image_name); |
| 5669 | if (ret) |
| 5670 | return ret; |
| 5671 | |
| 5672 | dout("rbd id object name is %s\n", oid.name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5673 | |
| 5674 | /* Response will be an encoded string, which includes a length */ |
| 5675 | |
| 5676 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5677 | response = kzalloc(size, GFP_NOIO); |
| 5678 | if (!response) { |
| 5679 | ret = -ENOMEM; |
| 5680 | goto out; |
| 5681 | } |
| 5682 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5683 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5684 | |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5685 | ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc, |
| 5686 | "get_id", NULL, 0, |
| 5687 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5688 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5689 | if (ret == -ENOENT) { |
| 5690 | image_id = kstrdup("", GFP_KERNEL); |
| 5691 | ret = image_id ? 0 : -ENOMEM; |
| 5692 | if (!ret) |
| 5693 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5694 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5695 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5696 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5697 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5698 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5699 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5700 | if (!ret) |
| 5701 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | if (!ret) { |
| 5705 | rbd_dev->spec->image_id = image_id; |
| 5706 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5707 | } |
| 5708 | out: |
| 5709 | kfree(response); |
Ilya Dryomov | ecd4a68 | 2017-01-25 18:16:21 +0100 | [diff] [blame] | 5710 | ceph_oid_destroy(&oid); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5711 | return ret; |
| 5712 | } |
| 5713 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5714 | /* |
| 5715 | * Undo whatever state changes are made by v1 or v2 header info |
| 5716 | * call. |
| 5717 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5718 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5719 | { |
| 5720 | struct rbd_image_header *header; |
| 5721 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5722 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5723 | |
| 5724 | /* Free dynamic fields from the header, then zero it out */ |
| 5725 | |
| 5726 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5727 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5728 | kfree(header->snap_sizes); |
| 5729 | kfree(header->snap_names); |
| 5730 | kfree(header->object_prefix); |
| 5731 | memset(header, 0, sizeof (*header)); |
| 5732 | } |
| 5733 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5734 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5735 | { |
| 5736 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5737 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5738 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5739 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5740 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5741 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5742 | /* |
| 5743 | * Get the and check features for the image. Currently the |
| 5744 | * features are assumed to never change. |
| 5745 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5746 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5747 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5748 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5749 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5750 | /* If the image supports fancy striping, get its parameters */ |
| 5751 | |
| 5752 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5753 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5754 | if (ret < 0) |
| 5755 | goto out_err; |
| 5756 | } |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5757 | |
Ilya Dryomov | 7e97332 | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5758 | if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) { |
| 5759 | ret = rbd_dev_v2_data_pool(rbd_dev); |
| 5760 | if (ret) |
| 5761 | goto out_err; |
| 5762 | } |
| 5763 | |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5764 | rbd_init_layout(rbd_dev); |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5765 | return 0; |
Ilya Dryomov | 263423f | 2017-01-25 18:16:22 +0100 | [diff] [blame] | 5766 | |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5767 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5768 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5769 | kfree(rbd_dev->header.object_prefix); |
| 5770 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5771 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5772 | } |
| 5773 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5774 | /* |
| 5775 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5776 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5777 | * length of the already discovered part of the parent chain. |
| 5778 | */ |
| 5779 | 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] | 5780 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5781 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5782 | int ret; |
| 5783 | |
| 5784 | if (!rbd_dev->parent_spec) |
| 5785 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5786 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5787 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5788 | pr_info("parent chain is too long (%d)\n", depth); |
| 5789 | ret = -EINVAL; |
| 5790 | goto out_err; |
| 5791 | } |
| 5792 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5793 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5794 | if (!parent) { |
| 5795 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5796 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5797 | } |
| 5798 | |
| 5799 | /* |
| 5800 | * Images related by parent/child relationships always share |
| 5801 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5802 | */ |
| 5803 | __rbd_get_client(rbd_dev->rbd_client); |
| 5804 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5805 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5806 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5807 | if (ret < 0) |
| 5808 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5809 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5810 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5811 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5812 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5813 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5814 | out_err: |
| 5815 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5816 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5817 | return ret; |
| 5818 | } |
| 5819 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5820 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
| 5821 | { |
| 5822 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 5823 | rbd_dev_mapping_clear(rbd_dev); |
| 5824 | rbd_free_disk(rbd_dev); |
| 5825 | if (!single_major) |
| 5826 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
| 5827 | } |
| 5828 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5829 | /* |
| 5830 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5831 | * upon return. |
| 5832 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5833 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5834 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5835 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5836 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5837 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5838 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5839 | if (!single_major) { |
| 5840 | ret = register_blkdev(0, rbd_dev->name); |
| 5841 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5842 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5843 | |
| 5844 | rbd_dev->major = ret; |
| 5845 | rbd_dev->minor = 0; |
| 5846 | } else { |
| 5847 | rbd_dev->major = rbd_major; |
| 5848 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 5849 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5850 | |
| 5851 | /* Set up the blkdev mapping. */ |
| 5852 | |
| 5853 | ret = rbd_init_disk(rbd_dev); |
| 5854 | if (ret) |
| 5855 | goto err_out_blkdev; |
| 5856 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5857 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5858 | if (ret) |
| 5859 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 5860 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5861 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 5862 | set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5863 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5864 | ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5865 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 5866 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5867 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5868 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5869 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 5870 | return 0; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5871 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 5872 | err_out_mapping: |
| 5873 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5874 | err_out_disk: |
| 5875 | rbd_free_disk(rbd_dev); |
| 5876 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5877 | if (!single_major) |
| 5878 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5879 | err_out_unlock: |
| 5880 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5881 | return ret; |
| 5882 | } |
| 5883 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5884 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 5885 | { |
| 5886 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5887 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5888 | |
| 5889 | /* Record the header object name for this rbd image. */ |
| 5890 | |
| 5891 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5892 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5893 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5894 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5895 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5896 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 5897 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5898 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5899 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5900 | } |
| 5901 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5902 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 5903 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5904 | rbd_dev_unprobe(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 5905 | if (rbd_dev->opts) |
| 5906 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5907 | rbd_dev->image_format = 0; |
| 5908 | kfree(rbd_dev->spec->image_id); |
| 5909 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5910 | } |
| 5911 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5912 | /* |
| 5913 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5914 | * device. If this image is the one being mapped (i.e., not a |
| 5915 | * parent), initiate a watch on its header object before using that |
| 5916 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5917 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5918 | 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] | 5919 | { |
| 5920 | int ret; |
| 5921 | |
| 5922 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5923 | * Get the id from the image id object. Unless there's an |
| 5924 | * error, rbd_dev->spec->image_id will be filled in with |
| 5925 | * a dynamically-allocated string, and rbd_dev->image_format |
| 5926 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5927 | */ |
| 5928 | ret = rbd_dev_image_id(rbd_dev); |
| 5929 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5930 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5931 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5932 | ret = rbd_dev_header_name(rbd_dev); |
| 5933 | if (ret) |
| 5934 | goto err_out_format; |
| 5935 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5936 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5937 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5938 | if (ret) { |
| 5939 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5940 | pr_info("image %s/%s%s%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5941 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5942 | rbd_dev->spec->pool_ns ?: "", |
| 5943 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5944 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5945 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5946 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5947 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5948 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5949 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5950 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5951 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5952 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5953 | /* |
| 5954 | * If this image is the one being mapped, we have pool name and |
| 5955 | * id, image name and id, and snap name - need to fill snap id. |
| 5956 | * Otherwise this is a parent image, identified by pool, image |
| 5957 | * and snap ids - need to fill in names for those ids. |
| 5958 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5959 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5960 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 5961 | else |
| 5962 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5963 | if (ret) { |
| 5964 | if (ret == -ENOENT) |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5965 | pr_info("snap %s/%s%s%s@%s does not exist\n", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5966 | rbd_dev->spec->pool_name, |
Ilya Dryomov | b26c047 | 2018-07-03 15:28:43 +0200 | [diff] [blame] | 5967 | rbd_dev->spec->pool_ns ?: "", |
| 5968 | rbd_dev->spec->pool_ns ? "/" : "", |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5969 | rbd_dev->spec->image_name, |
| 5970 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5971 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 5972 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5973 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5974 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 5975 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 5976 | if (ret) |
| 5977 | goto err_out_probe; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5978 | } |
| 5979 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5980 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5981 | if (ret) |
| 5982 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5983 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5984 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5985 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5986 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5987 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5988 | err_out_probe: |
| 5989 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 5990 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5991 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 5992 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 5993 | err_out_format: |
| 5994 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5995 | kfree(rbd_dev->spec->image_id); |
| 5996 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5997 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5998 | } |
| 5999 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6000 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6001 | const char *buf, |
| 6002 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6003 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6004 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6005 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6006 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6007 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6008 | struct rbd_client *rbdc; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6009 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6010 | |
| 6011 | if (!try_module_get(THIS_MODULE)) |
| 6012 | return -ENODEV; |
| 6013 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6014 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6015 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6016 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6017 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6018 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6019 | rbdc = rbd_get_client(ceph_opts); |
| 6020 | if (IS_ERR(rbdc)) { |
| 6021 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6022 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6023 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6024 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6025 | /* pick the pool */ |
Ilya Dryomov | dd43585 | 2018-02-22 13:43:24 +0100 | [diff] [blame] | 6026 | 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] | 6027 | if (rc < 0) { |
| 6028 | if (rc == -ENOENT) |
| 6029 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6030 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6031 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6032 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6033 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6034 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6035 | if (!rbd_dev) { |
| 6036 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6037 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6038 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6039 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6040 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6041 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6042 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6043 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6044 | if (!rbd_dev->config_info) { |
| 6045 | rc = -ENOMEM; |
| 6046 | goto err_out_rbd_dev; |
| 6047 | } |
| 6048 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6049 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6050 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6051 | if (rc < 0) { |
| 6052 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6053 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6054 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6055 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6056 | /* If we are mapping a snapshot it must be marked read-only */ |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6057 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
Ilya Dryomov | 9568c93 | 2017-10-12 12:35:19 +0200 | [diff] [blame] | 6058 | rbd_dev->opts->read_only = true; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6059 | |
Ilya Dryomov | 0c93e1b | 2019-01-30 15:14:48 +0100 | [diff] [blame] | 6060 | if (rbd_dev->opts->alloc_size > rbd_dev->layout.object_size) { |
| 6061 | rbd_warn(rbd_dev, "alloc_size adjusted to %u", |
| 6062 | rbd_dev->layout.object_size); |
| 6063 | rbd_dev->opts->alloc_size = rbd_dev->layout.object_size; |
| 6064 | } |
| 6065 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6066 | rc = rbd_dev_device_setup(rbd_dev); |
Ilya Dryomov | fd22aef | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6067 | if (rc) |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6068 | goto err_out_image_probe; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6069 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6070 | if (rbd_dev->opts->exclusive) { |
| 6071 | rc = rbd_add_acquire_lock(rbd_dev); |
| 6072 | if (rc) |
| 6073 | goto err_out_device_setup; |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6074 | } |
| 6075 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6076 | /* Everything's ready. Announce the disk to the world. */ |
| 6077 | |
| 6078 | rc = device_add(&rbd_dev->dev); |
| 6079 | if (rc) |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6080 | goto err_out_image_lock; |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6081 | |
| 6082 | add_disk(rbd_dev->disk); |
| 6083 | /* see rbd_init_disk() */ |
| 6084 | blk_put_queue(rbd_dev->disk->queue); |
| 6085 | |
| 6086 | spin_lock(&rbd_dev_list_lock); |
| 6087 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6088 | spin_unlock(&rbd_dev_list_lock); |
| 6089 | |
| 6090 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6091 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6092 | rbd_dev->header.features); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6093 | rc = count; |
| 6094 | out: |
| 6095 | module_put(THIS_MODULE); |
| 6096 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6097 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6098 | err_out_image_lock: |
| 6099 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6100 | err_out_device_setup: |
| 6101 | rbd_dev_device_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6102 | err_out_image_probe: |
| 6103 | rbd_dev_image_release(rbd_dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6104 | err_out_rbd_dev: |
| 6105 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6106 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6107 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6108 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6109 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6110 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6111 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6114 | 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] | 6115 | { |
| 6116 | if (single_major) |
| 6117 | return -EINVAL; |
| 6118 | |
| 6119 | return do_rbd_add(bus, buf, count); |
| 6120 | } |
| 6121 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6122 | static ssize_t add_single_major_store(struct bus_type *bus, const char *buf, |
| 6123 | size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6124 | { |
| 6125 | return do_rbd_add(bus, buf, count); |
| 6126 | } |
| 6127 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6128 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6129 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6130 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6131 | struct rbd_device *first = rbd_dev; |
| 6132 | struct rbd_device *second = first->parent; |
| 6133 | struct rbd_device *third; |
| 6134 | |
| 6135 | /* |
| 6136 | * Follow to the parent with no grandparent and |
| 6137 | * remove it. |
| 6138 | */ |
| 6139 | while (second && (third = second->parent)) { |
| 6140 | first = second; |
| 6141 | second = third; |
| 6142 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6143 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6144 | rbd_dev_image_release(second); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6145 | rbd_dev_destroy(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6146 | first->parent = NULL; |
| 6147 | first->parent_overlap = 0; |
| 6148 | |
| 6149 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6150 | rbd_spec_put(first->parent_spec); |
| 6151 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6152 | } |
| 6153 | } |
| 6154 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6155 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6156 | const char *buf, |
| 6157 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6158 | { |
| 6159 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6160 | struct list_head *tmp; |
| 6161 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6162 | char opt_buf[6]; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6163 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6164 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6165 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6166 | dev_id = -1; |
| 6167 | opt_buf[0] = '\0'; |
| 6168 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6169 | if (dev_id < 0) { |
| 6170 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6171 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6172 | } |
| 6173 | if (opt_buf[0] != '\0') { |
| 6174 | if (!strcmp(opt_buf, "force")) { |
| 6175 | force = true; |
| 6176 | } else { |
| 6177 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6178 | return -EINVAL; |
| 6179 | } |
| 6180 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6181 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6182 | ret = -ENOENT; |
| 6183 | spin_lock(&rbd_dev_list_lock); |
| 6184 | list_for_each(tmp, &rbd_dev_list) { |
| 6185 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6186 | if (rbd_dev->dev_id == dev_id) { |
| 6187 | ret = 0; |
| 6188 | break; |
| 6189 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6190 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6191 | if (!ret) { |
| 6192 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6193 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6194 | ret = -EBUSY; |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6195 | else if (test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6196 | &rbd_dev->flags)) |
| 6197 | ret = -EINPROGRESS; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6198 | spin_unlock_irq(&rbd_dev->lock); |
| 6199 | } |
| 6200 | spin_unlock(&rbd_dev_list_lock); |
Ilya Dryomov | 85f5a4d | 2019-01-08 19:47:38 +0100 | [diff] [blame] | 6201 | if (ret) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6202 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6203 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6204 | if (force) { |
| 6205 | /* |
| 6206 | * Prevent new IO from being queued and wait for existing |
| 6207 | * IO to complete/fail. |
| 6208 | */ |
| 6209 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6210 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6211 | } |
| 6212 | |
Ilya Dryomov | 5769ed0 | 2017-04-13 12:17:38 +0200 | [diff] [blame] | 6213 | del_gendisk(rbd_dev->disk); |
| 6214 | spin_lock(&rbd_dev_list_lock); |
| 6215 | list_del_init(&rbd_dev->node); |
| 6216 | spin_unlock(&rbd_dev_list_lock); |
| 6217 | device_del(&rbd_dev->dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6218 | |
Ilya Dryomov | e010dd0 | 2017-04-13 12:17:39 +0200 | [diff] [blame] | 6219 | rbd_dev_image_unlock(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6220 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6221 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | 8b679ec | 2017-04-13 12:17:37 +0200 | [diff] [blame] | 6222 | rbd_dev_destroy(rbd_dev); |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6223 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6224 | } |
| 6225 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6226 | 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] | 6227 | { |
| 6228 | if (single_major) |
| 6229 | return -EINVAL; |
| 6230 | |
| 6231 | return do_rbd_remove(bus, buf, count); |
| 6232 | } |
| 6233 | |
Greg Kroah-Hartman | 7e9586b | 2018-12-21 08:54:38 +0100 | [diff] [blame] | 6234 | static ssize_t remove_single_major_store(struct bus_type *bus, const char *buf, |
| 6235 | size_t count) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6236 | { |
| 6237 | return do_rbd_remove(bus, buf, count); |
| 6238 | } |
| 6239 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6240 | /* |
| 6241 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6242 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6243 | */ |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6244 | static int __init rbd_sysfs_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6245 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6246 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6247 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6248 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6249 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6250 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6251 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6252 | ret = bus_register(&rbd_bus_type); |
| 6253 | if (ret < 0) |
| 6254 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6255 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6256 | return ret; |
| 6257 | } |
| 6258 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6259 | static void __exit rbd_sysfs_cleanup(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6260 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6261 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6262 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6263 | } |
| 6264 | |
Chengguang Xu | 7d8dc53 | 2018-08-12 23:06:54 +0800 | [diff] [blame] | 6265 | static int __init rbd_slab_init(void) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6266 | { |
| 6267 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6268 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6269 | if (!rbd_img_request_cache) |
| 6270 | return -ENOMEM; |
| 6271 | |
| 6272 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6273 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6274 | if (!rbd_obj_request_cache) |
| 6275 | goto out_err; |
| 6276 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6277 | return 0; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6278 | |
Ilya Dryomov | 6c696d8 | 2017-01-25 18:16:23 +0100 | [diff] [blame] | 6279 | out_err: |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6280 | kmem_cache_destroy(rbd_img_request_cache); |
| 6281 | rbd_img_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6282 | return -ENOMEM; |
| 6283 | } |
| 6284 | |
| 6285 | static void rbd_slab_exit(void) |
| 6286 | { |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6287 | rbd_assert(rbd_obj_request_cache); |
| 6288 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6289 | rbd_obj_request_cache = NULL; |
| 6290 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6291 | rbd_assert(rbd_img_request_cache); |
| 6292 | kmem_cache_destroy(rbd_img_request_cache); |
| 6293 | rbd_img_request_cache = NULL; |
| 6294 | } |
| 6295 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6296 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6297 | { |
| 6298 | int rc; |
| 6299 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6300 | if (!libceph_compatible(NULL)) { |
| 6301 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6302 | return -EINVAL; |
| 6303 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6304 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6305 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6306 | if (rc) |
| 6307 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6308 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6309 | /* |
| 6310 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6311 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6312 | */ |
| 6313 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6314 | if (!rbd_wq) { |
| 6315 | rc = -ENOMEM; |
| 6316 | goto err_out_slab; |
| 6317 | } |
| 6318 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6319 | if (single_major) { |
| 6320 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6321 | if (rbd_major < 0) { |
| 6322 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6323 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6324 | } |
| 6325 | } |
| 6326 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6327 | rc = rbd_sysfs_init(); |
| 6328 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6329 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6330 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6331 | if (single_major) |
| 6332 | pr_info("loaded (major %d)\n", rbd_major); |
| 6333 | else |
| 6334 | pr_info("loaded\n"); |
| 6335 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6336 | return 0; |
| 6337 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6338 | err_out_blkdev: |
| 6339 | if (single_major) |
| 6340 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6341 | err_out_wq: |
| 6342 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6343 | err_out_slab: |
| 6344 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6345 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6346 | } |
| 6347 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6348 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6349 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6350 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6351 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6352 | if (single_major) |
| 6353 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6354 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6355 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6356 | } |
| 6357 | |
| 6358 | module_init(rbd_init); |
| 6359 | module_exit(rbd_exit); |
| 6360 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6361 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6362 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6363 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6364 | /* following authorship retained from original osdblk.c */ |
| 6365 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6366 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6367 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6368 | MODULE_LICENSE("GPL"); |