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> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 35 | #include <linux/ceph/decode.h> |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 36 | #include <linux/parser.h> |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 37 | #include <linux/bsearch.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 38 | |
| 39 | #include <linux/kernel.h> |
| 40 | #include <linux/device.h> |
| 41 | #include <linux/module.h> |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 42 | #include <linux/blk-mq.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 43 | #include <linux/fs.h> |
| 44 | #include <linux/blkdev.h> |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 45 | #include <linux/slab.h> |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 46 | #include <linux/idr.h> |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 47 | #include <linux/workqueue.h> |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 48 | |
| 49 | #include "rbd_types.h" |
| 50 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 51 | #define RBD_DEBUG /* Activate rbd_assert() calls */ |
| 52 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 53 | /* |
| 54 | * The basic unit of block I/O is a sector. It is interpreted in a |
| 55 | * number of contexts in Linux (blk, bio, genhd), but the default is |
| 56 | * universally 512 bytes. These symbols are just slightly more |
| 57 | * meaningful than the bare numbers they represent. |
| 58 | */ |
| 59 | #define SECTOR_SHIFT 9 |
| 60 | #define SECTOR_SIZE (1ULL << SECTOR_SHIFT) |
| 61 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Increment the given counter and return its updated value. |
| 64 | * If the counter is already 0 it will not be incremented. |
| 65 | * If the counter is already at its maximum value returns |
| 66 | * -EINVAL without updating it. |
| 67 | */ |
| 68 | static int atomic_inc_return_safe(atomic_t *v) |
| 69 | { |
| 70 | unsigned int counter; |
| 71 | |
| 72 | counter = (unsigned int)__atomic_add_unless(v, 1, 0); |
| 73 | if (counter <= (unsigned int)INT_MAX) |
| 74 | return (int)counter; |
| 75 | |
| 76 | atomic_dec(v); |
| 77 | |
| 78 | return -EINVAL; |
| 79 | } |
| 80 | |
| 81 | /* Decrement the counter. Return the resulting value, or -EINVAL */ |
| 82 | static int atomic_dec_return_safe(atomic_t *v) |
| 83 | { |
| 84 | int counter; |
| 85 | |
| 86 | counter = atomic_dec_return(v); |
| 87 | if (counter >= 0) |
| 88 | return counter; |
| 89 | |
| 90 | atomic_inc(v); |
| 91 | |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 95 | #define RBD_DRV_NAME "rbd" |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 96 | |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 97 | #define RBD_MINORS_PER_MAJOR 256 |
| 98 | #define RBD_SINGLE_MAJOR_PART_SHIFT 4 |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 99 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 100 | #define RBD_MAX_PARENT_CHAIN_LEN 16 |
| 101 | |
Alex Elder | d4b125e | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 102 | #define RBD_SNAP_DEV_NAME_PREFIX "snap_" |
| 103 | #define RBD_MAX_SNAP_NAME_LEN \ |
| 104 | (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1)) |
| 105 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 106 | #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] | 107 | |
| 108 | #define RBD_SNAP_HEAD_NAME "-" |
| 109 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 110 | #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */ |
| 111 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 112 | /* This allows a single page to hold an image name sent by OSD */ |
| 113 | #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 114 | #define RBD_IMAGE_ID_LEN_MAX 64 |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 115 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 116 | #define RBD_OBJ_PREFIX_LEN_MAX 64 |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 117 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 118 | #define RBD_NOTIFY_TIMEOUT 5 /* seconds */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 119 | #define RBD_RETRY_DELAY msecs_to_jiffies(1000) |
| 120 | |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 121 | /* Feature bits */ |
| 122 | |
Alex Elder | 5cbf6f12 | 2013-04-11 09:29:48 -0500 | [diff] [blame] | 123 | #define RBD_FEATURE_LAYERING (1<<0) |
| 124 | #define RBD_FEATURE_STRIPINGV2 (1<<1) |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 125 | #define RBD_FEATURE_EXCLUSIVE_LOCK (1<<2) |
| 126 | #define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \ |
| 127 | RBD_FEATURE_STRIPINGV2 | \ |
| 128 | RBD_FEATURE_EXCLUSIVE_LOCK) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 129 | |
| 130 | /* Features supported by this (client software) implementation. */ |
| 131 | |
Alex Elder | 770eba6 | 2012-10-25 23:34:40 -0500 | [diff] [blame] | 132 | #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL) |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 133 | |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 134 | /* |
| 135 | * An RBD device name will be "rbd#", where the "rbd" comes from |
| 136 | * RBD_DRV_NAME above, and # is a unique integer identifier. |
Alex Elder | 81a8979 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 137 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 138 | #define DEV_NAME_LEN 32 |
| 139 | |
| 140 | /* |
| 141 | * block device image metadata (in-memory version) |
| 142 | */ |
| 143 | struct rbd_image_header { |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 144 | /* These six fields never change for a given rbd image */ |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 145 | char *object_prefix; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 146 | __u8 obj_order; |
| 147 | __u8 crypt_type; |
| 148 | __u8 comp_type; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 149 | u64 stripe_unit; |
| 150 | u64 stripe_count; |
| 151 | u64 features; /* Might be changeable someday? */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 152 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 153 | /* The remaining fields need to be updated occasionally */ |
| 154 | u64 image_size; |
| 155 | struct ceph_snap_context *snapc; |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 156 | char *snap_names; /* format 1 only */ |
| 157 | u64 *snap_sizes; /* format 1 only */ |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 160 | /* |
| 161 | * An rbd image specification. |
| 162 | * |
| 163 | * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely |
Alex Elder | c66c6e0 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 164 | * identify an image. Each rbd_dev structure includes a pointer to |
| 165 | * an rbd_spec structure that encapsulates this identity. |
| 166 | * |
| 167 | * Each of the id's in an rbd_spec has an associated name. For a |
| 168 | * user-mapped image, the names are supplied and the id's associated |
| 169 | * with them are looked up. For a layered image, a parent image is |
| 170 | * defined by the tuple, and the names are looked up. |
| 171 | * |
| 172 | * An rbd_dev structure contains a parent_spec pointer which is |
| 173 | * non-null if the image it represents is a child in a layered |
| 174 | * image. This pointer will refer to the rbd_spec structure used |
| 175 | * by the parent rbd_dev for its own identity (i.e., the structure |
| 176 | * is shared between the parent and child). |
| 177 | * |
| 178 | * Since these structures are populated once, during the discovery |
| 179 | * phase of image construction, they are effectively immutable so |
| 180 | * we make no effort to synchronize access to them. |
| 181 | * |
| 182 | * Note that code herein does not assume the image name is known (it |
| 183 | * could be a null pointer). |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 184 | */ |
| 185 | struct rbd_spec { |
| 186 | u64 pool_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 187 | const char *pool_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 188 | |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 189 | const char *image_id; |
| 190 | const char *image_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 191 | |
| 192 | u64 snap_id; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 193 | const char *snap_name; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 194 | |
| 195 | struct kref kref; |
| 196 | }; |
| 197 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 198 | /* |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 199 | * an instance of the client. multiple devices may share an rbd client. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 200 | */ |
| 201 | struct rbd_client { |
| 202 | struct ceph_client *client; |
| 203 | struct kref kref; |
| 204 | struct list_head node; |
| 205 | }; |
| 206 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 207 | struct rbd_img_request; |
| 208 | typedef void (*rbd_img_callback_t)(struct rbd_img_request *); |
| 209 | |
| 210 | #define BAD_WHICH U32_MAX /* Good which or bad which, which? */ |
| 211 | |
| 212 | struct rbd_obj_request; |
| 213 | typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *); |
| 214 | |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 215 | enum obj_request_type { |
| 216 | OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES |
| 217 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 218 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 219 | enum obj_operation_type { |
| 220 | OBJ_OP_WRITE, |
| 221 | OBJ_OP_READ, |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 222 | OBJ_OP_DISCARD, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 223 | }; |
| 224 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 225 | enum obj_req_flags { |
| 226 | OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 227 | OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */ |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 228 | OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */ |
| 229 | OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 230 | }; |
| 231 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 232 | struct rbd_obj_request { |
| 233 | const char *object_name; |
| 234 | u64 offset; /* object start byte */ |
| 235 | u64 length; /* bytes from offset */ |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 236 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 237 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 238 | /* |
| 239 | * An object request associated with an image will have its |
| 240 | * img_data flag set; a standalone object request will not. |
| 241 | * |
| 242 | * A standalone object request will have which == BAD_WHICH |
| 243 | * and a null obj_request pointer. |
| 244 | * |
| 245 | * An object request initiated in support of a layered image |
| 246 | * object (to check for its existence before a write) will |
| 247 | * have which == BAD_WHICH and a non-null obj_request pointer. |
| 248 | * |
| 249 | * Finally, an object request for rbd image data will have |
| 250 | * which != BAD_WHICH, and will have a non-null img_request |
| 251 | * pointer. The value of which will be in the range |
| 252 | * 0..(img_request->obj_request_count-1). |
| 253 | */ |
| 254 | union { |
| 255 | struct rbd_obj_request *obj_request; /* STAT op */ |
| 256 | struct { |
| 257 | struct rbd_img_request *img_request; |
| 258 | u64 img_offset; |
| 259 | /* links for img_request->obj_requests list */ |
| 260 | struct list_head links; |
| 261 | }; |
| 262 | }; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 263 | u32 which; /* posn image request list */ |
| 264 | |
| 265 | enum obj_request_type type; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 266 | union { |
| 267 | struct bio *bio_list; |
| 268 | struct { |
| 269 | struct page **pages; |
| 270 | u32 page_count; |
| 271 | }; |
| 272 | }; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 273 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 274 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 275 | |
| 276 | struct ceph_osd_request *osd_req; |
| 277 | |
| 278 | u64 xferred; /* bytes transferred */ |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 279 | int result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 280 | |
| 281 | rbd_obj_callback_t callback; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 282 | struct completion completion; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 283 | |
| 284 | struct kref kref; |
| 285 | }; |
| 286 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 287 | enum img_req_flags { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 288 | IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */ |
| 289 | IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */ |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 290 | IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */ |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 291 | IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 292 | }; |
| 293 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 294 | struct rbd_img_request { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 295 | struct rbd_device *rbd_dev; |
| 296 | u64 offset; /* starting image byte offset */ |
| 297 | u64 length; /* byte count from offset */ |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 298 | unsigned long flags; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 299 | union { |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 300 | u64 snap_id; /* for reads */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 301 | struct ceph_snap_context *snapc; /* for writes */ |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 302 | }; |
| 303 | union { |
| 304 | struct request *rq; /* block request */ |
| 305 | struct rbd_obj_request *obj_request; /* obj req initiator */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 306 | }; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 307 | struct page **copyup_pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 308 | u32 copyup_page_count; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 309 | spinlock_t completion_lock;/* protects next_completion */ |
| 310 | u32 next_completion; |
| 311 | rbd_img_callback_t callback; |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 312 | u64 xferred;/* aggregate bytes transferred */ |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 313 | int result; /* first nonzero obj_request result */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 314 | |
| 315 | u32 obj_request_count; |
| 316 | struct list_head obj_requests; /* rbd_obj_request structs */ |
| 317 | |
| 318 | struct kref kref; |
| 319 | }; |
| 320 | |
| 321 | #define for_each_obj_request(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 322 | list_for_each_entry(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 323 | #define for_each_obj_request_from(ireq, oreq) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 324 | list_for_each_entry_from(oreq, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 325 | #define for_each_obj_request_safe(ireq, oreq, n) \ |
Alex Elder | ef06f4d3 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 326 | list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 327 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 328 | enum rbd_watch_state { |
| 329 | RBD_WATCH_STATE_UNREGISTERED, |
| 330 | RBD_WATCH_STATE_REGISTERED, |
| 331 | RBD_WATCH_STATE_ERROR, |
| 332 | }; |
| 333 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 334 | enum rbd_lock_state { |
| 335 | RBD_LOCK_STATE_UNLOCKED, |
| 336 | RBD_LOCK_STATE_LOCKED, |
| 337 | RBD_LOCK_STATE_RELEASING, |
| 338 | }; |
| 339 | |
| 340 | /* WatchNotify::ClientId */ |
| 341 | struct rbd_client_id { |
| 342 | u64 gid; |
| 343 | u64 handle; |
| 344 | }; |
| 345 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 346 | struct rbd_mapping { |
Alex Elder | 99c1f08 | 2012-08-30 14:42:15 -0500 | [diff] [blame] | 347 | u64 size; |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 348 | u64 features; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 349 | bool read_only; |
| 350 | }; |
| 351 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 352 | /* |
| 353 | * a single device |
| 354 | */ |
| 355 | struct rbd_device { |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 356 | int dev_id; /* blkdev unique id */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 357 | |
| 358 | int major; /* blkdev assigned major */ |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 359 | int minor; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 360 | struct gendisk *disk; /* blkdev's gendisk and rq */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 361 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 362 | u32 image_format; /* Either 1 or 2 */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 363 | struct rbd_client *rbd_client; |
| 364 | |
| 365 | char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */ |
| 366 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 367 | spinlock_t lock; /* queue, flags, open_count */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 368 | |
| 369 | struct rbd_image_header header; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 370 | unsigned long flags; /* possibly lock protected */ |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 371 | struct rbd_spec *spec; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 372 | struct rbd_options *opts; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 373 | char *config_info; /* add{,_single_major} string */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 374 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 375 | struct ceph_object_id header_oid; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 376 | struct ceph_object_locator header_oloc; |
Alex Elder | 971f839 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 377 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 378 | struct ceph_file_layout layout; /* used for all rbd requests */ |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 379 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 380 | struct mutex watch_mutex; |
| 381 | enum rbd_watch_state watch_state; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 382 | struct ceph_osd_linger_request *watch_handle; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 383 | u64 watch_cookie; |
| 384 | struct delayed_work watch_dwork; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 385 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 386 | struct rw_semaphore lock_rwsem; |
| 387 | enum rbd_lock_state lock_state; |
| 388 | struct rbd_client_id owner_cid; |
| 389 | struct work_struct acquired_lock_work; |
| 390 | struct work_struct released_lock_work; |
| 391 | struct delayed_work lock_dwork; |
| 392 | struct work_struct unlock_work; |
| 393 | wait_queue_head_t lock_waitq; |
| 394 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 395 | struct workqueue_struct *task_wq; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 396 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 397 | struct rbd_spec *parent_spec; |
| 398 | u64 parent_overlap; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 399 | atomic_t parent_ref; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 400 | struct rbd_device *parent; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 401 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 402 | /* Block layer tags. */ |
| 403 | struct blk_mq_tag_set tag_set; |
| 404 | |
Josh Durgin | c666601 | 2011-11-21 17:11:12 -0800 | [diff] [blame] | 405 | /* protects updating the header */ |
| 406 | struct rw_semaphore header_rwsem; |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 407 | |
| 408 | struct rbd_mapping mapping; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 409 | |
| 410 | struct list_head node; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 411 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 412 | /* sysfs related */ |
| 413 | struct device dev; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 414 | unsigned long open_count; /* protected by lock */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 415 | }; |
| 416 | |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 417 | /* |
| 418 | * Flag bits for rbd_dev->flags. If atomicity is required, |
| 419 | * rbd_dev->lock is used to protect access. |
| 420 | * |
| 421 | * Currently, only the "removing" flag (which is coupled with the |
| 422 | * "open_count" field) requires atomic access. |
| 423 | */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 424 | enum rbd_dev_flags { |
| 425 | RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */ |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 426 | RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */ |
Alex Elder | 6d29290 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 427 | }; |
| 428 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 429 | static DEFINE_MUTEX(client_mutex); /* Serialize client creation */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 430 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 431 | static LIST_HEAD(rbd_dev_list); /* devices */ |
Alex Elder | e124a82f | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 432 | static DEFINE_SPINLOCK(rbd_dev_list_lock); |
| 433 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 434 | static LIST_HEAD(rbd_client_list); /* clients */ |
| 435 | static DEFINE_SPINLOCK(rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 436 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 437 | /* Slab caches for frequently-allocated structures */ |
| 438 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 439 | static struct kmem_cache *rbd_img_request_cache; |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 440 | static struct kmem_cache *rbd_obj_request_cache; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 441 | static struct kmem_cache *rbd_segment_name_cache; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 442 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 443 | static int rbd_major; |
Ilya Dryomov | f8a22fc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 444 | static DEFINE_IDA(rbd_dev_id_ida); |
| 445 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 446 | static struct workqueue_struct *rbd_wq; |
| 447 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 448 | /* |
| 449 | * Default to false for now, as single-major requires >= 0.75 version of |
| 450 | * userspace rbd utility. |
| 451 | */ |
| 452 | static bool single_major = false; |
| 453 | module_param(single_major, bool, S_IRUGO); |
| 454 | MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: false)"); |
| 455 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 456 | static int rbd_img_request_submit(struct rbd_img_request *img_request); |
| 457 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 458 | static ssize_t rbd_add(struct bus_type *bus, const char *buf, |
| 459 | size_t count); |
| 460 | static ssize_t rbd_remove(struct bus_type *bus, const char *buf, |
| 461 | size_t count); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 462 | static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf, |
| 463 | size_t count); |
| 464 | static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf, |
| 465 | size_t count); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 466 | static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 467 | static void rbd_spec_put(struct rbd_spec *spec); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 468 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 469 | static int rbd_dev_id_to_minor(int dev_id) |
| 470 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 471 | return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | static int minor_to_rbd_dev_id(int minor) |
| 475 | { |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 476 | return minor >> RBD_SINGLE_MAJOR_PART_SHIFT; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 479 | static bool rbd_is_lock_supported(struct rbd_device *rbd_dev) |
| 480 | { |
| 481 | return (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) && |
| 482 | rbd_dev->spec->snap_id == CEPH_NOSNAP && |
| 483 | !rbd_dev->mapping.read_only; |
| 484 | } |
| 485 | |
| 486 | static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 487 | { |
| 488 | return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED || |
| 489 | rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING; |
| 490 | } |
| 491 | |
| 492 | static bool rbd_is_lock_owner(struct rbd_device *rbd_dev) |
| 493 | { |
| 494 | bool is_lock_owner; |
| 495 | |
| 496 | down_read(&rbd_dev->lock_rwsem); |
| 497 | is_lock_owner = __rbd_is_lock_owner(rbd_dev); |
| 498 | up_read(&rbd_dev->lock_rwsem); |
| 499 | return is_lock_owner; |
| 500 | } |
| 501 | |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 502 | static BUS_ATTR(add, S_IWUSR, NULL, rbd_add); |
| 503 | static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 504 | static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major); |
| 505 | static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major); |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 506 | |
| 507 | static struct attribute *rbd_bus_attrs[] = { |
| 508 | &bus_attr_add.attr, |
| 509 | &bus_attr_remove.attr, |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 510 | &bus_attr_add_single_major.attr, |
| 511 | &bus_attr_remove_single_major.attr, |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 512 | NULL, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 513 | }; |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 514 | |
| 515 | static umode_t rbd_bus_is_visible(struct kobject *kobj, |
| 516 | struct attribute *attr, int index) |
| 517 | { |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 518 | if (!single_major && |
| 519 | (attr == &bus_attr_add_single_major.attr || |
| 520 | attr == &bus_attr_remove_single_major.attr)) |
| 521 | return 0; |
| 522 | |
Ilya Dryomov | 92c76dc | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 523 | return attr->mode; |
| 524 | } |
| 525 | |
| 526 | static const struct attribute_group rbd_bus_group = { |
| 527 | .attrs = rbd_bus_attrs, |
| 528 | .is_visible = rbd_bus_is_visible, |
| 529 | }; |
| 530 | __ATTRIBUTE_GROUPS(rbd_bus); |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 531 | |
| 532 | static struct bus_type rbd_bus_type = { |
| 533 | .name = "rbd", |
Greg Kroah-Hartman | b15a21d | 2013-08-23 14:24:28 -0700 | [diff] [blame] | 534 | .bus_groups = rbd_bus_groups, |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 535 | }; |
| 536 | |
| 537 | static void rbd_root_dev_release(struct device *dev) |
| 538 | { |
| 539 | } |
| 540 | |
| 541 | static struct device rbd_root_dev = { |
| 542 | .init_name = "rbd", |
| 543 | .release = rbd_root_dev_release, |
| 544 | }; |
| 545 | |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 546 | static __printf(2, 3) |
| 547 | void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...) |
| 548 | { |
| 549 | struct va_format vaf; |
| 550 | va_list args; |
| 551 | |
| 552 | va_start(args, fmt); |
| 553 | vaf.fmt = fmt; |
| 554 | vaf.va = &args; |
| 555 | |
| 556 | if (!rbd_dev) |
| 557 | printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf); |
| 558 | else if (rbd_dev->disk) |
| 559 | printk(KERN_WARNING "%s: %s: %pV\n", |
| 560 | RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf); |
| 561 | else if (rbd_dev->spec && rbd_dev->spec->image_name) |
| 562 | printk(KERN_WARNING "%s: image %s: %pV\n", |
| 563 | RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf); |
| 564 | else if (rbd_dev->spec && rbd_dev->spec->image_id) |
| 565 | printk(KERN_WARNING "%s: id %s: %pV\n", |
| 566 | RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf); |
| 567 | else /* punt */ |
| 568 | printk(KERN_WARNING "%s: rbd_dev %p: %pV\n", |
| 569 | RBD_DRV_NAME, rbd_dev, &vaf); |
| 570 | va_end(args); |
| 571 | } |
| 572 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 573 | #ifdef RBD_DEBUG |
| 574 | #define rbd_assert(expr) \ |
| 575 | if (unlikely(!(expr))) { \ |
| 576 | printk(KERN_ERR "\nAssertion failure in %s() " \ |
| 577 | "at line %d:\n\n" \ |
| 578 | "\trbd_assert(%s);\n\n", \ |
| 579 | __func__, __LINE__, #expr); \ |
| 580 | BUG(); \ |
| 581 | } |
| 582 | #else /* !RBD_DEBUG */ |
| 583 | # define rbd_assert(expr) ((void) 0) |
| 584 | #endif /* !RBD_DEBUG */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 585 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 586 | static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request); |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 587 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 588 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request); |
| 589 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 590 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 591 | static int rbd_dev_refresh(struct rbd_device *rbd_dev); |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 592 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev); |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 593 | static int rbd_dev_header_info(struct rbd_device *rbd_dev); |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 594 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 595 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 596 | u64 snap_id); |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 597 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 598 | u8 *order, u64 *snap_size); |
| 599 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 600 | u64 *snap_features); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 601 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 602 | static int rbd_open(struct block_device *bdev, fmode_t mode) |
| 603 | { |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 604 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 605 | bool removing = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 606 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 607 | if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 608 | return -EROFS; |
| 609 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 610 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 611 | if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) |
| 612 | removing = true; |
| 613 | else |
| 614 | rbd_dev->open_count++; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 615 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 616 | if (removing) |
| 617 | return -ENOENT; |
| 618 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 619 | (void) get_device(&rbd_dev->dev); |
Alex Elder | 340c7a2 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 620 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 621 | return 0; |
| 622 | } |
| 623 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 624 | static void rbd_release(struct gendisk *disk, fmode_t mode) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 625 | { |
| 626 | struct rbd_device *rbd_dev = disk->private_data; |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 627 | unsigned long open_count_before; |
| 628 | |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 629 | spin_lock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 630 | open_count_before = rbd_dev->open_count--; |
Alex Elder | a14ea26 | 2013-02-05 13:23:12 -0600 | [diff] [blame] | 631 | spin_unlock_irq(&rbd_dev->lock); |
Alex Elder | b82d167 | 2013-01-14 12:43:31 -0600 | [diff] [blame] | 632 | rbd_assert(open_count_before > 0); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 633 | |
Alex Elder | c3e946c | 2012-11-16 09:29:16 -0600 | [diff] [blame] | 634 | put_device(&rbd_dev->dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 637 | static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg) |
| 638 | { |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 639 | int ret = 0; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 640 | int val; |
| 641 | bool ro; |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 642 | bool ro_changed = false; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 643 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 644 | /* get_user() may sleep, so call it before taking rbd_dev->lock */ |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 645 | if (get_user(val, (int __user *)(arg))) |
| 646 | return -EFAULT; |
| 647 | |
| 648 | ro = val ? true : false; |
| 649 | /* Snapshot doesn't allow to write*/ |
| 650 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro) |
| 651 | return -EROFS; |
| 652 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 653 | spin_lock_irq(&rbd_dev->lock); |
| 654 | /* prevent others open this device */ |
| 655 | if (rbd_dev->open_count > 1) { |
| 656 | ret = -EBUSY; |
| 657 | goto out; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 658 | } |
| 659 | |
Josh Durgin | 77f33c0 | 2013-09-30 17:09:54 -0700 | [diff] [blame] | 660 | if (rbd_dev->mapping.read_only != ro) { |
| 661 | rbd_dev->mapping.read_only = ro; |
| 662 | ro_changed = true; |
| 663 | } |
| 664 | |
| 665 | out: |
| 666 | spin_unlock_irq(&rbd_dev->lock); |
| 667 | /* set_disk_ro() may sleep, so call it after releasing rbd_dev->lock */ |
| 668 | if (ret == 0 && ro_changed) |
| 669 | set_disk_ro(rbd_dev->disk, ro ? 1 : 0); |
| 670 | |
| 671 | return ret; |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static int rbd_ioctl(struct block_device *bdev, fmode_t mode, |
| 675 | unsigned int cmd, unsigned long arg) |
| 676 | { |
| 677 | struct rbd_device *rbd_dev = bdev->bd_disk->private_data; |
| 678 | int ret = 0; |
| 679 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 680 | switch (cmd) { |
| 681 | case BLKROSET: |
| 682 | ret = rbd_ioctl_set_ro(rbd_dev, arg); |
| 683 | break; |
| 684 | default: |
| 685 | ret = -ENOTTY; |
| 686 | } |
| 687 | |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 688 | return ret; |
| 689 | } |
| 690 | |
| 691 | #ifdef CONFIG_COMPAT |
| 692 | static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 693 | unsigned int cmd, unsigned long arg) |
| 694 | { |
| 695 | return rbd_ioctl(bdev, mode, cmd, arg); |
| 696 | } |
| 697 | #endif /* CONFIG_COMPAT */ |
| 698 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 699 | static const struct block_device_operations rbd_bd_ops = { |
| 700 | .owner = THIS_MODULE, |
| 701 | .open = rbd_open, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 702 | .release = rbd_release, |
Guangliang Zhao | 131fd9f | 2013-09-24 11:25:36 +0800 | [diff] [blame] | 703 | .ioctl = rbd_ioctl, |
| 704 | #ifdef CONFIG_COMPAT |
| 705 | .compat_ioctl = rbd_compat_ioctl, |
| 706 | #endif |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 707 | }; |
| 708 | |
| 709 | /* |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 710 | * Initialize an rbd client instance. Success or not, this function |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 711 | * consumes ceph_opts. Caller holds client_mutex. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 712 | */ |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 713 | static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 714 | { |
| 715 | struct rbd_client *rbdc; |
| 716 | int ret = -ENOMEM; |
| 717 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 718 | dout("%s:\n", __func__); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 719 | rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL); |
| 720 | if (!rbdc) |
| 721 | goto out_opt; |
| 722 | |
| 723 | kref_init(&rbdc->kref); |
| 724 | INIT_LIST_HEAD(&rbdc->node); |
| 725 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 726 | rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 727 | if (IS_ERR(rbdc->client)) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 728 | goto out_rbdc; |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 729 | ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 730 | |
| 731 | ret = ceph_open_session(rbdc->client); |
| 732 | if (ret < 0) |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 733 | goto out_client; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 734 | |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 735 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 736 | list_add_tail(&rbdc->node, &rbd_client_list); |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 737 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 738 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 739 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | bc534d8 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 740 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 741 | return rbdc; |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 742 | out_client: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 743 | ceph_destroy_client(rbdc->client); |
Alex Elder | 08f7546 | 2013-05-29 11:19:00 -0500 | [diff] [blame] | 744 | out_rbdc: |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 745 | kfree(rbdc); |
| 746 | out_opt: |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 747 | if (ceph_opts) |
| 748 | ceph_destroy_options(ceph_opts); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 749 | dout("%s: error %d\n", __func__, ret); |
| 750 | |
Vasiliy Kulikov | 28f259b | 2010-09-26 12:59:37 +0400 | [diff] [blame] | 751 | return ERR_PTR(ret); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 754 | static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc) |
| 755 | { |
| 756 | kref_get(&rbdc->kref); |
| 757 | |
| 758 | return rbdc; |
| 759 | } |
| 760 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 761 | /* |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 762 | * Find a ceph client with specific addr and configuration. If |
| 763 | * found, bump its reference count. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 764 | */ |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 765 | static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 766 | { |
| 767 | struct rbd_client *client_node; |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 768 | bool found = false; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 769 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 770 | if (ceph_opts->flags & CEPH_OPT_NOSHARE) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 771 | return NULL; |
| 772 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 773 | spin_lock(&rbd_client_list_lock); |
| 774 | list_for_each_entry(client_node, &rbd_client_list, node) { |
| 775 | if (!ceph_compare_options(ceph_opts, client_node->client)) { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 776 | __rbd_get_client(client_node); |
| 777 | |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 778 | found = true; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | spin_unlock(&rbd_client_list_lock); |
| 783 | |
| 784 | return found ? client_node : NULL; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 788 | * (Per device) rbd map options |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 789 | */ |
| 790 | enum { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 791 | Opt_queue_depth, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 792 | Opt_last_int, |
| 793 | /* int args above */ |
| 794 | Opt_last_string, |
| 795 | /* string args above */ |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 796 | Opt_read_only, |
| 797 | Opt_read_write, |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 798 | Opt_lock_on_read, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 799 | Opt_err |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 802 | static match_table_t rbd_opts_tokens = { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 803 | {Opt_queue_depth, "queue_depth=%d"}, |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 804 | /* int args above */ |
| 805 | /* string args above */ |
Alex Elder | be466c1 | 2012-10-22 11:31:26 -0500 | [diff] [blame] | 806 | {Opt_read_only, "read_only"}, |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 807 | {Opt_read_only, "ro"}, /* Alternate spelling */ |
| 808 | {Opt_read_write, "read_write"}, |
| 809 | {Opt_read_write, "rw"}, /* Alternate spelling */ |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 810 | {Opt_lock_on_read, "lock_on_read"}, |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 811 | {Opt_err, NULL} |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 812 | }; |
| 813 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 814 | struct rbd_options { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 815 | int queue_depth; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 816 | bool read_only; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 817 | bool lock_on_read; |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 818 | }; |
| 819 | |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 820 | #define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 821 | #define RBD_READ_ONLY_DEFAULT false |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 822 | #define RBD_LOCK_ON_READ_DEFAULT false |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 823 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 824 | static int parse_rbd_opts_token(char *c, void *private) |
| 825 | { |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 826 | struct rbd_options *rbd_opts = private; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 827 | substring_t argstr[MAX_OPT_ARGS]; |
| 828 | int token, intval, ret; |
| 829 | |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 830 | token = match_token(c, rbd_opts_tokens, argstr); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 831 | if (token < Opt_last_int) { |
| 832 | ret = match_int(&argstr[0], &intval); |
| 833 | if (ret < 0) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 834 | pr_err("bad mount option arg (not int) at '%s'\n", c); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 835 | return ret; |
| 836 | } |
| 837 | dout("got int token %d val %d\n", token, intval); |
| 838 | } else if (token > Opt_last_int && token < Opt_last_string) { |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 839 | dout("got string token %d val %s\n", token, argstr[0].from); |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 840 | } else { |
| 841 | dout("got token %d\n", token); |
| 842 | } |
| 843 | |
| 844 | switch (token) { |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 845 | case Opt_queue_depth: |
| 846 | if (intval < 1) { |
| 847 | pr_err("queue_depth out of range\n"); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | rbd_opts->queue_depth = intval; |
| 851 | break; |
Alex Elder | cc0538b | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 852 | case Opt_read_only: |
| 853 | rbd_opts->read_only = true; |
| 854 | break; |
| 855 | case Opt_read_write: |
| 856 | rbd_opts->read_only = false; |
| 857 | break; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 858 | case Opt_lock_on_read: |
| 859 | rbd_opts->lock_on_read = true; |
| 860 | break; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 861 | default: |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 862 | /* libceph prints "bad option" msg */ |
| 863 | return -EINVAL; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 864 | } |
Ilya Dryomov | 210c104 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 865 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 866 | return 0; |
| 867 | } |
| 868 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 869 | static char* obj_op_name(enum obj_operation_type op_type) |
| 870 | { |
| 871 | switch (op_type) { |
| 872 | case OBJ_OP_READ: |
| 873 | return "read"; |
| 874 | case OBJ_OP_WRITE: |
| 875 | return "write"; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 876 | case OBJ_OP_DISCARD: |
| 877 | return "discard"; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 878 | default: |
| 879 | return "???"; |
| 880 | } |
| 881 | } |
| 882 | |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 883 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 884 | * Get a ceph client with specific addr and configuration, if one does |
Alex Elder | 7262cfc | 2013-05-16 15:04:20 -0500 | [diff] [blame] | 885 | * not exist create it. Either way, ceph_opts is consumed by this |
| 886 | * function. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 887 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 888 | static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 889 | { |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 890 | struct rbd_client *rbdc; |
Yehuda Sadeh | 59c2be1 | 2011-03-21 15:10:11 -0700 | [diff] [blame] | 891 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 892 | mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING); |
Alex Elder | 1f7ba33 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 893 | rbdc = rbd_client_find(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 894 | if (rbdc) /* using an existing client */ |
Alex Elder | 43ae470 | 2012-07-03 16:01:18 -0500 | [diff] [blame] | 895 | ceph_destroy_options(ceph_opts); |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 896 | else |
Alex Elder | f8c3892 | 2012-08-10 13:12:07 -0700 | [diff] [blame] | 897 | rbdc = rbd_client_create(ceph_opts); |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 898 | mutex_unlock(&client_mutex); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 899 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 900 | return rbdc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* |
| 904 | * Destroy ceph client |
Alex Elder | d23a4b3 | 2012-01-29 13:57:43 -0600 | [diff] [blame] | 905 | * |
Alex Elder | 432b858 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 906 | * Caller must hold rbd_client_list_lock. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 907 | */ |
| 908 | static void rbd_client_release(struct kref *kref) |
| 909 | { |
| 910 | struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref); |
| 911 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 912 | dout("%s: rbdc %p\n", __func__, rbdc); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 913 | spin_lock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 914 | list_del(&rbdc->node); |
Alex Elder | cd9d9f5 | 2012-04-04 13:35:44 -0500 | [diff] [blame] | 915 | spin_unlock(&rbd_client_list_lock); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 916 | |
| 917 | ceph_destroy_client(rbdc->client); |
| 918 | kfree(rbdc); |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * Drop reference to ceph client node. If it's not referenced anymore, release |
| 923 | * it. |
| 924 | */ |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 925 | static void rbd_put_client(struct rbd_client *rbdc) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 926 | { |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 927 | if (rbdc) |
| 928 | kref_put(&rbdc->kref, rbd_client_release); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 929 | } |
| 930 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 931 | static bool rbd_image_format_valid(u32 image_format) |
| 932 | { |
| 933 | return image_format == 1 || image_format == 2; |
| 934 | } |
| 935 | |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 936 | static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk) |
| 937 | { |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 938 | size_t size; |
| 939 | u32 snap_count; |
| 940 | |
| 941 | /* The header has to start with the magic rbd header text */ |
| 942 | if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT))) |
| 943 | return false; |
| 944 | |
Alex Elder | db2388b | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 945 | /* The bio layer requires at least sector-sized I/O */ |
| 946 | |
| 947 | if (ondisk->options.order < SECTOR_SHIFT) |
| 948 | return false; |
| 949 | |
| 950 | /* If we use u64 in a few spots we may be able to loosen this */ |
| 951 | |
| 952 | if (ondisk->options.order > 8 * sizeof (int) - 1) |
| 953 | return false; |
| 954 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 955 | /* |
| 956 | * The size of a snapshot header has to fit in a size_t, and |
| 957 | * that limits the number of snapshots. |
| 958 | */ |
| 959 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 960 | size = SIZE_MAX - sizeof (struct ceph_snap_context); |
| 961 | if (snap_count > size / sizeof (__le64)) |
| 962 | return false; |
| 963 | |
| 964 | /* |
| 965 | * Not only that, but the size of the entire the snapshot |
| 966 | * header must also be representable in a size_t. |
| 967 | */ |
| 968 | size -= snap_count * sizeof (__le64); |
| 969 | if ((u64) size < le64_to_cpu(ondisk->snap_names_len)) |
| 970 | return false; |
| 971 | |
| 972 | return true; |
Alex Elder | 8e94af8 | 2012-07-25 09:32:40 -0500 | [diff] [blame] | 973 | } |
| 974 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 975 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 976 | * Fill an rbd image header with information from the given format 1 |
| 977 | * on-disk header. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 978 | */ |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 979 | static int rbd_header_from_disk(struct rbd_device *rbd_dev, |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 980 | struct rbd_image_header_ondisk *ondisk) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 981 | { |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 982 | struct rbd_image_header *header = &rbd_dev->header; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 983 | bool first_time = header->object_prefix == NULL; |
| 984 | struct ceph_snap_context *snapc; |
| 985 | char *object_prefix = NULL; |
| 986 | char *snap_names = NULL; |
| 987 | u64 *snap_sizes = NULL; |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 988 | u32 snap_count; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 989 | int ret = -ENOMEM; |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 990 | u32 i; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 991 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 992 | /* Allocate this now to avoid having to handle failure below */ |
| 993 | |
| 994 | if (first_time) { |
| 995 | size_t len; |
| 996 | |
| 997 | len = strnlen(ondisk->object_prefix, |
| 998 | sizeof (ondisk->object_prefix)); |
| 999 | object_prefix = kmalloc(len + 1, GFP_KERNEL); |
| 1000 | if (!object_prefix) |
| 1001 | return -ENOMEM; |
| 1002 | memcpy(object_prefix, ondisk->object_prefix, len); |
| 1003 | object_prefix[len] = '\0'; |
| 1004 | } |
| 1005 | |
| 1006 | /* Allocate the snapshot context and fill it in */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1007 | |
Alex Elder | 103a150 | 2012-08-02 11:29:45 -0500 | [diff] [blame] | 1008 | snap_count = le32_to_cpu(ondisk->snap_count); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1009 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
| 1010 | if (!snapc) |
| 1011 | goto out_err; |
| 1012 | snapc->seq = le64_to_cpu(ondisk->snap_seq); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1013 | if (snap_count) { |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1014 | struct rbd_image_snap_ondisk *snaps; |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1015 | u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len); |
| 1016 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1017 | /* We'll keep a copy of the snapshot names... */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1018 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1019 | if (snap_names_len > (u64)SIZE_MAX) |
| 1020 | goto out_2big; |
| 1021 | snap_names = kmalloc(snap_names_len, GFP_KERNEL); |
| 1022 | if (!snap_names) |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1023 | goto out_err; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1024 | |
| 1025 | /* ...as well as the array of their sizes. */ |
Markus Elfring | 88a25a5 | 2016-09-11 12:21:25 +0200 | [diff] [blame] | 1026 | snap_sizes = kmalloc_array(snap_count, |
| 1027 | sizeof(*header->snap_sizes), |
| 1028 | GFP_KERNEL); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1029 | if (!snap_sizes) |
| 1030 | goto out_err; |
| 1031 | |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1032 | /* |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1033 | * Copy the names, and fill in each snapshot's id |
| 1034 | * and size. |
| 1035 | * |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 1036 | * Note that rbd_dev_v1_header_info() guarantees the |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1037 | * ondisk buffer we're working with has |
Alex Elder | f785cc1 | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1038 | * snap_names_len bytes beyond the end of the |
| 1039 | * snapshot id array, this memcpy() is safe. |
| 1040 | */ |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1041 | memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len); |
| 1042 | snaps = ondisk->snaps; |
| 1043 | for (i = 0; i < snap_count; i++) { |
| 1044 | snapc->snaps[i] = le64_to_cpu(snaps[i].id); |
| 1045 | snap_sizes[i] = le64_to_cpu(snaps[i].image_size); |
| 1046 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1047 | } |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 1048 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1049 | /* We won't fail any more, fill in the header */ |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1050 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1051 | if (first_time) { |
| 1052 | header->object_prefix = object_prefix; |
| 1053 | header->obj_order = ondisk->options.order; |
| 1054 | header->crypt_type = ondisk->options.crypt_type; |
| 1055 | header->comp_type = ondisk->options.comp_type; |
| 1056 | /* The rest aren't used for format 1 images */ |
| 1057 | header->stripe_unit = 0; |
| 1058 | header->stripe_count = 0; |
| 1059 | header->features = 0; |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1060 | } else { |
| 1061 | ceph_put_snap_context(header->snapc); |
| 1062 | kfree(header->snap_names); |
| 1063 | kfree(header->snap_sizes); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /* The remaining fields always get updated (when we refresh) */ |
Alex Elder | 621901d | 2012-08-23 23:22:06 -0500 | [diff] [blame] | 1067 | |
Alex Elder | f84344f | 2012-08-31 17:29:51 -0500 | [diff] [blame] | 1068 | header->image_size = le64_to_cpu(ondisk->image_size); |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1069 | header->snapc = snapc; |
| 1070 | header->snap_names = snap_names; |
| 1071 | header->snap_sizes = snap_sizes; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 1072 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1073 | return 0; |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1074 | out_2big: |
| 1075 | ret = -EIO; |
Alex Elder | 6a52325 | 2012-07-19 17:12:59 -0500 | [diff] [blame] | 1076 | out_err: |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1077 | kfree(snap_sizes); |
| 1078 | kfree(snap_names); |
| 1079 | ceph_put_snap_context(snapc); |
| 1080 | kfree(object_prefix); |
Alex Elder | ccece23 | 2012-07-10 20:30:10 -0500 | [diff] [blame] | 1081 | |
Alex Elder | bb23e37 | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 1082 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1085 | static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which) |
| 1086 | { |
| 1087 | const char *snap_name; |
| 1088 | |
| 1089 | rbd_assert(which < rbd_dev->header.snapc->num_snaps); |
| 1090 | |
| 1091 | /* Skip over names until we find the one we are looking for */ |
| 1092 | |
| 1093 | snap_name = rbd_dev->header.snap_names; |
| 1094 | while (which--) |
| 1095 | snap_name += strlen(snap_name) + 1; |
| 1096 | |
| 1097 | return kstrdup(snap_name, GFP_KERNEL); |
| 1098 | } |
| 1099 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1100 | /* |
| 1101 | * Snapshot id comparison function for use with qsort()/bsearch(). |
| 1102 | * Note that result is for snapshots in *descending* order. |
| 1103 | */ |
| 1104 | static int snapid_compare_reverse(const void *s1, const void *s2) |
| 1105 | { |
| 1106 | u64 snap_id1 = *(u64 *)s1; |
| 1107 | u64 snap_id2 = *(u64 *)s2; |
| 1108 | |
| 1109 | if (snap_id1 < snap_id2) |
| 1110 | return 1; |
| 1111 | return snap_id1 == snap_id2 ? 0 : -1; |
| 1112 | } |
| 1113 | |
| 1114 | /* |
| 1115 | * Search a snapshot context to see if the given snapshot id is |
| 1116 | * present. |
| 1117 | * |
| 1118 | * Returns the position of the snapshot id in the array if it's found, |
| 1119 | * or BAD_SNAP_INDEX otherwise. |
| 1120 | * |
| 1121 | * Note: The snapshot array is in kept sorted (by the osd) in |
| 1122 | * reverse order, highest snapshot id first. |
| 1123 | */ |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1124 | static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id) |
| 1125 | { |
| 1126 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1127 | u64 *found; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1128 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1129 | found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps, |
| 1130 | sizeof (snap_id), snapid_compare_reverse); |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1131 | |
Alex Elder | 30d1cff | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 1132 | return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX; |
Alex Elder | 9682fc6 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1133 | } |
| 1134 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1135 | static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, |
| 1136 | u64 snap_id) |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1137 | { |
| 1138 | u32 which; |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1139 | const char *snap_name; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1140 | |
| 1141 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1142 | if (which == BAD_SNAP_INDEX) |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1143 | return ERR_PTR(-ENOENT); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1144 | |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 1145 | snap_name = _rbd_dev_v1_snap_name(rbd_dev, which); |
| 1146 | return snap_name ? snap_name : ERR_PTR(-ENOMEM); |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1147 | } |
| 1148 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1149 | static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id) |
| 1150 | { |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1151 | if (snap_id == CEPH_NOSNAP) |
| 1152 | return RBD_SNAP_HEAD_NAME; |
| 1153 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1154 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1155 | if (rbd_dev->image_format == 1) |
| 1156 | return rbd_dev_v1_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1157 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1158 | return rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 1159 | } |
| 1160 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1161 | static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 1162 | u64 *snap_size) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1163 | { |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1164 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1165 | if (snap_id == CEPH_NOSNAP) { |
| 1166 | *snap_size = rbd_dev->header.image_size; |
| 1167 | } else if (rbd_dev->image_format == 1) { |
| 1168 | u32 which; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1169 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1170 | which = rbd_dev_snap_index(rbd_dev, snap_id); |
| 1171 | if (which == BAD_SNAP_INDEX) |
| 1172 | return -ENOENT; |
Alex Elder | 00f1f36 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 1173 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1174 | *snap_size = rbd_dev->header.snap_sizes[which]; |
| 1175 | } else { |
| 1176 | u64 size = 0; |
| 1177 | int ret; |
| 1178 | |
| 1179 | ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size); |
| 1180 | if (ret) |
| 1181 | return ret; |
| 1182 | |
| 1183 | *snap_size = size; |
| 1184 | } |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
| 1188 | static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 1189 | u64 *snap_features) |
| 1190 | { |
| 1191 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 1192 | if (snap_id == CEPH_NOSNAP) { |
| 1193 | *snap_features = rbd_dev->header.features; |
| 1194 | } else if (rbd_dev->image_format == 1) { |
| 1195 | *snap_features = 0; /* No features for format 1 */ |
| 1196 | } else { |
| 1197 | u64 features = 0; |
| 1198 | int ret; |
| 1199 | |
| 1200 | ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features); |
| 1201 | if (ret) |
| 1202 | return ret; |
| 1203 | |
| 1204 | *snap_features = features; |
| 1205 | } |
| 1206 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1209 | static int rbd_dev_mapping_set(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1210 | { |
Alex Elder | 8f4b7d9 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 1211 | u64 snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1212 | u64 size = 0; |
| 1213 | u64 features = 0; |
| 1214 | int ret; |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1215 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 1216 | ret = rbd_snap_size(rbd_dev, snap_id, &size); |
| 1217 | if (ret) |
| 1218 | return ret; |
| 1219 | ret = rbd_snap_features(rbd_dev, snap_id, &features); |
| 1220 | if (ret) |
| 1221 | return ret; |
| 1222 | |
| 1223 | rbd_dev->mapping.size = size; |
| 1224 | rbd_dev->mapping.features = features; |
| 1225 | |
Alex Elder | 8b0241f | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 1226 | return 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Alex Elder | d1cf578 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 1229 | static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev) |
| 1230 | { |
| 1231 | rbd_dev->mapping.size = 0; |
| 1232 | rbd_dev->mapping.features = 0; |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 1233 | } |
| 1234 | |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1235 | static void rbd_segment_name_free(const char *name) |
| 1236 | { |
| 1237 | /* The explicit cast here is needed to drop the const qualifier */ |
| 1238 | |
| 1239 | kmem_cache_free(rbd_segment_name_cache, (void *)name); |
| 1240 | } |
| 1241 | |
Alex Elder | 98571b5 | 2013-01-20 14:44:42 -0600 | [diff] [blame] | 1242 | static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1243 | { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1244 | char *name; |
| 1245 | u64 segment; |
| 1246 | int ret; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1247 | char *name_format; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1248 | |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 1249 | name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1250 | if (!name) |
| 1251 | return NULL; |
| 1252 | segment = offset >> rbd_dev->header.obj_order; |
Josh Durgin | 3a96d5c | 2013-06-12 19:15:06 -0700 | [diff] [blame] | 1253 | name_format = "%s.%012llx"; |
| 1254 | if (rbd_dev->image_format == 2) |
| 1255 | name_format = "%s.%016llx"; |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1256 | ret = snprintf(name, CEPH_MAX_OID_NAME_LEN + 1, name_format, |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1257 | rbd_dev->header.object_prefix, segment); |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 1258 | if (ret < 0 || ret > CEPH_MAX_OID_NAME_LEN) { |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1259 | pr_err("error formatting segment name for #%llu (%d)\n", |
| 1260 | segment, ret); |
Himangi Saraogi | 7d5079a | 2014-07-24 03:17:07 +0530 | [diff] [blame] | 1261 | rbd_segment_name_free(name); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1262 | name = NULL; |
| 1263 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1264 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1265 | return name; |
| 1266 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1267 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1268 | static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset) |
| 1269 | { |
| 1270 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1271 | |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1272 | return offset & (segment_size - 1); |
| 1273 | } |
| 1274 | |
| 1275 | static u64 rbd_segment_length(struct rbd_device *rbd_dev, |
| 1276 | u64 offset, u64 length) |
| 1277 | { |
| 1278 | u64 segment_size = (u64) 1 << rbd_dev->header.obj_order; |
| 1279 | |
| 1280 | offset &= segment_size - 1; |
| 1281 | |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 1282 | rbd_assert(length <= U64_MAX - offset); |
Alex Elder | 65ccfe2 | 2012-08-09 10:33:26 -0700 | [diff] [blame] | 1283 | if (offset + length > segment_size) |
| 1284 | length = segment_size - offset; |
| 1285 | |
| 1286 | return length; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /* |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 1290 | * returns the size of an object in the image |
| 1291 | */ |
| 1292 | static u64 rbd_obj_bytes(struct rbd_image_header *header) |
| 1293 | { |
| 1294 | return 1 << header->obj_order; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1298 | * bio helpers |
| 1299 | */ |
| 1300 | |
| 1301 | static void bio_chain_put(struct bio *chain) |
| 1302 | { |
| 1303 | struct bio *tmp; |
| 1304 | |
| 1305 | while (chain) { |
| 1306 | tmp = chain; |
| 1307 | chain = chain->bi_next; |
| 1308 | bio_put(tmp); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * zeros a bio chain, starting at specific offset |
| 1314 | */ |
| 1315 | static void zero_bio_chain(struct bio *chain, int start_ofs) |
| 1316 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1317 | struct bio_vec bv; |
| 1318 | struct bvec_iter iter; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1319 | unsigned long flags; |
| 1320 | void *buf; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1321 | int pos = 0; |
| 1322 | |
| 1323 | while (chain) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1324 | bio_for_each_segment(bv, chain, iter) { |
| 1325 | if (pos + bv.bv_len > start_ofs) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1326 | int remainder = max(start_ofs - pos, 0); |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1327 | buf = bvec_kmap_irq(&bv, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1328 | memset(buf + remainder, 0, |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1329 | bv.bv_len - remainder); |
| 1330 | flush_dcache_page(bv.bv_page); |
Dan Carpenter | 85b5aaa | 2010-10-11 21:15:11 +0200 | [diff] [blame] | 1331 | bvec_kunmap_irq(buf, &flags); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1332 | } |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1333 | pos += bv.bv_len; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | chain = chain->bi_next; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | /* |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1341 | * similar to zero_bio_chain(), zeros data defined by a page array, |
| 1342 | * starting at the given byte offset from the start of the array and |
| 1343 | * continuing up to the given end offset. The pages array is |
| 1344 | * assumed to be big enough to hold all bytes up to the end. |
| 1345 | */ |
| 1346 | static void zero_pages(struct page **pages, u64 offset, u64 end) |
| 1347 | { |
| 1348 | struct page **page = &pages[offset >> PAGE_SHIFT]; |
| 1349 | |
| 1350 | rbd_assert(end > offset); |
| 1351 | rbd_assert(end - offset <= (u64)SIZE_MAX); |
| 1352 | while (offset < end) { |
| 1353 | size_t page_offset; |
| 1354 | size_t length; |
| 1355 | unsigned long flags; |
| 1356 | void *kaddr; |
| 1357 | |
Geert Uytterhoeven | 491205a | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 1358 | page_offset = offset & ~PAGE_MASK; |
| 1359 | length = min_t(size_t, PAGE_SIZE - page_offset, end - offset); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1360 | local_irq_save(flags); |
| 1361 | kaddr = kmap_atomic(*page); |
| 1362 | memset(kaddr + page_offset, 0, length); |
Alex Elder | e215605 | 2013-05-22 20:54:25 -0500 | [diff] [blame] | 1363 | flush_dcache_page(*page); |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1364 | kunmap_atomic(kaddr); |
| 1365 | local_irq_restore(flags); |
| 1366 | |
| 1367 | offset += length; |
| 1368 | page++; |
| 1369 | } |
| 1370 | } |
| 1371 | |
| 1372 | /* |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1373 | * Clone a portion of a bio, starting at the given byte offset |
| 1374 | * and continuing for the number of bytes indicated. |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1375 | */ |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1376 | static struct bio *bio_clone_range(struct bio *bio_src, |
| 1377 | unsigned int offset, |
| 1378 | unsigned int len, |
| 1379 | gfp_t gfpmask) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1380 | { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1381 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1382 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1383 | bio = bio_clone(bio_src, gfpmask); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1384 | if (!bio) |
| 1385 | return NULL; /* ENOMEM */ |
| 1386 | |
Kent Overstreet | 5341a627 | 2013-08-07 14:31:11 -0700 | [diff] [blame] | 1387 | bio_advance(bio, offset); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1388 | bio->bi_iter.bi_size = len; |
Alex Elder | 542582f | 2012-08-09 10:33:25 -0700 | [diff] [blame] | 1389 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1390 | return bio; |
| 1391 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1392 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1393 | /* |
| 1394 | * Clone a portion of a bio chain, starting at the given byte offset |
| 1395 | * into the first bio in the source chain and continuing for the |
| 1396 | * number of bytes indicated. The result is another bio chain of |
| 1397 | * exactly the given length, or a null pointer on error. |
| 1398 | * |
| 1399 | * The bio_src and offset parameters are both in-out. On entry they |
| 1400 | * refer to the first source bio and the offset into that bio where |
| 1401 | * the start of data to be cloned is located. |
| 1402 | * |
| 1403 | * On return, bio_src is updated to refer to the bio in the source |
| 1404 | * chain that contains first un-cloned byte, and *offset will |
| 1405 | * contain the offset of that byte within that bio. |
| 1406 | */ |
| 1407 | static struct bio *bio_chain_clone_range(struct bio **bio_src, |
| 1408 | unsigned int *offset, |
| 1409 | unsigned int len, |
| 1410 | gfp_t gfpmask) |
| 1411 | { |
| 1412 | struct bio *bi = *bio_src; |
| 1413 | unsigned int off = *offset; |
| 1414 | struct bio *chain = NULL; |
| 1415 | struct bio **end; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1416 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1417 | /* Build up a chain of clone bios up to the limit */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1418 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1419 | if (!bi || off >= bi->bi_iter.bi_size || !len) |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1420 | return NULL; /* Nothing to clone */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1421 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1422 | end = &chain; |
| 1423 | while (len) { |
| 1424 | unsigned int bi_size; |
| 1425 | struct bio *bio; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1426 | |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1427 | if (!bi) { |
| 1428 | rbd_warn(NULL, "bio_chain exhausted with %u left", len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1429 | goto out_err; /* EINVAL; ran out of bio's */ |
Alex Elder | f5400b7 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 1430 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1431 | bi_size = min_t(unsigned int, bi->bi_iter.bi_size - off, len); |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1432 | bio = bio_clone_range(bi, off, bi_size, gfpmask); |
| 1433 | if (!bio) |
| 1434 | goto out_err; /* ENOMEM */ |
| 1435 | |
| 1436 | *end = bio; |
| 1437 | end = &bio->bi_next; |
| 1438 | |
| 1439 | off += bi_size; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1440 | if (off == bi->bi_iter.bi_size) { |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1441 | bi = bi->bi_next; |
| 1442 | off = 0; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1443 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1444 | len -= bi_size; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1445 | } |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1446 | *bio_src = bi; |
| 1447 | *offset = off; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1448 | |
Alex Elder | f7760da | 2012-10-20 22:17:27 -0500 | [diff] [blame] | 1449 | return chain; |
| 1450 | out_err: |
| 1451 | bio_chain_put(chain); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1452 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1456 | /* |
| 1457 | * The default/initial value for all object request flags is 0. For |
| 1458 | * each flag, once its value is set to 1 it is never reset to 0 |
| 1459 | * again. |
| 1460 | */ |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1461 | static void obj_request_img_data_set(struct rbd_obj_request *obj_request) |
| 1462 | { |
| 1463 | if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1464 | struct rbd_device *rbd_dev; |
| 1465 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1466 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1467 | rbd_warn(rbd_dev, "obj_request %p already marked img_data", |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1468 | obj_request); |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | static bool obj_request_img_data_test(struct rbd_obj_request *obj_request) |
| 1473 | { |
| 1474 | smp_mb(); |
| 1475 | return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0; |
| 1476 | } |
| 1477 | |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1478 | static void obj_request_done_set(struct rbd_obj_request *obj_request) |
| 1479 | { |
| 1480 | if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) { |
| 1481 | struct rbd_device *rbd_dev = NULL; |
| 1482 | |
| 1483 | if (obj_request_img_data_test(obj_request)) |
| 1484 | rbd_dev = obj_request->img_request->rbd_dev; |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1485 | rbd_warn(rbd_dev, "obj_request %p already marked done", |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1486 | obj_request); |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | static bool obj_request_done_test(struct rbd_obj_request *obj_request) |
| 1491 | { |
| 1492 | smp_mb(); |
| 1493 | return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0; |
| 1494 | } |
| 1495 | |
Alex Elder | 5679c59 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1496 | /* |
| 1497 | * This sets the KNOWN flag after (possibly) setting the EXISTS |
| 1498 | * flag. The latter is set based on the "exists" value provided. |
| 1499 | * |
| 1500 | * Note that for our purposes once an object exists it never goes |
| 1501 | * away again. It's possible that the response from two existence |
| 1502 | * checks are separated by the creation of the target object, and |
| 1503 | * the first ("doesn't exist") response arrives *after* the second |
| 1504 | * ("does exist"). In that case we ignore the second one. |
| 1505 | */ |
| 1506 | static void obj_request_existence_set(struct rbd_obj_request *obj_request, |
| 1507 | bool exists) |
| 1508 | { |
| 1509 | if (exists) |
| 1510 | set_bit(OBJ_REQ_EXISTS, &obj_request->flags); |
| 1511 | set_bit(OBJ_REQ_KNOWN, &obj_request->flags); |
| 1512 | smp_mb(); |
| 1513 | } |
| 1514 | |
| 1515 | static bool obj_request_known_test(struct rbd_obj_request *obj_request) |
| 1516 | { |
| 1517 | smp_mb(); |
| 1518 | return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0; |
| 1519 | } |
| 1520 | |
| 1521 | static bool obj_request_exists_test(struct rbd_obj_request *obj_request) |
| 1522 | { |
| 1523 | smp_mb(); |
| 1524 | return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0; |
| 1525 | } |
| 1526 | |
Ilya Dryomov | 9638556 | 2014-06-10 13:53:29 +0400 | [diff] [blame] | 1527 | static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request) |
| 1528 | { |
| 1529 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
| 1530 | |
| 1531 | return obj_request->img_offset < |
| 1532 | round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header)); |
| 1533 | } |
| 1534 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1535 | static void rbd_obj_request_get(struct rbd_obj_request *obj_request) |
| 1536 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1537 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1538 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1539 | kref_get(&obj_request->kref); |
| 1540 | } |
| 1541 | |
| 1542 | static void rbd_obj_request_destroy(struct kref *kref); |
| 1543 | static void rbd_obj_request_put(struct rbd_obj_request *obj_request) |
| 1544 | { |
| 1545 | rbd_assert(obj_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1546 | dout("%s: obj %p (was %d)\n", __func__, obj_request, |
| 1547 | atomic_read(&obj_request->kref.refcount)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1548 | kref_put(&obj_request->kref, rbd_obj_request_destroy); |
| 1549 | } |
| 1550 | |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 1551 | static void rbd_img_request_get(struct rbd_img_request *img_request) |
| 1552 | { |
| 1553 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1554 | atomic_read(&img_request->kref.refcount)); |
| 1555 | kref_get(&img_request->kref); |
| 1556 | } |
| 1557 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1558 | static bool img_request_child_test(struct rbd_img_request *img_request); |
| 1559 | static void rbd_parent_request_destroy(struct kref *kref); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1560 | static void rbd_img_request_destroy(struct kref *kref); |
| 1561 | static void rbd_img_request_put(struct rbd_img_request *img_request) |
| 1562 | { |
| 1563 | rbd_assert(img_request != NULL); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1564 | dout("%s: img %p (was %d)\n", __func__, img_request, |
| 1565 | atomic_read(&img_request->kref.refcount)); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1566 | if (img_request_child_test(img_request)) |
| 1567 | kref_put(&img_request->kref, rbd_parent_request_destroy); |
| 1568 | else |
| 1569 | kref_put(&img_request->kref, rbd_img_request_destroy); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request, |
| 1573 | struct rbd_obj_request *obj_request) |
| 1574 | { |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1575 | rbd_assert(obj_request->img_request == NULL); |
| 1576 | |
Alex Elder | b155e86 | 2013-04-15 14:50:37 -0500 | [diff] [blame] | 1577 | /* Image request now owns object's original reference */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1578 | obj_request->img_request = img_request; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1579 | obj_request->which = img_request->obj_request_count; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1580 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 1581 | obj_request_img_data_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1582 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1583 | img_request->obj_request_count++; |
| 1584 | list_add_tail(&obj_request->links, &img_request->obj_requests); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1585 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1586 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request, |
| 1590 | struct rbd_obj_request *obj_request) |
| 1591 | { |
| 1592 | rbd_assert(obj_request->which != BAD_WHICH); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1593 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1594 | dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request, |
| 1595 | obj_request->which); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1596 | list_del(&obj_request->links); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1597 | rbd_assert(img_request->obj_request_count > 0); |
| 1598 | img_request->obj_request_count--; |
| 1599 | rbd_assert(obj_request->which == img_request->obj_request_count); |
| 1600 | obj_request->which = BAD_WHICH; |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1601 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1602 | rbd_assert(obj_request->img_request == img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1603 | obj_request->img_request = NULL; |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 1604 | obj_request->callback = NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1605 | rbd_obj_request_put(obj_request); |
| 1606 | } |
| 1607 | |
| 1608 | static bool obj_request_type_valid(enum obj_request_type type) |
| 1609 | { |
| 1610 | switch (type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 1611 | case OBJ_REQUEST_NODATA: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1612 | case OBJ_REQUEST_BIO: |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1613 | case OBJ_REQUEST_PAGES: |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1614 | return true; |
| 1615 | default: |
| 1616 | return false; |
| 1617 | } |
| 1618 | } |
| 1619 | |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1620 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request); |
| 1621 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1622 | static void rbd_obj_request_submit(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1623 | { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1624 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
| 1625 | |
| 1626 | dout("%s %p osd_req %p\n", __func__, obj_request, osd_req); |
Ilya Dryomov | 4a17dad | 2016-09-13 21:08:10 +0200 | [diff] [blame] | 1627 | if (obj_request_img_data_test(obj_request)) { |
| 1628 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1629 | rbd_img_request_get(obj_request->img_request); |
| 1630 | } |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 1631 | ceph_osdc_start_request(osd_req->r_osdc, osd_req, false); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1632 | } |
| 1633 | |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1634 | static void rbd_obj_request_end(struct rbd_obj_request *obj_request) |
| 1635 | { |
| 1636 | dout("%s %p\n", __func__, obj_request); |
| 1637 | ceph_osdc_cancel_request(obj_request->osd_req); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Wait for an object request to complete. If interrupted, cancel the |
| 1642 | * underlying osd request. |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1643 | * |
| 1644 | * @timeout: in jiffies, 0 means "wait forever" |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1645 | */ |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1646 | static int __rbd_obj_request_wait(struct rbd_obj_request *obj_request, |
| 1647 | unsigned long timeout) |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1648 | { |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1649 | long ret; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1650 | |
| 1651 | dout("%s %p\n", __func__, obj_request); |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1652 | ret = wait_for_completion_interruptible_timeout( |
| 1653 | &obj_request->completion, |
| 1654 | ceph_timeout_jiffies(timeout)); |
| 1655 | if (ret <= 0) { |
| 1656 | if (ret == 0) |
| 1657 | ret = -ETIMEDOUT; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1658 | rbd_obj_request_end(obj_request); |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1659 | } else { |
| 1660 | ret = 0; |
Ilya Dryomov | 71c20a0 | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 1661 | } |
| 1662 | |
Ilya Dryomov | 2894e1d | 2015-05-12 19:53:24 +0300 | [diff] [blame] | 1663 | dout("%s %p ret %d\n", __func__, obj_request, (int)ret); |
| 1664 | return ret; |
| 1665 | } |
| 1666 | |
| 1667 | static int rbd_obj_request_wait(struct rbd_obj_request *obj_request) |
| 1668 | { |
| 1669 | return __rbd_obj_request_wait(obj_request, 0); |
| 1670 | } |
| 1671 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1672 | static void rbd_img_request_complete(struct rbd_img_request *img_request) |
| 1673 | { |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1674 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1675 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | 55f27e0 | 2013-04-10 12:34:25 -0500 | [diff] [blame] | 1676 | |
| 1677 | /* |
| 1678 | * If no error occurred, compute the aggregate transfer |
| 1679 | * count for the image request. We could instead use |
| 1680 | * atomic64_cmpxchg() to update it as each object request |
| 1681 | * completes; not clear which way is better off hand. |
| 1682 | */ |
| 1683 | if (!img_request->result) { |
| 1684 | struct rbd_obj_request *obj_request; |
| 1685 | u64 xferred = 0; |
| 1686 | |
| 1687 | for_each_obj_request(img_request, obj_request) |
| 1688 | xferred += obj_request->xferred; |
| 1689 | img_request->xferred = xferred; |
| 1690 | } |
| 1691 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1692 | if (img_request->callback) |
| 1693 | img_request->callback(img_request); |
| 1694 | else |
| 1695 | rbd_img_request_put(img_request); |
| 1696 | } |
| 1697 | |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 1698 | /* |
| 1699 | * The default/initial value for all image request flags is 0. Each |
| 1700 | * is conditionally set to 1 at image request initialization time |
| 1701 | * and currently never change thereafter. |
| 1702 | */ |
| 1703 | static void img_request_write_set(struct rbd_img_request *img_request) |
| 1704 | { |
| 1705 | set_bit(IMG_REQ_WRITE, &img_request->flags); |
| 1706 | smp_mb(); |
| 1707 | } |
| 1708 | |
| 1709 | static bool img_request_write_test(struct rbd_img_request *img_request) |
| 1710 | { |
| 1711 | smp_mb(); |
| 1712 | return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0; |
| 1713 | } |
| 1714 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1715 | /* |
| 1716 | * Set the discard flag when the img_request is an discard request |
| 1717 | */ |
| 1718 | static void img_request_discard_set(struct rbd_img_request *img_request) |
| 1719 | { |
| 1720 | set_bit(IMG_REQ_DISCARD, &img_request->flags); |
| 1721 | smp_mb(); |
| 1722 | } |
| 1723 | |
| 1724 | static bool img_request_discard_test(struct rbd_img_request *img_request) |
| 1725 | { |
| 1726 | smp_mb(); |
| 1727 | return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0; |
| 1728 | } |
| 1729 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1730 | static void img_request_child_set(struct rbd_img_request *img_request) |
| 1731 | { |
| 1732 | set_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1733 | smp_mb(); |
| 1734 | } |
| 1735 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1736 | static void img_request_child_clear(struct rbd_img_request *img_request) |
| 1737 | { |
| 1738 | clear_bit(IMG_REQ_CHILD, &img_request->flags); |
| 1739 | smp_mb(); |
| 1740 | } |
| 1741 | |
Alex Elder | 9849e98 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1742 | static bool img_request_child_test(struct rbd_img_request *img_request) |
| 1743 | { |
| 1744 | smp_mb(); |
| 1745 | return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0; |
| 1746 | } |
| 1747 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1748 | static void img_request_layered_set(struct rbd_img_request *img_request) |
| 1749 | { |
| 1750 | set_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1751 | smp_mb(); |
| 1752 | } |
| 1753 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 1754 | static void img_request_layered_clear(struct rbd_img_request *img_request) |
| 1755 | { |
| 1756 | clear_bit(IMG_REQ_LAYERED, &img_request->flags); |
| 1757 | smp_mb(); |
| 1758 | } |
| 1759 | |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1760 | static bool img_request_layered_test(struct rbd_img_request *img_request) |
| 1761 | { |
| 1762 | smp_mb(); |
| 1763 | return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0; |
| 1764 | } |
| 1765 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 1766 | static enum obj_operation_type |
| 1767 | rbd_img_request_op_type(struct rbd_img_request *img_request) |
| 1768 | { |
| 1769 | if (img_request_write_test(img_request)) |
| 1770 | return OBJ_OP_WRITE; |
| 1771 | else if (img_request_discard_test(img_request)) |
| 1772 | return OBJ_OP_DISCARD; |
| 1773 | else |
| 1774 | return OBJ_OP_READ; |
| 1775 | } |
| 1776 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1777 | static void |
| 1778 | rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request) |
| 1779 | { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1780 | u64 xferred = obj_request->xferred; |
| 1781 | u64 length = obj_request->length; |
| 1782 | |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1783 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1784 | obj_request, obj_request->img_request, obj_request->result, |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1785 | xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1786 | /* |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1787 | * ENOENT means a hole in the image. We zero-fill the entire |
| 1788 | * length of the request. A short read also implies zero-fill |
| 1789 | * to the end of the request. An error requires the whole |
| 1790 | * length of the request to be reported finished with an error |
| 1791 | * to the block layer. In each case we update the xferred |
| 1792 | * count to indicate the whole request was satisfied. |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1793 | */ |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1794 | rbd_assert(obj_request->type != OBJ_REQUEST_NODATA); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1795 | if (obj_request->result == -ENOENT) { |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1796 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1797 | zero_bio_chain(obj_request->bio_list, 0); |
| 1798 | else |
| 1799 | zero_pages(obj_request->pages, 0, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1800 | obj_request->result = 0; |
Alex Elder | b9434c5 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1801 | } else if (xferred < length && !obj_request->result) { |
| 1802 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 1803 | zero_bio_chain(obj_request->bio_list, xferred); |
| 1804 | else |
| 1805 | zero_pages(obj_request->pages, xferred, length); |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1806 | } |
Josh Durgin | 17c1cc1 | 2013-08-26 17:55:38 -0700 | [diff] [blame] | 1807 | obj_request->xferred = length; |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1808 | obj_request_done_set(obj_request); |
| 1809 | } |
| 1810 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1811 | static void rbd_obj_request_complete(struct rbd_obj_request *obj_request) |
| 1812 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1813 | dout("%s: obj %p cb %p\n", __func__, obj_request, |
| 1814 | obj_request->callback); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1815 | if (obj_request->callback) |
| 1816 | obj_request->callback(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 1817 | else |
| 1818 | complete_all(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1819 | } |
| 1820 | |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 1821 | static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err) |
| 1822 | { |
| 1823 | obj_request->result = err; |
| 1824 | obj_request->xferred = 0; |
| 1825 | /* |
| 1826 | * kludge - mirror rbd_obj_request_submit() to match a put in |
| 1827 | * rbd_img_obj_callback() |
| 1828 | */ |
| 1829 | if (obj_request_img_data_test(obj_request)) { |
| 1830 | WARN_ON(obj_request->callback != rbd_img_obj_callback); |
| 1831 | rbd_img_request_get(obj_request->img_request); |
| 1832 | } |
| 1833 | obj_request_done_set(obj_request); |
| 1834 | rbd_obj_request_complete(obj_request); |
| 1835 | } |
| 1836 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1837 | static void rbd_osd_read_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1838 | { |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1839 | struct rbd_img_request *img_request = NULL; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1840 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1841 | bool layered = false; |
| 1842 | |
| 1843 | if (obj_request_img_data_test(obj_request)) { |
| 1844 | img_request = obj_request->img_request; |
| 1845 | layered = img_request && img_request_layered_test(img_request); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1846 | rbd_dev = img_request->rbd_dev; |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1847 | } |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1848 | |
| 1849 | dout("%s: obj %p img %p result %d %llu/%llu\n", __func__, |
| 1850 | obj_request, img_request, obj_request->result, |
| 1851 | obj_request->xferred, obj_request->length); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 1852 | if (layered && obj_request->result == -ENOENT && |
| 1853 | obj_request->img_offset < rbd_dev->parent_overlap) |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1854 | rbd_img_parent_read(obj_request); |
| 1855 | else if (img_request) |
Alex Elder | 6e2a450 | 2013-03-27 09:16:30 -0500 | [diff] [blame] | 1856 | rbd_img_obj_request_read_callback(obj_request); |
| 1857 | else |
| 1858 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1859 | } |
| 1860 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1861 | static void rbd_osd_write_callback(struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1862 | { |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1863 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1864 | obj_request->result, obj_request->length); |
| 1865 | /* |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1866 | * There is no such thing as a successful short write. Set |
| 1867 | * it to our originally-requested length. |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1868 | */ |
| 1869 | obj_request->xferred = obj_request->length; |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1870 | obj_request_done_set(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1871 | } |
| 1872 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1873 | static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request) |
| 1874 | { |
| 1875 | dout("%s: obj %p result %d %llu\n", __func__, obj_request, |
| 1876 | obj_request->result, obj_request->length); |
| 1877 | /* |
| 1878 | * There is no such thing as a successful short discard. Set |
| 1879 | * it to our originally-requested length. |
| 1880 | */ |
| 1881 | obj_request->xferred = obj_request->length; |
Josh Durgin | d0265de | 2014-04-07 16:54:10 -0700 | [diff] [blame] | 1882 | /* discarding a non-existent object is not a problem */ |
| 1883 | if (obj_request->result == -ENOENT) |
| 1884 | obj_request->result = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1885 | obj_request_done_set(obj_request); |
| 1886 | } |
| 1887 | |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1888 | /* |
| 1889 | * For a simple stat call there's nothing to do. We'll do more if |
| 1890 | * this is part of a write sequence for a layered image. |
| 1891 | */ |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1892 | static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request) |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1893 | { |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 1894 | dout("%s: obj %p\n", __func__, obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1895 | obj_request_done_set(obj_request); |
| 1896 | } |
| 1897 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1898 | static void rbd_osd_call_callback(struct rbd_obj_request *obj_request) |
| 1899 | { |
| 1900 | dout("%s: obj %p\n", __func__, obj_request); |
| 1901 | |
| 1902 | if (obj_request_img_data_test(obj_request)) |
| 1903 | rbd_osd_copyup_callback(obj_request); |
| 1904 | else |
| 1905 | obj_request_done_set(obj_request); |
| 1906 | } |
| 1907 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1908 | static void rbd_osd_req_callback(struct ceph_osd_request *osd_req) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1909 | { |
| 1910 | struct rbd_obj_request *obj_request = osd_req->r_priv; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1911 | u16 opcode; |
| 1912 | |
Ilya Dryomov | 85e084f | 2016-04-28 16:07:24 +0200 | [diff] [blame] | 1913 | dout("%s: osd_req %p\n", __func__, osd_req); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1914 | rbd_assert(osd_req == obj_request->osd_req); |
Alex Elder | 57acbaa | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 1915 | if (obj_request_img_data_test(obj_request)) { |
| 1916 | rbd_assert(obj_request->img_request); |
| 1917 | rbd_assert(obj_request->which != BAD_WHICH); |
| 1918 | } else { |
| 1919 | rbd_assert(obj_request->which == BAD_WHICH); |
| 1920 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1921 | |
Sage Weil | 1b83bef | 2013-02-25 16:11:12 -0800 | [diff] [blame] | 1922 | if (osd_req->r_result < 0) |
| 1923 | obj_request->result = osd_req->r_result; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1924 | |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1925 | /* |
| 1926 | * We support a 64-bit length, but ultimately it has to be |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 1927 | * passed to the block layer, which just supports a 32-bit |
| 1928 | * length field. |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1929 | */ |
Yan, Zheng | 7665d85 | 2016-01-07 16:48:57 +0800 | [diff] [blame] | 1930 | obj_request->xferred = osd_req->r_ops[0].outdata_len; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 1931 | rbd_assert(obj_request->xferred < (u64)UINT_MAX); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1932 | |
Alex Elder | 7952873 | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1933 | opcode = osd_req->r_ops[0].op; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1934 | switch (opcode) { |
| 1935 | case CEPH_OSD_OP_READ: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1936 | rbd_osd_read_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1937 | break; |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1938 | case CEPH_OSD_OP_SETALLOCHINT: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1939 | rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE || |
| 1940 | osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL); |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1941 | /* fall through */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1942 | case CEPH_OSD_OP_WRITE: |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 1943 | case CEPH_OSD_OP_WRITEFULL: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1944 | rbd_osd_write_callback(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1945 | break; |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1946 | case CEPH_OSD_OP_STAT: |
Alex Elder | c47f937 | 2013-02-26 14:23:07 -0600 | [diff] [blame] | 1947 | rbd_osd_stat_callback(obj_request); |
Alex Elder | fbfab53 | 2013-02-08 09:55:48 -0600 | [diff] [blame] | 1948 | break; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1949 | case CEPH_OSD_OP_DELETE: |
| 1950 | case CEPH_OSD_OP_TRUNCATE: |
| 1951 | case CEPH_OSD_OP_ZERO: |
| 1952 | rbd_osd_discard_callback(obj_request); |
| 1953 | break; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 1954 | case CEPH_OSD_OP_CALL: |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 1955 | rbd_osd_call_callback(obj_request); |
| 1956 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1957 | default: |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 1958 | rbd_warn(NULL, "%s: unsupported op %hu", |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1959 | obj_request->object_name, (unsigned short) opcode); |
| 1960 | break; |
| 1961 | } |
| 1962 | |
Alex Elder | 0774130 | 2013-02-05 23:41:50 -0600 | [diff] [blame] | 1963 | if (obj_request_done_test(obj_request)) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1964 | rbd_obj_request_complete(obj_request); |
| 1965 | } |
| 1966 | |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1967 | 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] | 1968 | { |
Alex Elder | 8c042b0 | 2013-04-03 01:28:58 -0500 | [diff] [blame] | 1969 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1970 | |
Ilya Dryomov | 7c84883 | 2016-09-15 17:56:39 +0200 | [diff] [blame] | 1971 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 1972 | osd_req->r_snapid = obj_request->img_request->snap_id; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request) |
| 1976 | { |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1977 | struct ceph_osd_request *osd_req = obj_request->osd_req; |
Alex Elder | 9d4df01 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 1978 | |
Ilya Dryomov | bb873b5 | 2016-05-26 00:29:52 +0200 | [diff] [blame] | 1979 | osd_req->r_mtime = CURRENT_TIME; |
| 1980 | osd_req->r_data_offset = obj_request->offset; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1981 | } |
| 1982 | |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 1983 | /* |
| 1984 | * Create an osd request. A read request has one osd op (read). |
| 1985 | * A write request has either one (watch) or two (hint+write) osd ops. |
| 1986 | * (All rbd data writes are prefixed with an allocation hint op, but |
| 1987 | * technically osd watch is a write request, hence this distinction.) |
| 1988 | */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1989 | static struct ceph_osd_request *rbd_osd_req_create( |
| 1990 | struct rbd_device *rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 1991 | enum obj_operation_type op_type, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 1992 | unsigned int num_ops, |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 1993 | struct rbd_obj_request *obj_request) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1994 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1995 | struct ceph_snap_context *snapc = NULL; |
| 1996 | struct ceph_osd_client *osdc; |
| 1997 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 1998 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 1999 | if (obj_request_img_data_test(obj_request) && |
| 2000 | (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2001 | struct rbd_img_request *img_request = obj_request->img_request; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2002 | if (op_type == OBJ_OP_WRITE) { |
| 2003 | rbd_assert(img_request_write_test(img_request)); |
| 2004 | } else { |
| 2005 | rbd_assert(img_request_discard_test(img_request)); |
| 2006 | } |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2007 | snapc = img_request->snapc; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2008 | } |
| 2009 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2010 | rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2)); |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 2011 | |
| 2012 | /* Allocate and initialize the request, for the num_ops ops */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2013 | |
| 2014 | osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 2015 | osd_req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2016 | GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2017 | if (!osd_req) |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2018 | goto fail; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2019 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2020 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2021 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK; |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 2022 | else |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2023 | osd_req->r_flags = CEPH_OSD_FLAG_READ; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2024 | |
| 2025 | osd_req->r_callback = rbd_osd_req_callback; |
| 2026 | osd_req->r_priv = obj_request; |
| 2027 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 2028 | osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 2029 | if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s", |
| 2030 | obj_request->object_name)) |
| 2031 | goto fail; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2032 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2033 | if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO)) |
| 2034 | goto fail; |
| 2035 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2036 | return osd_req; |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2037 | |
| 2038 | fail: |
| 2039 | ceph_osdc_put_request(osd_req); |
| 2040 | return NULL; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2041 | } |
| 2042 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2043 | /* |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2044 | * Create a copyup osd request based on the information in the object |
| 2045 | * request supplied. A copyup request has two or three osd ops, a |
| 2046 | * copyup method call, potentially a hint op, and a write or truncate |
| 2047 | * or zero op. |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2048 | */ |
| 2049 | static struct ceph_osd_request * |
| 2050 | rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request) |
| 2051 | { |
| 2052 | struct rbd_img_request *img_request; |
| 2053 | struct ceph_snap_context *snapc; |
| 2054 | struct rbd_device *rbd_dev; |
| 2055 | struct ceph_osd_client *osdc; |
| 2056 | struct ceph_osd_request *osd_req; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2057 | int num_osd_ops = 3; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2058 | |
| 2059 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2060 | img_request = obj_request->img_request; |
| 2061 | rbd_assert(img_request); |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2062 | rbd_assert(img_request_write_test(img_request) || |
| 2063 | img_request_discard_test(img_request)); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2064 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2065 | if (img_request_discard_test(img_request)) |
| 2066 | num_osd_ops = 2; |
| 2067 | |
| 2068 | /* Allocate and initialize the request, for all the ops */ |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2069 | |
| 2070 | snapc = img_request->snapc; |
| 2071 | rbd_dev = img_request->rbd_dev; |
| 2072 | osdc = &rbd_dev->rbd_client->client->osdc; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2073 | osd_req = ceph_osdc_alloc_request(osdc, snapc, num_osd_ops, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2074 | false, GFP_NOIO); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2075 | if (!osd_req) |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2076 | goto fail; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2077 | |
| 2078 | osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK; |
| 2079 | osd_req->r_callback = rbd_osd_req_callback; |
| 2080 | osd_req->r_priv = obj_request; |
| 2081 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 2082 | osd_req->r_base_oloc.pool = rbd_dev->layout.pool_id; |
Ilya Dryomov | d30291b | 2016-04-29 19:54:20 +0200 | [diff] [blame] | 2083 | if (ceph_oid_aprintf(&osd_req->r_base_oid, GFP_NOIO, "%s", |
| 2084 | obj_request->object_name)) |
| 2085 | goto fail; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2086 | |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2087 | if (ceph_osdc_alloc_messages(osd_req, GFP_NOIO)) |
| 2088 | goto fail; |
| 2089 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2090 | return osd_req; |
Ilya Dryomov | 13d1ad1 | 2016-04-27 14:15:51 +0200 | [diff] [blame] | 2091 | |
| 2092 | fail: |
| 2093 | ceph_osdc_put_request(osd_req); |
| 2094 | return NULL; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2098 | static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req) |
| 2099 | { |
| 2100 | ceph_osdc_put_request(osd_req); |
| 2101 | } |
| 2102 | |
| 2103 | /* object_name is assumed to be a non-null pointer and NUL-terminated */ |
| 2104 | |
| 2105 | static struct rbd_obj_request *rbd_obj_request_create(const char *object_name, |
| 2106 | u64 offset, u64 length, |
| 2107 | enum obj_request_type type) |
| 2108 | { |
| 2109 | struct rbd_obj_request *obj_request; |
| 2110 | size_t size; |
| 2111 | char *name; |
| 2112 | |
| 2113 | rbd_assert(obj_request_type_valid(type)); |
| 2114 | |
| 2115 | size = strlen(object_name) + 1; |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2116 | name = kmalloc(size, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2117 | if (!name) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2118 | return NULL; |
| 2119 | |
Ilya Dryomov | 5a60e87 | 2015-06-24 17:24:33 +0300 | [diff] [blame] | 2120 | obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO); |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2121 | if (!obj_request) { |
| 2122 | kfree(name); |
| 2123 | return NULL; |
| 2124 | } |
| 2125 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2126 | obj_request->object_name = memcpy(name, object_name, size); |
| 2127 | obj_request->offset = offset; |
| 2128 | obj_request->length = length; |
Alex Elder | 926f9b3 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2129 | obj_request->flags = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2130 | obj_request->which = BAD_WHICH; |
| 2131 | obj_request->type = type; |
| 2132 | INIT_LIST_HEAD(&obj_request->links); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2133 | init_completion(&obj_request->completion); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2134 | kref_init(&obj_request->kref); |
| 2135 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2136 | dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name, |
| 2137 | offset, length, (int)type, obj_request); |
| 2138 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2139 | return obj_request; |
| 2140 | } |
| 2141 | |
| 2142 | static void rbd_obj_request_destroy(struct kref *kref) |
| 2143 | { |
| 2144 | struct rbd_obj_request *obj_request; |
| 2145 | |
| 2146 | obj_request = container_of(kref, struct rbd_obj_request, kref); |
| 2147 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2148 | dout("%s: obj %p\n", __func__, obj_request); |
| 2149 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2150 | rbd_assert(obj_request->img_request == NULL); |
| 2151 | rbd_assert(obj_request->which == BAD_WHICH); |
| 2152 | |
| 2153 | if (obj_request->osd_req) |
| 2154 | rbd_osd_req_destroy(obj_request->osd_req); |
| 2155 | |
| 2156 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2157 | switch (obj_request->type) { |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 2158 | case OBJ_REQUEST_NODATA: |
| 2159 | break; /* Nothing to do */ |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2160 | case OBJ_REQUEST_BIO: |
| 2161 | if (obj_request->bio_list) |
| 2162 | bio_chain_put(obj_request->bio_list); |
| 2163 | break; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2164 | case OBJ_REQUEST_PAGES: |
Ilya Dryomov | 04dc923 | 2016-09-15 18:05:16 +0200 | [diff] [blame] | 2165 | /* img_data requests don't own their page array */ |
| 2166 | if (obj_request->pages && |
| 2167 | !obj_request_img_data_test(obj_request)) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 2168 | ceph_release_page_vector(obj_request->pages, |
| 2169 | obj_request->page_count); |
| 2170 | break; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2171 | } |
| 2172 | |
Alex Elder | f907ad5 | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2173 | kfree(obj_request->object_name); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2174 | obj_request->object_name = NULL; |
| 2175 | kmem_cache_free(rbd_obj_request_cache, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2176 | } |
| 2177 | |
Alex Elder | fb65d228 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2178 | /* It's OK to call this for a device with no parent */ |
| 2179 | |
| 2180 | static void rbd_spec_put(struct rbd_spec *spec); |
| 2181 | static void rbd_dev_unparent(struct rbd_device *rbd_dev) |
| 2182 | { |
| 2183 | rbd_dev_remove_parent(rbd_dev); |
| 2184 | rbd_spec_put(rbd_dev->parent_spec); |
| 2185 | rbd_dev->parent_spec = NULL; |
| 2186 | rbd_dev->parent_overlap = 0; |
| 2187 | } |
| 2188 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2189 | /* |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2190 | * Parent image reference counting is used to determine when an |
| 2191 | * image's parent fields can be safely torn down--after there are no |
| 2192 | * more in-flight requests to the parent image. When the last |
| 2193 | * reference is dropped, cleaning them up is safe. |
| 2194 | */ |
| 2195 | static void rbd_dev_parent_put(struct rbd_device *rbd_dev) |
| 2196 | { |
| 2197 | int counter; |
| 2198 | |
| 2199 | if (!rbd_dev->parent_spec) |
| 2200 | return; |
| 2201 | |
| 2202 | counter = atomic_dec_return_safe(&rbd_dev->parent_ref); |
| 2203 | if (counter > 0) |
| 2204 | return; |
| 2205 | |
| 2206 | /* Last reference; clean up parent data structures */ |
| 2207 | |
| 2208 | if (!counter) |
| 2209 | rbd_dev_unparent(rbd_dev); |
| 2210 | else |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2211 | rbd_warn(rbd_dev, "parent reference underflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /* |
| 2215 | * If an image has a non-zero parent overlap, get a reference to its |
| 2216 | * parent. |
| 2217 | * |
| 2218 | * Returns true if the rbd device has a parent with a non-zero |
| 2219 | * overlap and a reference for it was successfully taken, or |
| 2220 | * false otherwise. |
| 2221 | */ |
| 2222 | static bool rbd_dev_parent_get(struct rbd_device *rbd_dev) |
| 2223 | { |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2224 | int counter = 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2225 | |
| 2226 | if (!rbd_dev->parent_spec) |
| 2227 | return false; |
| 2228 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2229 | down_read(&rbd_dev->header_rwsem); |
| 2230 | if (rbd_dev->parent_overlap) |
| 2231 | counter = atomic_inc_return_safe(&rbd_dev->parent_ref); |
| 2232 | up_read(&rbd_dev->header_rwsem); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2233 | |
| 2234 | if (counter < 0) |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2235 | rbd_warn(rbd_dev, "parent reference overflow"); |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2236 | |
Ilya Dryomov | ae43e9d | 2015-01-19 18:13:43 +0300 | [diff] [blame] | 2237 | return counter > 0; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2238 | } |
| 2239 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2240 | /* |
| 2241 | * Caller is responsible for filling in the list of object requests |
| 2242 | * that comprises the image request, and the Linux request pointer |
| 2243 | * (if there is one). |
| 2244 | */ |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 2245 | static struct rbd_img_request *rbd_img_request_create( |
| 2246 | struct rbd_device *rbd_dev, |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2247 | u64 offset, u64 length, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2248 | enum obj_operation_type op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2249 | struct ceph_snap_context *snapc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2250 | { |
| 2251 | struct rbd_img_request *img_request; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2252 | |
Ilya Dryomov | 7a716aa | 2014-08-05 11:25:54 +0400 | [diff] [blame] | 2253 | img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_NOIO); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2254 | if (!img_request) |
| 2255 | return NULL; |
| 2256 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2257 | img_request->rq = NULL; |
| 2258 | img_request->rbd_dev = rbd_dev; |
| 2259 | img_request->offset = offset; |
| 2260 | img_request->length = length; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2261 | img_request->flags = 0; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2262 | if (op_type == OBJ_OP_DISCARD) { |
| 2263 | img_request_discard_set(img_request); |
| 2264 | img_request->snapc = snapc; |
| 2265 | } else if (op_type == OBJ_OP_WRITE) { |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2266 | img_request_write_set(img_request); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2267 | img_request->snapc = snapc; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2268 | } else { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2269 | img_request->snap_id = rbd_dev->spec->snap_id; |
Alex Elder | 0c42524 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2270 | } |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2271 | if (rbd_dev_parent_get(rbd_dev)) |
Alex Elder | d0b2e94 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2272 | img_request_layered_set(img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2273 | spin_lock_init(&img_request->completion_lock); |
| 2274 | img_request->next_completion = 0; |
| 2275 | img_request->callback = NULL; |
Alex Elder | a5a337d | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2276 | img_request->result = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2277 | img_request->obj_request_count = 0; |
| 2278 | INIT_LIST_HEAD(&img_request->obj_requests); |
| 2279 | kref_init(&img_request->kref); |
| 2280 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2281 | dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2282 | obj_op_name(op_type), offset, length, img_request); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2283 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2284 | return img_request; |
| 2285 | } |
| 2286 | |
| 2287 | static void rbd_img_request_destroy(struct kref *kref) |
| 2288 | { |
| 2289 | struct rbd_img_request *img_request; |
| 2290 | struct rbd_obj_request *obj_request; |
| 2291 | struct rbd_obj_request *next_obj_request; |
| 2292 | |
| 2293 | img_request = container_of(kref, struct rbd_img_request, kref); |
| 2294 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2295 | dout("%s: img %p\n", __func__, img_request); |
| 2296 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2297 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
| 2298 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | 25dcf95 | 2013-01-25 17:08:55 -0600 | [diff] [blame] | 2299 | rbd_assert(img_request->obj_request_count == 0); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2300 | |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2301 | if (img_request_layered_test(img_request)) { |
| 2302 | img_request_layered_clear(img_request); |
| 2303 | rbd_dev_parent_put(img_request->rbd_dev); |
| 2304 | } |
| 2305 | |
Josh Durgin | bef9545 | 2014-04-04 17:47:52 -0700 | [diff] [blame] | 2306 | if (img_request_write_test(img_request) || |
| 2307 | img_request_discard_test(img_request)) |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 2308 | ceph_put_snap_context(img_request->snapc); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2309 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 2310 | kmem_cache_free(rbd_img_request_cache, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2311 | } |
| 2312 | |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2313 | static struct rbd_img_request *rbd_parent_request_create( |
| 2314 | struct rbd_obj_request *obj_request, |
| 2315 | u64 img_offset, u64 length) |
| 2316 | { |
| 2317 | struct rbd_img_request *parent_request; |
| 2318 | struct rbd_device *rbd_dev; |
| 2319 | |
| 2320 | rbd_assert(obj_request->img_request); |
| 2321 | rbd_dev = obj_request->img_request->rbd_dev; |
| 2322 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 2323 | parent_request = rbd_img_request_create(rbd_dev->parent, img_offset, |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2324 | length, OBJ_OP_READ, NULL); |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2325 | if (!parent_request) |
| 2326 | return NULL; |
| 2327 | |
| 2328 | img_request_child_set(parent_request); |
| 2329 | rbd_obj_request_get(obj_request); |
| 2330 | parent_request->obj_request = obj_request; |
| 2331 | |
| 2332 | return parent_request; |
| 2333 | } |
| 2334 | |
| 2335 | static void rbd_parent_request_destroy(struct kref *kref) |
| 2336 | { |
| 2337 | struct rbd_img_request *parent_request; |
| 2338 | struct rbd_obj_request *orig_request; |
| 2339 | |
| 2340 | parent_request = container_of(kref, struct rbd_img_request, kref); |
| 2341 | orig_request = parent_request->obj_request; |
| 2342 | |
| 2343 | parent_request->obj_request = NULL; |
| 2344 | rbd_obj_request_put(orig_request); |
| 2345 | img_request_child_clear(parent_request); |
| 2346 | |
| 2347 | rbd_img_request_destroy(kref); |
| 2348 | } |
| 2349 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2350 | static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request) |
| 2351 | { |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2352 | struct rbd_img_request *img_request; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2353 | unsigned int xferred; |
| 2354 | int result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2355 | bool more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2356 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2357 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2358 | img_request = obj_request->img_request; |
| 2359 | |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2360 | rbd_assert(obj_request->xferred <= (u64)UINT_MAX); |
| 2361 | xferred = (unsigned int)obj_request->xferred; |
| 2362 | result = obj_request->result; |
| 2363 | if (result) { |
| 2364 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2365 | enum obj_operation_type op_type; |
| 2366 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2367 | if (img_request_discard_test(img_request)) |
| 2368 | op_type = OBJ_OP_DISCARD; |
| 2369 | else if (img_request_write_test(img_request)) |
| 2370 | op_type = OBJ_OP_WRITE; |
| 2371 | else |
| 2372 | op_type = OBJ_OP_READ; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2373 | |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2374 | rbd_warn(rbd_dev, "%s %llx at %llx (%llx)", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2375 | obj_op_name(op_type), obj_request->length, |
| 2376 | obj_request->img_offset, obj_request->offset); |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 2377 | rbd_warn(rbd_dev, " result %d xferred %x", |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2378 | result, xferred); |
| 2379 | if (!img_request->result) |
| 2380 | img_request->result = result; |
Ilya Dryomov | 082a75d | 2015-04-25 15:56:15 +0300 | [diff] [blame] | 2381 | /* |
| 2382 | * Need to end I/O on the entire obj_request worth of |
| 2383 | * bytes in case of error. |
| 2384 | */ |
| 2385 | xferred = obj_request->length; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2386 | } |
| 2387 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2388 | if (img_request_child_test(img_request)) { |
| 2389 | rbd_assert(img_request->obj_request != NULL); |
| 2390 | more = obj_request->which < img_request->obj_request_count - 1; |
| 2391 | } else { |
| 2392 | rbd_assert(img_request->rq != NULL); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 2393 | |
| 2394 | more = blk_update_request(img_request->rq, result, xferred); |
| 2395 | if (!more) |
| 2396 | __blk_mq_end_request(img_request->rq, result); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | return more; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2400 | } |
| 2401 | |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2402 | static void rbd_img_obj_callback(struct rbd_obj_request *obj_request) |
| 2403 | { |
| 2404 | struct rbd_img_request *img_request; |
| 2405 | u32 which = obj_request->which; |
| 2406 | bool more = true; |
| 2407 | |
Alex Elder | 6365d33 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2408 | rbd_assert(obj_request_img_data_test(obj_request)); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2409 | img_request = obj_request->img_request; |
| 2410 | |
| 2411 | dout("%s: img %p obj %p\n", __func__, img_request, obj_request); |
| 2412 | rbd_assert(img_request != NULL); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2413 | rbd_assert(img_request->obj_request_count > 0); |
| 2414 | rbd_assert(which != BAD_WHICH); |
| 2415 | rbd_assert(which < img_request->obj_request_count); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2416 | |
| 2417 | spin_lock_irq(&img_request->completion_lock); |
| 2418 | if (which != img_request->next_completion) |
| 2419 | goto out; |
| 2420 | |
| 2421 | for_each_obj_request_from(img_request, obj_request) { |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2422 | rbd_assert(more); |
| 2423 | rbd_assert(which < img_request->obj_request_count); |
| 2424 | |
| 2425 | if (!obj_request_done_test(obj_request)) |
| 2426 | break; |
Alex Elder | 1217857 | 2013-02-08 09:55:49 -0600 | [diff] [blame] | 2427 | more = rbd_img_obj_end_request(obj_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2428 | which++; |
| 2429 | } |
| 2430 | |
| 2431 | rbd_assert(more ^ (which == img_request->obj_request_count)); |
| 2432 | img_request->next_completion = which; |
| 2433 | out: |
| 2434 | spin_unlock_irq(&img_request->completion_lock); |
Alex Elder | 0f2d5be | 2014-04-26 14:21:44 +0400 | [diff] [blame] | 2435 | rbd_img_request_put(img_request); |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2436 | |
| 2437 | if (!more) |
| 2438 | rbd_img_request_complete(img_request); |
| 2439 | } |
| 2440 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2441 | /* |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2442 | * Add individual osd ops to the given ceph_osd_request and prepare |
| 2443 | * them for submission. num_ops is the current number of |
| 2444 | * osd operations already to the object request. |
| 2445 | */ |
| 2446 | static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request, |
| 2447 | struct ceph_osd_request *osd_request, |
| 2448 | enum obj_operation_type op_type, |
| 2449 | unsigned int num_ops) |
| 2450 | { |
| 2451 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2452 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2453 | u64 object_size = rbd_obj_bytes(&rbd_dev->header); |
| 2454 | u64 offset = obj_request->offset; |
| 2455 | u64 length = obj_request->length; |
| 2456 | u64 img_end; |
| 2457 | u16 opcode; |
| 2458 | |
| 2459 | if (op_type == OBJ_OP_DISCARD) { |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2460 | if (!offset && length == object_size && |
| 2461 | (!img_request_layered_test(img_request) || |
| 2462 | !obj_request_overlaps_parent(obj_request))) { |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2463 | opcode = CEPH_OSD_OP_DELETE; |
| 2464 | } else if ((offset + length == object_size)) { |
| 2465 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2466 | } else { |
| 2467 | down_read(&rbd_dev->header_rwsem); |
| 2468 | img_end = rbd_dev->header.image_size; |
| 2469 | up_read(&rbd_dev->header_rwsem); |
| 2470 | |
| 2471 | if (obj_request->img_offset + length == img_end) |
| 2472 | opcode = CEPH_OSD_OP_TRUNCATE; |
| 2473 | else |
| 2474 | opcode = CEPH_OSD_OP_ZERO; |
| 2475 | } |
| 2476 | } else if (op_type == OBJ_OP_WRITE) { |
Ilya Dryomov | e30b757 | 2015-10-07 17:27:17 +0200 | [diff] [blame] | 2477 | if (!offset && length == object_size) |
| 2478 | opcode = CEPH_OSD_OP_WRITEFULL; |
| 2479 | else |
| 2480 | opcode = CEPH_OSD_OP_WRITE; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2481 | osd_req_op_alloc_hint_init(osd_request, num_ops, |
| 2482 | object_size, object_size); |
| 2483 | num_ops++; |
| 2484 | } else { |
| 2485 | opcode = CEPH_OSD_OP_READ; |
| 2486 | } |
| 2487 | |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2488 | if (opcode == CEPH_OSD_OP_DELETE) |
Yan, Zheng | 144cba1 | 2015-04-27 11:09:54 +0800 | [diff] [blame] | 2489 | osd_req_op_init(osd_request, num_ops, opcode, 0); |
Ilya Dryomov | 7e868b6 | 2014-11-21 22:16:43 +0300 | [diff] [blame] | 2490 | else |
| 2491 | osd_req_op_extent_init(osd_request, num_ops, opcode, |
| 2492 | offset, length, 0, 0); |
| 2493 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2494 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 2495 | osd_req_op_extent_osd_data_bio(osd_request, num_ops, |
| 2496 | obj_request->bio_list, length); |
| 2497 | else if (obj_request->type == OBJ_REQUEST_PAGES) |
| 2498 | osd_req_op_extent_osd_data_pages(osd_request, num_ops, |
| 2499 | obj_request->pages, length, |
| 2500 | offset & ~PAGE_MASK, false, false); |
| 2501 | |
| 2502 | /* Discards are also writes */ |
| 2503 | if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) |
| 2504 | rbd_osd_req_format_write(obj_request); |
| 2505 | else |
| 2506 | rbd_osd_req_format_read(obj_request); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2510 | * Split up an image request into one or more object requests, each |
| 2511 | * to a different object. The "type" parameter indicates whether |
| 2512 | * "data_desc" is the pointer to the head of a list of bio |
| 2513 | * structures, or the base of a page array. In either case this |
| 2514 | * function assumes data_desc describes memory sufficient to hold |
| 2515 | * all data described by the image request. |
| 2516 | */ |
| 2517 | static int rbd_img_request_fill(struct rbd_img_request *img_request, |
| 2518 | enum obj_request_type type, |
| 2519 | void *data_desc) |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2520 | { |
| 2521 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
| 2522 | struct rbd_obj_request *obj_request = NULL; |
| 2523 | struct rbd_obj_request *next_obj_request; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2524 | struct bio *bio_list = NULL; |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2525 | unsigned int bio_offset = 0; |
Jingoo Han | a158073 | 2013-08-09 13:04:35 +0900 | [diff] [blame] | 2526 | struct page **pages = NULL; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2527 | enum obj_operation_type op_type; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2528 | u64 img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2529 | u64 resid; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2530 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2531 | dout("%s: img %p type %d data_desc %p\n", __func__, img_request, |
| 2532 | (int)type, data_desc); |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 2533 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2534 | img_offset = img_request->offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2535 | resid = img_request->length; |
Alex Elder | 4dda41d | 2013-02-20 21:59:33 -0600 | [diff] [blame] | 2536 | rbd_assert(resid > 0); |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2537 | op_type = rbd_img_request_op_type(img_request); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2538 | |
| 2539 | if (type == OBJ_REQUEST_BIO) { |
| 2540 | bio_list = data_desc; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2541 | rbd_assert(img_offset == |
| 2542 | bio_list->bi_iter.bi_sector << SECTOR_SHIFT); |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2543 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2544 | pages = data_desc; |
| 2545 | } |
| 2546 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2547 | while (resid) { |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2548 | struct ceph_osd_request *osd_req; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2549 | const char *object_name; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2550 | u64 offset; |
| 2551 | u64 length; |
| 2552 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2553 | object_name = rbd_segment_name(rbd_dev, img_offset); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2554 | if (!object_name) |
| 2555 | goto out_unwind; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2556 | offset = rbd_segment_offset(rbd_dev, img_offset); |
| 2557 | length = rbd_segment_length(rbd_dev, img_offset, resid); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2558 | obj_request = rbd_obj_request_create(object_name, |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2559 | offset, length, type); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 2560 | /* object request has its own copy of the object name */ |
| 2561 | rbd_segment_name_free(object_name); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2562 | if (!obj_request) |
| 2563 | goto out_unwind; |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2564 | |
Josh Durgin | 03507db | 2013-08-27 14:45:46 -0700 | [diff] [blame] | 2565 | /* |
| 2566 | * set obj_request->img_request before creating the |
| 2567 | * osd_request so that it gets the right snapc |
| 2568 | */ |
| 2569 | rbd_img_obj_request_add(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2570 | |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2571 | if (type == OBJ_REQUEST_BIO) { |
| 2572 | unsigned int clone_size; |
| 2573 | |
| 2574 | rbd_assert(length <= (u64)UINT_MAX); |
| 2575 | clone_size = (unsigned int)length; |
| 2576 | obj_request->bio_list = |
| 2577 | bio_chain_clone_range(&bio_list, |
| 2578 | &bio_offset, |
| 2579 | clone_size, |
David Disseldorp | 2224d87 | 2016-04-05 11:13:39 +0200 | [diff] [blame] | 2580 | GFP_NOIO); |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2581 | if (!obj_request->bio_list) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2582 | goto out_unwind; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 2583 | } else if (type == OBJ_REQUEST_PAGES) { |
Alex Elder | f1a4739 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2584 | unsigned int page_count; |
| 2585 | |
| 2586 | obj_request->pages = pages; |
| 2587 | page_count = (u32)calc_pages_for(offset, length); |
| 2588 | obj_request->page_count = page_count; |
| 2589 | if ((offset + length) & ~PAGE_MASK) |
| 2590 | page_count--; /* more on last page */ |
| 2591 | pages += page_count; |
| 2592 | } |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2593 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 2594 | osd_req = rbd_osd_req_create(rbd_dev, op_type, |
| 2595 | (op_type == OBJ_OP_WRITE) ? 2 : 1, |
| 2596 | obj_request); |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2597 | if (!osd_req) |
Ilya Dryomov | 62054da | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2598 | goto out_unwind; |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2599 | |
Alex Elder | 2fa1232 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2600 | obj_request->osd_req = osd_req; |
Alex Elder | 2169238 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 2601 | obj_request->callback = rbd_img_obj_callback; |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2602 | obj_request->img_offset = img_offset; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2603 | |
Josh Durgin | 3b434a2a | 2014-04-04 17:32:15 -0700 | [diff] [blame] | 2604 | rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0); |
| 2605 | |
Alex Elder | 7da22d2 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 2606 | img_offset += length; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2607 | resid -= length; |
| 2608 | } |
| 2609 | |
| 2610 | return 0; |
| 2611 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2612 | out_unwind: |
| 2613 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) |
Ilya Dryomov | 42dd037 | 2014-03-04 11:57:17 +0200 | [diff] [blame] | 2614 | rbd_img_obj_request_del(img_request, obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 2615 | |
| 2616 | return -ENOMEM; |
| 2617 | } |
| 2618 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2619 | static void |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2620 | rbd_osd_copyup_callback(struct rbd_obj_request *obj_request) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2621 | { |
| 2622 | struct rbd_img_request *img_request; |
| 2623 | struct rbd_device *rbd_dev; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2624 | struct page **pages; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2625 | u32 page_count; |
| 2626 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2627 | dout("%s: obj %p\n", __func__, obj_request); |
| 2628 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2629 | rbd_assert(obj_request->type == OBJ_REQUEST_BIO || |
| 2630 | obj_request->type == OBJ_REQUEST_NODATA); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2631 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2632 | img_request = obj_request->img_request; |
| 2633 | rbd_assert(img_request); |
| 2634 | |
| 2635 | rbd_dev = img_request->rbd_dev; |
| 2636 | rbd_assert(rbd_dev); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2637 | |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2638 | pages = obj_request->copyup_pages; |
| 2639 | rbd_assert(pages != NULL); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2640 | obj_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2641 | page_count = obj_request->copyup_page_count; |
| 2642 | rbd_assert(page_count); |
| 2643 | obj_request->copyup_page_count = 0; |
| 2644 | ceph_release_page_vector(pages, page_count); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2645 | |
| 2646 | /* |
| 2647 | * We want the transfer count to reflect the size of the |
| 2648 | * original write request. There is no such thing as a |
| 2649 | * successful short write, so if the request was successful |
| 2650 | * we can just set it to the originally-requested length. |
| 2651 | */ |
| 2652 | if (!obj_request->result) |
| 2653 | obj_request->xferred = obj_request->length; |
| 2654 | |
Ilya Dryomov | 2761713 | 2015-07-16 17:36:11 +0300 | [diff] [blame] | 2655 | obj_request_done_set(obj_request); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | static void |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2659 | rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request) |
| 2660 | { |
| 2661 | struct rbd_obj_request *orig_request; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2662 | struct ceph_osd_request *osd_req; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2663 | struct rbd_device *rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2664 | struct page **pages; |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2665 | enum obj_operation_type op_type; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2666 | u32 page_count; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2667 | int img_result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2668 | u64 parent_length; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2669 | |
| 2670 | rbd_assert(img_request_child_test(img_request)); |
| 2671 | |
| 2672 | /* First get what we need from the image request */ |
| 2673 | |
| 2674 | pages = img_request->copyup_pages; |
| 2675 | rbd_assert(pages != NULL); |
| 2676 | img_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2677 | page_count = img_request->copyup_page_count; |
| 2678 | rbd_assert(page_count); |
| 2679 | img_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2680 | |
| 2681 | orig_request = img_request->obj_request; |
| 2682 | rbd_assert(orig_request != NULL); |
Alex Elder | b91f09f | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2683 | rbd_assert(obj_request_type_valid(orig_request->type)); |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2684 | img_result = img_request->result; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2685 | parent_length = img_request->length; |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2686 | rbd_assert(img_result || parent_length == img_request->xferred); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2687 | rbd_img_request_put(img_request); |
| 2688 | |
Alex Elder | 91c6feb | 2013-05-06 17:40:32 -0500 | [diff] [blame] | 2689 | rbd_assert(orig_request->img_request); |
| 2690 | rbd_dev = orig_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2691 | rbd_assert(rbd_dev); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2692 | |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2693 | /* |
| 2694 | * If the overlap has become 0 (most likely because the |
| 2695 | * image has been flattened) we need to free the pages |
| 2696 | * and re-submit the original write request. |
| 2697 | */ |
| 2698 | if (!rbd_dev->parent_overlap) { |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2699 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2700 | rbd_obj_request_submit(orig_request); |
| 2701 | return; |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | if (img_result) |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2705 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2706 | |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2707 | /* |
| 2708 | * The original osd request is of no use to use any more. |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2709 | * We need a new one that can hold the three ops in a copyup |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2710 | * request. Allocate the new copyup osd request for the |
| 2711 | * original request, and release the old one. |
| 2712 | */ |
Alex Elder | bbea1c1 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2713 | img_result = -ENOMEM; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2714 | osd_req = rbd_osd_req_create_copyup(orig_request); |
| 2715 | if (!osd_req) |
| 2716 | goto out_err; |
Alex Elder | 8785b1d | 2013-05-09 10:08:49 -0500 | [diff] [blame] | 2717 | rbd_osd_req_destroy(orig_request->osd_req); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2718 | orig_request->osd_req = osd_req; |
| 2719 | orig_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2720 | orig_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2721 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2722 | /* Initialize the copyup op */ |
| 2723 | |
| 2724 | osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup"); |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2725 | osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0, |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2726 | false, false); |
| 2727 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2728 | /* Add the other op(s) */ |
Ilya Dryomov | 0ccd592 | 2014-02-25 16:22:28 +0200 | [diff] [blame] | 2729 | |
Josh Durgin | d3246fb | 2014-04-07 16:49:21 -0700 | [diff] [blame] | 2730 | op_type = rbd_img_request_op_type(orig_request->img_request); |
| 2731 | rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1); |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2732 | |
| 2733 | /* All set, send it off. */ |
| 2734 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2735 | rbd_obj_request_submit(orig_request); |
| 2736 | return; |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2737 | |
Alex Elder | 0eefd47 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2738 | out_err: |
Ilya Dryomov | fa35511 | 2016-09-16 15:20:42 +0200 | [diff] [blame] | 2739 | ceph_release_page_vector(pages, page_count); |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2740 | rbd_obj_request_error(orig_request, img_result); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | /* |
| 2744 | * Read from the parent image the range of data that covers the |
| 2745 | * entire target of the given object request. This is used for |
| 2746 | * satisfying a layered image write request when the target of an |
| 2747 | * object request from the image request does not exist. |
| 2748 | * |
| 2749 | * A page array big enough to hold the returned data is allocated |
| 2750 | * and supplied to rbd_img_request_fill() as the "data descriptor." |
| 2751 | * When the read completes, this page array will be transferred to |
| 2752 | * the original object request for the copyup operation. |
| 2753 | * |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2754 | * If an error occurs, it is recorded as the result of the original |
| 2755 | * object request in rbd_img_obj_exists_callback(). |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2756 | */ |
| 2757 | static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request) |
| 2758 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2759 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2760 | struct rbd_img_request *parent_request = NULL; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2761 | u64 img_offset; |
| 2762 | u64 length; |
| 2763 | struct page **pages = NULL; |
| 2764 | u32 page_count; |
| 2765 | int result; |
| 2766 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2767 | rbd_assert(rbd_dev->parent != NULL); |
| 2768 | |
| 2769 | /* |
| 2770 | * Determine the byte range covered by the object in the |
| 2771 | * child image to which the original request was to be sent. |
| 2772 | */ |
| 2773 | img_offset = obj_request->img_offset - obj_request->offset; |
| 2774 | length = (u64)1 << rbd_dev->header.obj_order; |
| 2775 | |
| 2776 | /* |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 2777 | * There is no defined parent data beyond the parent |
| 2778 | * overlap, so limit what we read at that boundary if |
| 2779 | * necessary. |
| 2780 | */ |
| 2781 | if (img_offset + length > rbd_dev->parent_overlap) { |
| 2782 | rbd_assert(img_offset < rbd_dev->parent_overlap); |
| 2783 | length = rbd_dev->parent_overlap - img_offset; |
| 2784 | } |
| 2785 | |
| 2786 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2787 | * Allocate a page array big enough to receive the data read |
| 2788 | * from the parent. |
| 2789 | */ |
| 2790 | page_count = (u32)calc_pages_for(0, length); |
| 2791 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 2792 | if (IS_ERR(pages)) { |
| 2793 | result = PTR_ERR(pages); |
| 2794 | pages = NULL; |
| 2795 | goto out_err; |
| 2796 | } |
| 2797 | |
| 2798 | result = -ENOMEM; |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 2799 | parent_request = rbd_parent_request_create(obj_request, |
| 2800 | img_offset, length); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2801 | if (!parent_request) |
| 2802 | goto out_err; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2803 | |
| 2804 | result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages); |
| 2805 | if (result) |
| 2806 | goto out_err; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2807 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2808 | parent_request->copyup_pages = pages; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2809 | parent_request->copyup_page_count = page_count; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2810 | parent_request->callback = rbd_img_obj_parent_read_full_callback; |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2811 | |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2812 | result = rbd_img_request_submit(parent_request); |
| 2813 | if (!result) |
| 2814 | return 0; |
| 2815 | |
| 2816 | parent_request->copyup_pages = NULL; |
Alex Elder | ebda640 | 2013-05-10 16:29:22 -0500 | [diff] [blame] | 2817 | parent_request->copyup_page_count = 0; |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2818 | parent_request->obj_request = NULL; |
| 2819 | rbd_obj_request_put(obj_request); |
| 2820 | out_err: |
| 2821 | if (pages) |
| 2822 | ceph_release_page_vector(pages, page_count); |
| 2823 | if (parent_request) |
| 2824 | rbd_img_request_put(parent_request); |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2825 | return result; |
| 2826 | } |
| 2827 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2828 | static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request) |
| 2829 | { |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2830 | struct rbd_obj_request *orig_request; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2831 | struct rbd_device *rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2832 | int result; |
| 2833 | |
| 2834 | rbd_assert(!obj_request_img_data_test(obj_request)); |
| 2835 | |
| 2836 | /* |
| 2837 | * All we need from the object request is the original |
| 2838 | * request and the result of the STAT op. Grab those, then |
| 2839 | * we're done with the request. |
| 2840 | */ |
| 2841 | orig_request = obj_request->obj_request; |
| 2842 | obj_request->obj_request = NULL; |
Alex Elder | 912c317 | 2013-05-13 20:35:38 -0500 | [diff] [blame] | 2843 | rbd_obj_request_put(orig_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2844 | rbd_assert(orig_request); |
| 2845 | rbd_assert(orig_request->img_request); |
| 2846 | |
| 2847 | result = obj_request->result; |
| 2848 | obj_request->result = 0; |
| 2849 | |
| 2850 | dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__, |
| 2851 | obj_request, orig_request, result, |
| 2852 | obj_request->xferred, obj_request->length); |
| 2853 | rbd_obj_request_put(obj_request); |
| 2854 | |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2855 | /* |
| 2856 | * If the overlap has become 0 (most likely because the |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2857 | * image has been flattened) we need to re-submit the |
| 2858 | * original request. |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2859 | */ |
| 2860 | rbd_dev = orig_request->img_request->rbd_dev; |
| 2861 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2862 | rbd_obj_request_submit(orig_request); |
| 2863 | return; |
Alex Elder | 638f5ab | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 2864 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2865 | |
| 2866 | /* |
| 2867 | * Our only purpose here is to determine whether the object |
| 2868 | * exists, and we don't want to treat the non-existence as |
| 2869 | * an error. If something else comes back, transfer the |
| 2870 | * error to the original request and complete it now. |
| 2871 | */ |
| 2872 | if (!result) { |
| 2873 | obj_request_existence_set(orig_request, true); |
| 2874 | } else if (result == -ENOENT) { |
| 2875 | obj_request_existence_set(orig_request, false); |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2876 | } else { |
| 2877 | goto fail_orig_request; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* |
| 2881 | * Resubmit the original request now that we have recorded |
| 2882 | * whether the target object exists. |
| 2883 | */ |
Ilya Dryomov | c2e8241 | 2016-09-13 20:18:01 +0200 | [diff] [blame] | 2884 | result = rbd_img_obj_request_submit(orig_request); |
| 2885 | if (result) |
| 2886 | goto fail_orig_request; |
| 2887 | |
| 2888 | return; |
| 2889 | |
| 2890 | fail_orig_request: |
Ilya Dryomov | 0dcc685 | 2016-09-26 15:43:52 +0200 | [diff] [blame] | 2891 | rbd_obj_request_error(orig_request, result); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request) |
| 2895 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2896 | struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2897 | struct rbd_obj_request *stat_request; |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2898 | struct page **pages; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2899 | u32 page_count; |
| 2900 | size_t size; |
| 2901 | int ret; |
| 2902 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2903 | stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0, |
| 2904 | OBJ_REQUEST_PAGES); |
| 2905 | if (!stat_request) |
| 2906 | return -ENOMEM; |
| 2907 | |
| 2908 | stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
| 2909 | stat_request); |
| 2910 | if (!stat_request->osd_req) { |
| 2911 | ret = -ENOMEM; |
| 2912 | goto fail_stat_request; |
| 2913 | } |
| 2914 | |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2915 | /* |
| 2916 | * The response data for a STAT call consists of: |
| 2917 | * le64 length; |
| 2918 | * struct { |
| 2919 | * le32 tv_sec; |
| 2920 | * le32 tv_nsec; |
| 2921 | * } mtime; |
| 2922 | */ |
| 2923 | size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32); |
| 2924 | page_count = (u32)calc_pages_for(0, size); |
| 2925 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2926 | if (IS_ERR(pages)) { |
| 2927 | ret = PTR_ERR(pages); |
| 2928 | goto fail_stat_request; |
| 2929 | } |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2930 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2931 | osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0); |
| 2932 | osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0, |
| 2933 | false, false); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2934 | |
| 2935 | rbd_obj_request_get(obj_request); |
| 2936 | stat_request->obj_request = obj_request; |
| 2937 | stat_request->pages = pages; |
| 2938 | stat_request->page_count = page_count; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2939 | stat_request->callback = rbd_img_obj_exists_callback; |
| 2940 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2941 | rbd_obj_request_submit(stat_request); |
| 2942 | return 0; |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2943 | |
Ilya Dryomov | 710214e | 2016-09-15 17:53:32 +0200 | [diff] [blame] | 2944 | fail_stat_request: |
| 2945 | rbd_obj_request_put(stat_request); |
Alex Elder | c5b5ef6 | 2013-02-11 12:33:24 -0600 | [diff] [blame] | 2946 | return ret; |
| 2947 | } |
| 2948 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2949 | static bool img_obj_request_simple(struct rbd_obj_request *obj_request) |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2950 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2951 | struct rbd_img_request *img_request = obj_request->img_request; |
| 2952 | struct rbd_device *rbd_dev = img_request->rbd_dev; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2953 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2954 | /* Reads */ |
Josh Durgin | 1c22088 | 2014-04-04 17:49:12 -0700 | [diff] [blame] | 2955 | if (!img_request_write_test(img_request) && |
| 2956 | !img_request_discard_test(img_request)) |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2957 | return true; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2958 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2959 | /* Non-layered writes */ |
| 2960 | if (!img_request_layered_test(img_request)) |
| 2961 | return true; |
| 2962 | |
| 2963 | /* |
| 2964 | * Layered writes outside of the parent overlap range don't |
| 2965 | * share any data with the parent. |
| 2966 | */ |
| 2967 | if (!obj_request_overlaps_parent(obj_request)) |
| 2968 | return true; |
| 2969 | |
| 2970 | /* |
Guangliang Zhao | c622d22 | 2014-04-01 22:22:15 +0800 | [diff] [blame] | 2971 | * Entire-object layered writes - we will overwrite whatever |
| 2972 | * parent data there is anyway. |
| 2973 | */ |
| 2974 | if (!obj_request->offset && |
| 2975 | obj_request->length == rbd_obj_bytes(&rbd_dev->header)) |
| 2976 | return true; |
| 2977 | |
| 2978 | /* |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2979 | * If the object is known to already exist, its parent data has |
| 2980 | * already been copied. |
| 2981 | */ |
| 2982 | if (obj_request_known_test(obj_request) && |
| 2983 | obj_request_exists_test(obj_request)) |
| 2984 | return true; |
| 2985 | |
| 2986 | return false; |
| 2987 | } |
| 2988 | |
| 2989 | static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request) |
| 2990 | { |
Ilya Dryomov | 058aa99 | 2016-09-12 14:44:45 +0200 | [diff] [blame] | 2991 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 2992 | rbd_assert(obj_request_type_valid(obj_request->type)); |
| 2993 | rbd_assert(obj_request->img_request); |
| 2994 | |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 2995 | if (img_obj_request_simple(obj_request)) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 2996 | rbd_obj_request_submit(obj_request); |
| 2997 | return 0; |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | /* |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3001 | * It's a layered write. The target object might exist but |
| 3002 | * we may not know that yet. If we know it doesn't exist, |
| 3003 | * start by reading the data for the full target object from |
| 3004 | * the parent so we can use it for a copyup to the target. |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3005 | */ |
Ilya Dryomov | 70d045f | 2014-09-12 16:02:01 +0400 | [diff] [blame] | 3006 | if (obj_request_known_test(obj_request)) |
Alex Elder | 3d7efd1 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3007 | return rbd_img_obj_parent_read_full(obj_request); |
| 3008 | |
| 3009 | /* We don't know whether the target exists. Go find out. */ |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3010 | |
| 3011 | return rbd_img_obj_exists_submit(obj_request); |
| 3012 | } |
| 3013 | |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3014 | static int rbd_img_request_submit(struct rbd_img_request *img_request) |
| 3015 | { |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3016 | struct rbd_obj_request *obj_request; |
Alex Elder | 46faeed | 2013-04-10 17:47:46 -0500 | [diff] [blame] | 3017 | struct rbd_obj_request *next_obj_request; |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3018 | int ret = 0; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3019 | |
Alex Elder | 37206ee | 2013-02-20 17:32:08 -0600 | [diff] [blame] | 3020 | dout("%s: img %p\n", __func__, img_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3021 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3022 | rbd_img_request_get(img_request); |
| 3023 | for_each_obj_request_safe(img_request, obj_request, next_obj_request) { |
Alex Elder | b454e36 | 2013-04-19 15:34:50 -0500 | [diff] [blame] | 3024 | ret = rbd_img_obj_request_submit(obj_request); |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3025 | if (ret) |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3026 | goto out_put_ireq; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3027 | } |
| 3028 | |
Ilya Dryomov | 663ae2c | 2016-05-16 13:18:57 +0200 | [diff] [blame] | 3029 | out_put_ireq: |
| 3030 | rbd_img_request_put(img_request); |
| 3031 | return ret; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 3032 | } |
| 3033 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3034 | static void rbd_img_parent_read_callback(struct rbd_img_request *img_request) |
| 3035 | { |
| 3036 | struct rbd_obj_request *obj_request; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3037 | struct rbd_device *rbd_dev; |
| 3038 | u64 obj_end; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3039 | u64 img_xferred; |
| 3040 | int img_result; |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3041 | |
| 3042 | rbd_assert(img_request_child_test(img_request)); |
| 3043 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3044 | /* First get what we need from the image request and release it */ |
| 3045 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3046 | obj_request = img_request->obj_request; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3047 | img_xferred = img_request->xferred; |
| 3048 | img_result = img_request->result; |
| 3049 | rbd_img_request_put(img_request); |
| 3050 | |
| 3051 | /* |
| 3052 | * If the overlap has become 0 (most likely because the |
| 3053 | * image has been flattened) we need to re-submit the |
| 3054 | * original request. |
| 3055 | */ |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3056 | rbd_assert(obj_request); |
| 3057 | rbd_assert(obj_request->img_request); |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3058 | rbd_dev = obj_request->img_request->rbd_dev; |
| 3059 | if (!rbd_dev->parent_overlap) { |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 3060 | rbd_obj_request_submit(obj_request); |
| 3061 | return; |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | obj_request->result = img_result; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3065 | if (obj_request->result) |
| 3066 | goto out; |
| 3067 | |
| 3068 | /* |
| 3069 | * We need to zero anything beyond the parent overlap |
| 3070 | * boundary. Since rbd_img_obj_request_read_callback() |
| 3071 | * will zero anything beyond the end of a short read, an |
| 3072 | * easy way to do this is to pretend the data from the |
| 3073 | * parent came up short--ending at the overlap boundary. |
| 3074 | */ |
| 3075 | rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length); |
| 3076 | obj_end = obj_request->img_offset + obj_request->length; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3077 | if (obj_end > rbd_dev->parent_overlap) { |
| 3078 | u64 xferred = 0; |
| 3079 | |
| 3080 | if (obj_request->img_offset < rbd_dev->parent_overlap) |
| 3081 | xferred = rbd_dev->parent_overlap - |
| 3082 | obj_request->img_offset; |
| 3083 | |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3084 | obj_request->xferred = min(img_xferred, xferred); |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3085 | } else { |
Alex Elder | 02c74fb | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3086 | obj_request->xferred = img_xferred; |
Alex Elder | a9e8ba2c | 2013-04-21 00:32:07 -0500 | [diff] [blame] | 3087 | } |
| 3088 | out: |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3089 | rbd_img_obj_request_read_callback(obj_request); |
| 3090 | rbd_obj_request_complete(obj_request); |
| 3091 | } |
| 3092 | |
| 3093 | static void rbd_img_parent_read(struct rbd_obj_request *obj_request) |
| 3094 | { |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3095 | struct rbd_img_request *img_request; |
| 3096 | int result; |
| 3097 | |
| 3098 | rbd_assert(obj_request_img_data_test(obj_request)); |
| 3099 | rbd_assert(obj_request->img_request != NULL); |
| 3100 | rbd_assert(obj_request->result == (s32) -ENOENT); |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3101 | rbd_assert(obj_request_type_valid(obj_request->type)); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3102 | |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3103 | /* rbd_read_finish(obj_request, obj_request->length); */ |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3104 | img_request = rbd_parent_request_create(obj_request, |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3105 | obj_request->img_offset, |
Alex Elder | e93f315 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 3106 | obj_request->length); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3107 | result = -ENOMEM; |
| 3108 | if (!img_request) |
| 3109 | goto out_err; |
| 3110 | |
Alex Elder | 5b2ab72 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 3111 | if (obj_request->type == OBJ_REQUEST_BIO) |
| 3112 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 3113 | obj_request->bio_list); |
| 3114 | else |
| 3115 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES, |
| 3116 | obj_request->pages); |
Alex Elder | 8b3e1a5 | 2013-01-24 16:13:36 -0600 | [diff] [blame] | 3117 | if (result) |
| 3118 | goto out_err; |
| 3119 | |
| 3120 | img_request->callback = rbd_img_parent_read_callback; |
| 3121 | result = rbd_img_request_submit(img_request); |
| 3122 | if (result) |
| 3123 | goto out_err; |
| 3124 | |
| 3125 | return; |
| 3126 | out_err: |
| 3127 | if (img_request) |
| 3128 | rbd_img_request_put(img_request); |
| 3129 | obj_request->result = result; |
| 3130 | obj_request->xferred = 0; |
| 3131 | obj_request_done_set(obj_request); |
| 3132 | } |
| 3133 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3134 | static const struct rbd_client_id rbd_empty_cid; |
| 3135 | |
| 3136 | static bool rbd_cid_equal(const struct rbd_client_id *lhs, |
| 3137 | const struct rbd_client_id *rhs) |
| 3138 | { |
| 3139 | return lhs->gid == rhs->gid && lhs->handle == rhs->handle; |
| 3140 | } |
| 3141 | |
| 3142 | static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev) |
| 3143 | { |
| 3144 | struct rbd_client_id cid; |
| 3145 | |
| 3146 | mutex_lock(&rbd_dev->watch_mutex); |
| 3147 | cid.gid = ceph_client_gid(rbd_dev->rbd_client->client); |
| 3148 | cid.handle = rbd_dev->watch_cookie; |
| 3149 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3150 | return cid; |
| 3151 | } |
| 3152 | |
| 3153 | /* |
| 3154 | * lock_rwsem must be held for write |
| 3155 | */ |
| 3156 | static void rbd_set_owner_cid(struct rbd_device *rbd_dev, |
| 3157 | const struct rbd_client_id *cid) |
| 3158 | { |
| 3159 | dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev, |
| 3160 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle, |
| 3161 | cid->gid, cid->handle); |
| 3162 | rbd_dev->owner_cid = *cid; /* struct */ |
| 3163 | } |
| 3164 | |
| 3165 | static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf) |
| 3166 | { |
| 3167 | mutex_lock(&rbd_dev->watch_mutex); |
| 3168 | sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie); |
| 3169 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | * lock_rwsem must be held for write |
| 3174 | */ |
| 3175 | static int rbd_lock(struct rbd_device *rbd_dev) |
| 3176 | { |
| 3177 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3178 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3179 | char cookie[32]; |
| 3180 | int ret; |
| 3181 | |
| 3182 | WARN_ON(__rbd_is_lock_owner(rbd_dev)); |
| 3183 | |
| 3184 | format_lock_cookie(rbd_dev, cookie); |
| 3185 | ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3186 | RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie, |
| 3187 | RBD_LOCK_TAG, "", 0); |
| 3188 | if (ret) |
| 3189 | return ret; |
| 3190 | |
| 3191 | rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED; |
| 3192 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3193 | queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work); |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
| 3197 | /* |
| 3198 | * lock_rwsem must be held for write |
| 3199 | */ |
| 3200 | static int rbd_unlock(struct rbd_device *rbd_dev) |
| 3201 | { |
| 3202 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3203 | char cookie[32]; |
| 3204 | int ret; |
| 3205 | |
| 3206 | WARN_ON(!__rbd_is_lock_owner(rbd_dev)); |
| 3207 | |
| 3208 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 3209 | |
| 3210 | format_lock_cookie(rbd_dev, cookie); |
| 3211 | ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc, |
| 3212 | RBD_LOCK_NAME, cookie); |
| 3213 | if (ret && ret != -ENOENT) { |
| 3214 | rbd_warn(rbd_dev, "cls_unlock failed: %d", ret); |
| 3215 | return ret; |
| 3216 | } |
| 3217 | |
| 3218 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3219 | queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work); |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
| 3223 | static int __rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3224 | enum rbd_notify_op notify_op, |
| 3225 | struct page ***preply_pages, |
| 3226 | size_t *preply_len) |
| 3227 | { |
| 3228 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3229 | struct rbd_client_id cid = rbd_get_cid(rbd_dev); |
| 3230 | int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN; |
| 3231 | char buf[buf_size]; |
| 3232 | void *p = buf; |
| 3233 | |
| 3234 | dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op); |
| 3235 | |
| 3236 | /* encode *LockPayload NotifyMessage (op + ClientId) */ |
| 3237 | ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3238 | ceph_encode_32(&p, notify_op); |
| 3239 | ceph_encode_64(&p, cid.gid); |
| 3240 | ceph_encode_64(&p, cid.handle); |
| 3241 | |
| 3242 | return ceph_osdc_notify(osdc, &rbd_dev->header_oid, |
| 3243 | &rbd_dev->header_oloc, buf, buf_size, |
| 3244 | RBD_NOTIFY_TIMEOUT, preply_pages, preply_len); |
| 3245 | } |
| 3246 | |
| 3247 | static void rbd_notify_op_lock(struct rbd_device *rbd_dev, |
| 3248 | enum rbd_notify_op notify_op) |
| 3249 | { |
| 3250 | struct page **reply_pages; |
| 3251 | size_t reply_len; |
| 3252 | |
| 3253 | __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len); |
| 3254 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3255 | } |
| 3256 | |
| 3257 | static void rbd_notify_acquired_lock(struct work_struct *work) |
| 3258 | { |
| 3259 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3260 | acquired_lock_work); |
| 3261 | |
| 3262 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK); |
| 3263 | } |
| 3264 | |
| 3265 | static void rbd_notify_released_lock(struct work_struct *work) |
| 3266 | { |
| 3267 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3268 | released_lock_work); |
| 3269 | |
| 3270 | rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK); |
| 3271 | } |
| 3272 | |
| 3273 | static int rbd_request_lock(struct rbd_device *rbd_dev) |
| 3274 | { |
| 3275 | struct page **reply_pages; |
| 3276 | size_t reply_len; |
| 3277 | bool lock_owner_responded = false; |
| 3278 | int ret; |
| 3279 | |
| 3280 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3281 | |
| 3282 | ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK, |
| 3283 | &reply_pages, &reply_len); |
| 3284 | if (ret && ret != -ETIMEDOUT) { |
| 3285 | rbd_warn(rbd_dev, "failed to request lock: %d", ret); |
| 3286 | goto out; |
| 3287 | } |
| 3288 | |
| 3289 | if (reply_len > 0 && reply_len <= PAGE_SIZE) { |
| 3290 | void *p = page_address(reply_pages[0]); |
| 3291 | void *const end = p + reply_len; |
| 3292 | u32 n; |
| 3293 | |
| 3294 | ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */ |
| 3295 | while (n--) { |
| 3296 | u8 struct_v; |
| 3297 | u32 len; |
| 3298 | |
| 3299 | ceph_decode_need(&p, end, 8 + 8, e_inval); |
| 3300 | p += 8 + 8; /* skip gid and cookie */ |
| 3301 | |
| 3302 | ceph_decode_32_safe(&p, end, len, e_inval); |
| 3303 | if (!len) |
| 3304 | continue; |
| 3305 | |
| 3306 | if (lock_owner_responded) { |
| 3307 | rbd_warn(rbd_dev, |
| 3308 | "duplicate lock owners detected"); |
| 3309 | ret = -EIO; |
| 3310 | goto out; |
| 3311 | } |
| 3312 | |
| 3313 | lock_owner_responded = true; |
| 3314 | ret = ceph_start_decoding(&p, end, 1, "ResponseMessage", |
| 3315 | &struct_v, &len); |
| 3316 | if (ret) { |
| 3317 | rbd_warn(rbd_dev, |
| 3318 | "failed to decode ResponseMessage: %d", |
| 3319 | ret); |
| 3320 | goto e_inval; |
| 3321 | } |
| 3322 | |
| 3323 | ret = ceph_decode_32(&p); |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | if (!lock_owner_responded) { |
| 3328 | rbd_warn(rbd_dev, "no lock owners detected"); |
| 3329 | ret = -ETIMEDOUT; |
| 3330 | } |
| 3331 | |
| 3332 | out: |
| 3333 | ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len)); |
| 3334 | return ret; |
| 3335 | |
| 3336 | e_inval: |
| 3337 | ret = -EINVAL; |
| 3338 | goto out; |
| 3339 | } |
| 3340 | |
| 3341 | static void wake_requests(struct rbd_device *rbd_dev, bool wake_all) |
| 3342 | { |
| 3343 | dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all); |
| 3344 | |
| 3345 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3346 | if (wake_all) |
| 3347 | wake_up_all(&rbd_dev->lock_waitq); |
| 3348 | else |
| 3349 | wake_up(&rbd_dev->lock_waitq); |
| 3350 | } |
| 3351 | |
| 3352 | static int get_lock_owner_info(struct rbd_device *rbd_dev, |
| 3353 | struct ceph_locker **lockers, u32 *num_lockers) |
| 3354 | { |
| 3355 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3356 | u8 lock_type; |
| 3357 | char *lock_tag; |
| 3358 | int ret; |
| 3359 | |
| 3360 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3361 | |
| 3362 | ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid, |
| 3363 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3364 | &lock_type, &lock_tag, lockers, num_lockers); |
| 3365 | if (ret) |
| 3366 | return ret; |
| 3367 | |
| 3368 | if (*num_lockers == 0) { |
| 3369 | dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev); |
| 3370 | goto out; |
| 3371 | } |
| 3372 | |
| 3373 | if (strcmp(lock_tag, RBD_LOCK_TAG)) { |
| 3374 | rbd_warn(rbd_dev, "locked by external mechanism, tag %s", |
| 3375 | lock_tag); |
| 3376 | ret = -EBUSY; |
| 3377 | goto out; |
| 3378 | } |
| 3379 | |
| 3380 | if (lock_type == CEPH_CLS_LOCK_SHARED) { |
| 3381 | rbd_warn(rbd_dev, "shared lock type detected"); |
| 3382 | ret = -EBUSY; |
| 3383 | goto out; |
| 3384 | } |
| 3385 | |
| 3386 | if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX, |
| 3387 | strlen(RBD_LOCK_COOKIE_PREFIX))) { |
| 3388 | rbd_warn(rbd_dev, "locked by external mechanism, cookie %s", |
| 3389 | (*lockers)[0].id.cookie); |
| 3390 | ret = -EBUSY; |
| 3391 | goto out; |
| 3392 | } |
| 3393 | |
| 3394 | out: |
| 3395 | kfree(lock_tag); |
| 3396 | return ret; |
| 3397 | } |
| 3398 | |
| 3399 | static int find_watcher(struct rbd_device *rbd_dev, |
| 3400 | const struct ceph_locker *locker) |
| 3401 | { |
| 3402 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3403 | struct ceph_watch_item *watchers; |
| 3404 | u32 num_watchers; |
| 3405 | u64 cookie; |
| 3406 | int i; |
| 3407 | int ret; |
| 3408 | |
| 3409 | ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid, |
| 3410 | &rbd_dev->header_oloc, &watchers, |
| 3411 | &num_watchers); |
| 3412 | if (ret) |
| 3413 | return ret; |
| 3414 | |
| 3415 | sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie); |
| 3416 | for (i = 0; i < num_watchers; i++) { |
| 3417 | if (!memcmp(&watchers[i].addr, &locker->info.addr, |
| 3418 | sizeof(locker->info.addr)) && |
| 3419 | watchers[i].cookie == cookie) { |
| 3420 | struct rbd_client_id cid = { |
| 3421 | .gid = le64_to_cpu(watchers[i].name.num), |
| 3422 | .handle = cookie, |
| 3423 | }; |
| 3424 | |
| 3425 | dout("%s rbd_dev %p found cid %llu-%llu\n", __func__, |
| 3426 | rbd_dev, cid.gid, cid.handle); |
| 3427 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3428 | ret = 1; |
| 3429 | goto out; |
| 3430 | } |
| 3431 | } |
| 3432 | |
| 3433 | dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev); |
| 3434 | ret = 0; |
| 3435 | out: |
| 3436 | kfree(watchers); |
| 3437 | return ret; |
| 3438 | } |
| 3439 | |
| 3440 | /* |
| 3441 | * lock_rwsem must be held for write |
| 3442 | */ |
| 3443 | static int rbd_try_lock(struct rbd_device *rbd_dev) |
| 3444 | { |
| 3445 | struct ceph_client *client = rbd_dev->rbd_client->client; |
| 3446 | struct ceph_locker *lockers; |
| 3447 | u32 num_lockers; |
| 3448 | int ret; |
| 3449 | |
| 3450 | for (;;) { |
| 3451 | ret = rbd_lock(rbd_dev); |
| 3452 | if (ret != -EBUSY) |
| 3453 | return ret; |
| 3454 | |
| 3455 | /* determine if the current lock holder is still alive */ |
| 3456 | ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers); |
| 3457 | if (ret) |
| 3458 | return ret; |
| 3459 | |
| 3460 | if (num_lockers == 0) |
| 3461 | goto again; |
| 3462 | |
| 3463 | ret = find_watcher(rbd_dev, lockers); |
| 3464 | if (ret) { |
| 3465 | if (ret > 0) |
| 3466 | ret = 0; /* have to request lock */ |
| 3467 | goto out; |
| 3468 | } |
| 3469 | |
| 3470 | rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock", |
| 3471 | ENTITY_NAME(lockers[0].id.name)); |
| 3472 | |
| 3473 | ret = ceph_monc_blacklist_add(&client->monc, |
| 3474 | &lockers[0].info.addr); |
| 3475 | if (ret) { |
| 3476 | rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d", |
| 3477 | ENTITY_NAME(lockers[0].id.name), ret); |
| 3478 | goto out; |
| 3479 | } |
| 3480 | |
| 3481 | ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid, |
| 3482 | &rbd_dev->header_oloc, RBD_LOCK_NAME, |
| 3483 | lockers[0].id.cookie, |
| 3484 | &lockers[0].id.name); |
| 3485 | if (ret && ret != -ENOENT) |
| 3486 | goto out; |
| 3487 | |
| 3488 | again: |
| 3489 | ceph_free_lockers(lockers, num_lockers); |
| 3490 | } |
| 3491 | |
| 3492 | out: |
| 3493 | ceph_free_lockers(lockers, num_lockers); |
| 3494 | return ret; |
| 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED |
| 3499 | */ |
| 3500 | static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev, |
| 3501 | int *pret) |
| 3502 | { |
| 3503 | enum rbd_lock_state lock_state; |
| 3504 | |
| 3505 | down_read(&rbd_dev->lock_rwsem); |
| 3506 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3507 | rbd_dev->lock_state); |
| 3508 | if (__rbd_is_lock_owner(rbd_dev)) { |
| 3509 | lock_state = rbd_dev->lock_state; |
| 3510 | up_read(&rbd_dev->lock_rwsem); |
| 3511 | return lock_state; |
| 3512 | } |
| 3513 | |
| 3514 | up_read(&rbd_dev->lock_rwsem); |
| 3515 | down_write(&rbd_dev->lock_rwsem); |
| 3516 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3517 | rbd_dev->lock_state); |
| 3518 | if (!__rbd_is_lock_owner(rbd_dev)) { |
| 3519 | *pret = rbd_try_lock(rbd_dev); |
| 3520 | if (*pret) |
| 3521 | rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret); |
| 3522 | } |
| 3523 | |
| 3524 | lock_state = rbd_dev->lock_state; |
| 3525 | up_write(&rbd_dev->lock_rwsem); |
| 3526 | return lock_state; |
| 3527 | } |
| 3528 | |
| 3529 | static void rbd_acquire_lock(struct work_struct *work) |
| 3530 | { |
| 3531 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3532 | struct rbd_device, lock_dwork); |
| 3533 | enum rbd_lock_state lock_state; |
| 3534 | int ret; |
| 3535 | |
| 3536 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3537 | again: |
| 3538 | lock_state = rbd_try_acquire_lock(rbd_dev, &ret); |
| 3539 | if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) { |
| 3540 | if (lock_state == RBD_LOCK_STATE_LOCKED) |
| 3541 | wake_requests(rbd_dev, true); |
| 3542 | dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__, |
| 3543 | rbd_dev, lock_state, ret); |
| 3544 | return; |
| 3545 | } |
| 3546 | |
| 3547 | ret = rbd_request_lock(rbd_dev); |
| 3548 | if (ret == -ETIMEDOUT) { |
| 3549 | goto again; /* treat this as a dead client */ |
| 3550 | } else if (ret < 0) { |
| 3551 | rbd_warn(rbd_dev, "error requesting lock: %d", ret); |
| 3552 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3553 | RBD_RETRY_DELAY); |
| 3554 | } else { |
| 3555 | /* |
| 3556 | * lock owner acked, but resend if we don't see them |
| 3557 | * release the lock |
| 3558 | */ |
| 3559 | dout("%s rbd_dev %p requeueing lock_dwork\n", __func__, |
| 3560 | rbd_dev); |
| 3561 | mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, |
| 3562 | msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC)); |
| 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | /* |
| 3567 | * lock_rwsem must be held for write |
| 3568 | */ |
| 3569 | static bool rbd_release_lock(struct rbd_device *rbd_dev) |
| 3570 | { |
| 3571 | dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev, |
| 3572 | rbd_dev->lock_state); |
| 3573 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 3574 | return false; |
| 3575 | |
| 3576 | rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING; |
| 3577 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3578 | /* |
| 3579 | * Ensure that all in-flight IO is flushed. |
| 3580 | * |
| 3581 | * FIXME: ceph_osdc_sync() flushes the entire OSD client, which |
| 3582 | * may be shared with other devices. |
| 3583 | */ |
| 3584 | ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc); |
| 3585 | up_read(&rbd_dev->lock_rwsem); |
| 3586 | |
| 3587 | down_write(&rbd_dev->lock_rwsem); |
| 3588 | dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev, |
| 3589 | rbd_dev->lock_state); |
| 3590 | if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING) |
| 3591 | return false; |
| 3592 | |
| 3593 | if (!rbd_unlock(rbd_dev)) |
| 3594 | /* |
| 3595 | * Give others a chance to grab the lock - we would re-acquire |
| 3596 | * almost immediately if we got new IO during ceph_osdc_sync() |
| 3597 | * otherwise. We need to ack our own notifications, so this |
| 3598 | * lock_dwork will be requeued from rbd_wait_state_locked() |
| 3599 | * after wake_requests() in rbd_handle_released_lock(). |
| 3600 | */ |
| 3601 | cancel_delayed_work(&rbd_dev->lock_dwork); |
| 3602 | |
| 3603 | return true; |
| 3604 | } |
| 3605 | |
| 3606 | static void rbd_release_lock_work(struct work_struct *work) |
| 3607 | { |
| 3608 | struct rbd_device *rbd_dev = container_of(work, struct rbd_device, |
| 3609 | unlock_work); |
| 3610 | |
| 3611 | down_write(&rbd_dev->lock_rwsem); |
| 3612 | rbd_release_lock(rbd_dev); |
| 3613 | up_write(&rbd_dev->lock_rwsem); |
| 3614 | } |
| 3615 | |
| 3616 | static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3617 | void **p) |
| 3618 | { |
| 3619 | struct rbd_client_id cid = { 0 }; |
| 3620 | |
| 3621 | if (struct_v >= 2) { |
| 3622 | cid.gid = ceph_decode_64(p); |
| 3623 | cid.handle = ceph_decode_64(p); |
| 3624 | } |
| 3625 | |
| 3626 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3627 | cid.handle); |
| 3628 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3629 | down_write(&rbd_dev->lock_rwsem); |
| 3630 | if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3631 | /* |
| 3632 | * we already know that the remote client is |
| 3633 | * the owner |
| 3634 | */ |
| 3635 | up_write(&rbd_dev->lock_rwsem); |
| 3636 | return; |
| 3637 | } |
| 3638 | |
| 3639 | rbd_set_owner_cid(rbd_dev, &cid); |
| 3640 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3641 | } else { |
| 3642 | down_read(&rbd_dev->lock_rwsem); |
| 3643 | } |
| 3644 | |
| 3645 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3646 | wake_requests(rbd_dev, false); |
| 3647 | up_read(&rbd_dev->lock_rwsem); |
| 3648 | } |
| 3649 | |
| 3650 | static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3651 | void **p) |
| 3652 | { |
| 3653 | struct rbd_client_id cid = { 0 }; |
| 3654 | |
| 3655 | if (struct_v >= 2) { |
| 3656 | cid.gid = ceph_decode_64(p); |
| 3657 | cid.handle = ceph_decode_64(p); |
| 3658 | } |
| 3659 | |
| 3660 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3661 | cid.handle); |
| 3662 | if (!rbd_cid_equal(&cid, &rbd_empty_cid)) { |
| 3663 | down_write(&rbd_dev->lock_rwsem); |
| 3664 | if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) { |
| 3665 | dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n", |
| 3666 | __func__, rbd_dev, cid.gid, cid.handle, |
| 3667 | rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle); |
| 3668 | up_write(&rbd_dev->lock_rwsem); |
| 3669 | return; |
| 3670 | } |
| 3671 | |
| 3672 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3673 | downgrade_write(&rbd_dev->lock_rwsem); |
| 3674 | } else { |
| 3675 | down_read(&rbd_dev->lock_rwsem); |
| 3676 | } |
| 3677 | |
| 3678 | if (!__rbd_is_lock_owner(rbd_dev)) |
| 3679 | wake_requests(rbd_dev, false); |
| 3680 | up_read(&rbd_dev->lock_rwsem); |
| 3681 | } |
| 3682 | |
| 3683 | static bool rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v, |
| 3684 | void **p) |
| 3685 | { |
| 3686 | struct rbd_client_id my_cid = rbd_get_cid(rbd_dev); |
| 3687 | struct rbd_client_id cid = { 0 }; |
| 3688 | bool need_to_send; |
| 3689 | |
| 3690 | if (struct_v >= 2) { |
| 3691 | cid.gid = ceph_decode_64(p); |
| 3692 | cid.handle = ceph_decode_64(p); |
| 3693 | } |
| 3694 | |
| 3695 | dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid, |
| 3696 | cid.handle); |
| 3697 | if (rbd_cid_equal(&cid, &my_cid)) |
| 3698 | return false; |
| 3699 | |
| 3700 | down_read(&rbd_dev->lock_rwsem); |
| 3701 | need_to_send = __rbd_is_lock_owner(rbd_dev); |
| 3702 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) { |
| 3703 | if (!rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid)) { |
| 3704 | dout("%s rbd_dev %p queueing unlock_work\n", __func__, |
| 3705 | rbd_dev); |
| 3706 | queue_work(rbd_dev->task_wq, &rbd_dev->unlock_work); |
| 3707 | } |
| 3708 | } |
| 3709 | up_read(&rbd_dev->lock_rwsem); |
| 3710 | return need_to_send; |
| 3711 | } |
| 3712 | |
| 3713 | static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev, |
| 3714 | u64 notify_id, u64 cookie, s32 *result) |
| 3715 | { |
| 3716 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3717 | int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN; |
| 3718 | char buf[buf_size]; |
| 3719 | int ret; |
| 3720 | |
| 3721 | if (result) { |
| 3722 | void *p = buf; |
| 3723 | |
| 3724 | /* encode ResponseMessage */ |
| 3725 | ceph_start_encoding(&p, 1, 1, |
| 3726 | buf_size - CEPH_ENCODING_START_BLK_LEN); |
| 3727 | ceph_encode_32(&p, *result); |
| 3728 | } else { |
| 3729 | buf_size = 0; |
| 3730 | } |
| 3731 | |
| 3732 | ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid, |
| 3733 | &rbd_dev->header_oloc, notify_id, cookie, |
| 3734 | buf, buf_size); |
| 3735 | if (ret) |
| 3736 | rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret); |
| 3737 | } |
| 3738 | |
| 3739 | static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id, |
| 3740 | u64 cookie) |
| 3741 | { |
| 3742 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3743 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL); |
| 3744 | } |
| 3745 | |
| 3746 | static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev, |
| 3747 | u64 notify_id, u64 cookie, s32 result) |
| 3748 | { |
| 3749 | dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result); |
| 3750 | __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result); |
| 3751 | } |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3752 | |
| 3753 | static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie, |
| 3754 | u64 notifier_id, void *data, size_t data_len) |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3755 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3756 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3757 | void *p = data; |
| 3758 | void *const end = p + data_len; |
| 3759 | u8 struct_v; |
| 3760 | u32 len; |
| 3761 | u32 notify_op; |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3762 | int ret; |
| 3763 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3764 | dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n", |
| 3765 | __func__, rbd_dev, cookie, notify_id, data_len); |
| 3766 | if (data_len) { |
| 3767 | ret = ceph_start_decoding(&p, end, 1, "NotifyMessage", |
| 3768 | &struct_v, &len); |
| 3769 | if (ret) { |
| 3770 | rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d", |
| 3771 | ret); |
| 3772 | return; |
| 3773 | } |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 3774 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3775 | notify_op = ceph_decode_32(&p); |
| 3776 | } else { |
| 3777 | /* legacy notification for header updates */ |
| 3778 | notify_op = RBD_NOTIFY_OP_HEADER_UPDATE; |
| 3779 | len = 0; |
| 3780 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3781 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3782 | dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op); |
| 3783 | switch (notify_op) { |
| 3784 | case RBD_NOTIFY_OP_ACQUIRED_LOCK: |
| 3785 | rbd_handle_acquired_lock(rbd_dev, struct_v, &p); |
| 3786 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3787 | break; |
| 3788 | case RBD_NOTIFY_OP_RELEASED_LOCK: |
| 3789 | rbd_handle_released_lock(rbd_dev, struct_v, &p); |
| 3790 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3791 | break; |
| 3792 | case RBD_NOTIFY_OP_REQUEST_LOCK: |
| 3793 | if (rbd_handle_request_lock(rbd_dev, struct_v, &p)) |
| 3794 | /* |
| 3795 | * send ResponseMessage(0) back so the client |
| 3796 | * can detect a missing owner |
| 3797 | */ |
| 3798 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3799 | cookie, 0); |
| 3800 | else |
| 3801 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3802 | break; |
| 3803 | case RBD_NOTIFY_OP_HEADER_UPDATE: |
| 3804 | ret = rbd_dev_refresh(rbd_dev); |
| 3805 | if (ret) |
| 3806 | rbd_warn(rbd_dev, "refresh failed: %d", ret); |
| 3807 | |
| 3808 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3809 | break; |
| 3810 | default: |
| 3811 | if (rbd_is_lock_owner(rbd_dev)) |
| 3812 | rbd_acknowledge_notify_result(rbd_dev, notify_id, |
| 3813 | cookie, -EOPNOTSUPP); |
| 3814 | else |
| 3815 | rbd_acknowledge_notify(rbd_dev, notify_id, cookie); |
| 3816 | break; |
| 3817 | } |
Alex Elder | b8d7003 | 2012-11-30 17:53:04 -0600 | [diff] [blame] | 3818 | } |
| 3819 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3820 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev); |
| 3821 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3822 | static void rbd_watch_errcb(void *arg, u64 cookie, int err) |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3823 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3824 | struct rbd_device *rbd_dev = arg; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3825 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3826 | rbd_warn(rbd_dev, "encountered watch error: %d", err); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3827 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3828 | down_write(&rbd_dev->lock_rwsem); |
| 3829 | rbd_set_owner_cid(rbd_dev, &rbd_empty_cid); |
| 3830 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3831 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3832 | mutex_lock(&rbd_dev->watch_mutex); |
| 3833 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) { |
| 3834 | __rbd_unregister_watch(rbd_dev); |
| 3835 | rbd_dev->watch_state = RBD_WATCH_STATE_ERROR; |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3836 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3837 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3838 | } |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3839 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | bb040aa | 2014-06-19 11:38:14 +0400 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3843 | * watch_mutex must be locked |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3844 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3845 | static int __rbd_register_watch(struct rbd_device *rbd_dev) |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3846 | { |
| 3847 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3848 | struct ceph_osd_linger_request *handle; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3849 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3850 | rbd_assert(!rbd_dev->watch_handle); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3851 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3852 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3853 | handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid, |
| 3854 | &rbd_dev->header_oloc, rbd_watch_cb, |
| 3855 | rbd_watch_errcb, rbd_dev); |
| 3856 | if (IS_ERR(handle)) |
| 3857 | return PTR_ERR(handle); |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3858 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3859 | rbd_dev->watch_handle = handle; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3860 | return 0; |
Alex Elder | 9969ebc | 2013-01-18 12:31:10 -0600 | [diff] [blame] | 3861 | } |
| 3862 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3863 | /* |
| 3864 | * watch_mutex must be locked |
| 3865 | */ |
| 3866 | static void __rbd_unregister_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3867 | { |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3868 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 3869 | int ret; |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3870 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3871 | rbd_assert(rbd_dev->watch_handle); |
| 3872 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3873 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3874 | ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle); |
| 3875 | if (ret) |
| 3876 | rbd_warn(rbd_dev, "failed to unwatch: %d", ret); |
Ilya Dryomov | b30a01f | 2014-05-22 19:28:52 +0400 | [diff] [blame] | 3877 | |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 3878 | rbd_dev->watch_handle = NULL; |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3879 | } |
| 3880 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3881 | static int rbd_register_watch(struct rbd_device *rbd_dev) |
Ilya Dryomov | c525f03 | 2016-04-28 16:07:26 +0200 | [diff] [blame] | 3882 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3883 | int ret; |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3884 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3885 | mutex_lock(&rbd_dev->watch_mutex); |
| 3886 | rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED); |
| 3887 | ret = __rbd_register_watch(rbd_dev); |
| 3888 | if (ret) |
| 3889 | goto out; |
| 3890 | |
| 3891 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3892 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3893 | |
| 3894 | out: |
| 3895 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3896 | return ret; |
| 3897 | } |
| 3898 | |
| 3899 | static void cancel_tasks_sync(struct rbd_device *rbd_dev) |
| 3900 | { |
| 3901 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3902 | |
| 3903 | cancel_delayed_work_sync(&rbd_dev->watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3904 | cancel_work_sync(&rbd_dev->acquired_lock_work); |
| 3905 | cancel_work_sync(&rbd_dev->released_lock_work); |
| 3906 | cancel_delayed_work_sync(&rbd_dev->lock_dwork); |
| 3907 | cancel_work_sync(&rbd_dev->unlock_work); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | static void rbd_unregister_watch(struct rbd_device *rbd_dev) |
| 3911 | { |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3912 | WARN_ON(waitqueue_active(&rbd_dev->lock_waitq)); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3913 | cancel_tasks_sync(rbd_dev); |
| 3914 | |
| 3915 | mutex_lock(&rbd_dev->watch_mutex); |
| 3916 | if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) |
| 3917 | __rbd_unregister_watch(rbd_dev); |
| 3918 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 3919 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3920 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 3921 | ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 3922 | } |
| 3923 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3924 | static void rbd_reregister_watch(struct work_struct *work) |
| 3925 | { |
| 3926 | struct rbd_device *rbd_dev = container_of(to_delayed_work(work), |
| 3927 | struct rbd_device, watch_dwork); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3928 | bool was_lock_owner = false; |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3929 | int ret; |
| 3930 | |
| 3931 | dout("%s rbd_dev %p\n", __func__, rbd_dev); |
| 3932 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3933 | down_write(&rbd_dev->lock_rwsem); |
| 3934 | if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) |
| 3935 | was_lock_owner = rbd_release_lock(rbd_dev); |
| 3936 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3937 | mutex_lock(&rbd_dev->watch_mutex); |
| 3938 | if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) |
| 3939 | goto fail_unlock; |
| 3940 | |
| 3941 | ret = __rbd_register_watch(rbd_dev); |
| 3942 | if (ret) { |
| 3943 | rbd_warn(rbd_dev, "failed to reregister watch: %d", ret); |
| 3944 | if (ret != -EBLACKLISTED) |
| 3945 | queue_delayed_work(rbd_dev->task_wq, |
| 3946 | &rbd_dev->watch_dwork, |
| 3947 | RBD_RETRY_DELAY); |
| 3948 | goto fail_unlock; |
| 3949 | } |
| 3950 | |
| 3951 | rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED; |
| 3952 | rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id; |
| 3953 | mutex_unlock(&rbd_dev->watch_mutex); |
| 3954 | |
| 3955 | ret = rbd_dev_refresh(rbd_dev); |
| 3956 | if (ret) |
| 3957 | rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret); |
| 3958 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3959 | if (was_lock_owner) { |
| 3960 | ret = rbd_try_lock(rbd_dev); |
| 3961 | if (ret) |
| 3962 | rbd_warn(rbd_dev, "reregisteration lock failed: %d", |
| 3963 | ret); |
| 3964 | } |
| 3965 | |
| 3966 | up_write(&rbd_dev->lock_rwsem); |
| 3967 | wake_requests(rbd_dev, true); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3968 | return; |
| 3969 | |
| 3970 | fail_unlock: |
| 3971 | mutex_unlock(&rbd_dev->watch_mutex); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 3972 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 3973 | } |
| 3974 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3975 | /* |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 3976 | * Synchronous osd object method call. Returns the number of bytes |
| 3977 | * returned in the outbound buffer, or a negative error code. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3978 | */ |
| 3979 | static int rbd_obj_method_sync(struct rbd_device *rbd_dev, |
| 3980 | const char *object_name, |
| 3981 | const char *class_name, |
| 3982 | const char *method_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3983 | const void *outbound, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3984 | size_t outbound_size, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 3985 | void *inbound, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 3986 | size_t inbound_size) |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3987 | { |
| 3988 | struct rbd_obj_request *obj_request; |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3989 | struct page **pages; |
| 3990 | u32 page_count; |
| 3991 | int ret; |
| 3992 | |
| 3993 | /* |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 3994 | * Method calls are ultimately read operations. The result |
| 3995 | * should placed into the inbound buffer provided. They |
| 3996 | * also supply outbound data--parameters for the object |
| 3997 | * method. Currently if this is present it will be a |
| 3998 | * snapshot id. |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 3999 | */ |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4000 | page_count = (u32)calc_pages_for(0, inbound_size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4001 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 4002 | if (IS_ERR(pages)) |
| 4003 | return PTR_ERR(pages); |
| 4004 | |
| 4005 | ret = -ENOMEM; |
Alex Elder | 6010a45 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4006 | obj_request = rbd_obj_request_create(object_name, 0, inbound_size, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4007 | OBJ_REQUEST_PAGES); |
| 4008 | if (!obj_request) |
| 4009 | goto out; |
| 4010 | |
| 4011 | obj_request->pages = pages; |
| 4012 | obj_request->page_count = page_count; |
| 4013 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4014 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 4015 | obj_request); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4016 | if (!obj_request->osd_req) |
| 4017 | goto out; |
| 4018 | |
Alex Elder | c99d2d4 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4019 | osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL, |
Alex Elder | 04017e2 | 2013-04-05 14:46:02 -0500 | [diff] [blame] | 4020 | class_name, method_name); |
| 4021 | if (outbound_size) { |
| 4022 | struct ceph_pagelist *pagelist; |
| 4023 | |
| 4024 | pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS); |
| 4025 | if (!pagelist) |
| 4026 | goto out; |
| 4027 | |
| 4028 | ceph_pagelist_init(pagelist); |
| 4029 | ceph_pagelist_append(pagelist, outbound, outbound_size); |
| 4030 | osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0, |
| 4031 | pagelist); |
| 4032 | } |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4033 | osd_req_op_cls_response_data_pages(obj_request->osd_req, 0, |
| 4034 | obj_request->pages, inbound_size, |
Alex Elder | 44cd188 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4035 | 0, false, false); |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 4036 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 4037 | rbd_obj_request_submit(obj_request); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4038 | ret = rbd_obj_request_wait(obj_request); |
| 4039 | if (ret) |
| 4040 | goto out; |
| 4041 | |
| 4042 | ret = obj_request->result; |
| 4043 | if (ret < 0) |
| 4044 | goto out; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4045 | |
| 4046 | rbd_assert(obj_request->xferred < (u64)INT_MAX); |
| 4047 | ret = (int)obj_request->xferred; |
Alex Elder | 903bb32 | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4048 | ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4049 | out: |
| 4050 | if (obj_request) |
| 4051 | rbd_obj_request_put(obj_request); |
| 4052 | else |
| 4053 | ceph_release_page_vector(pages, page_count); |
| 4054 | |
| 4055 | return ret; |
| 4056 | } |
| 4057 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4058 | /* |
| 4059 | * lock_rwsem must be held for read |
| 4060 | */ |
| 4061 | static void rbd_wait_state_locked(struct rbd_device *rbd_dev) |
| 4062 | { |
| 4063 | DEFINE_WAIT(wait); |
| 4064 | |
| 4065 | do { |
| 4066 | /* |
| 4067 | * Note the use of mod_delayed_work() in rbd_acquire_lock() |
| 4068 | * and cancel_delayed_work() in wake_requests(). |
| 4069 | */ |
| 4070 | dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev); |
| 4071 | queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0); |
| 4072 | prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait, |
| 4073 | TASK_UNINTERRUPTIBLE); |
| 4074 | up_read(&rbd_dev->lock_rwsem); |
| 4075 | schedule(); |
| 4076 | down_read(&rbd_dev->lock_rwsem); |
| 4077 | } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED); |
| 4078 | finish_wait(&rbd_dev->lock_waitq, &wait); |
| 4079 | } |
| 4080 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4081 | static void rbd_queue_workfn(struct work_struct *work) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4082 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4083 | struct request *rq = blk_mq_rq_from_pdu(work); |
| 4084 | struct rbd_device *rbd_dev = rq->q->queuedata; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4085 | struct rbd_img_request *img_request; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4086 | struct ceph_snap_context *snapc = NULL; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4087 | u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT; |
| 4088 | u64 length = blk_rq_bytes(rq); |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4089 | enum obj_operation_type op_type; |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4090 | u64 mapping_size; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4091 | bool must_be_locked; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4092 | int result; |
| 4093 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4094 | if (rq->cmd_type != REQ_TYPE_FS) { |
| 4095 | dout("%s: non-fs request type %d\n", __func__, |
| 4096 | (int) rq->cmd_type); |
| 4097 | result = -EIO; |
| 4098 | goto err; |
| 4099 | } |
| 4100 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4101 | if (req_op(rq) == REQ_OP_DISCARD) |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4102 | op_type = OBJ_OP_DISCARD; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 4103 | else if (req_op(rq) == REQ_OP_WRITE) |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4104 | op_type = OBJ_OP_WRITE; |
| 4105 | else |
| 4106 | op_type = OBJ_OP_READ; |
| 4107 | |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4108 | /* Ignore/skip any zero-length requests */ |
| 4109 | |
| 4110 | if (!length) { |
| 4111 | dout("%s: zero-length request\n", __func__); |
| 4112 | result = 0; |
| 4113 | goto err_rq; |
| 4114 | } |
| 4115 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4116 | /* Only reads are allowed to a read-only device */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4117 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4118 | if (op_type != OBJ_OP_READ) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4119 | if (rbd_dev->mapping.read_only) { |
| 4120 | result = -EROFS; |
| 4121 | goto err_rq; |
| 4122 | } |
| 4123 | rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP); |
| 4124 | } |
| 4125 | |
| 4126 | /* |
| 4127 | * Quit early if the mapped snapshot no longer exists. It's |
| 4128 | * still possible the snapshot will have disappeared by the |
| 4129 | * time our request arrives at the osd, but there's no sense in |
| 4130 | * sending it if we already know. |
| 4131 | */ |
| 4132 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) { |
| 4133 | dout("request for non-existent snapshot"); |
| 4134 | rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP); |
| 4135 | result = -ENXIO; |
| 4136 | goto err_rq; |
| 4137 | } |
| 4138 | |
| 4139 | if (offset && length > U64_MAX - offset + 1) { |
| 4140 | rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset, |
| 4141 | length); |
| 4142 | result = -EINVAL; |
| 4143 | goto err_rq; /* Shouldn't happen */ |
| 4144 | } |
| 4145 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4146 | blk_mq_start_request(rq); |
| 4147 | |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4148 | down_read(&rbd_dev->header_rwsem); |
| 4149 | mapping_size = rbd_dev->mapping.size; |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4150 | if (op_type != OBJ_OP_READ) { |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4151 | snapc = rbd_dev->header.snapc; |
| 4152 | ceph_get_snap_context(snapc); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4153 | must_be_locked = rbd_is_lock_supported(rbd_dev); |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 4154 | } else { |
| 4155 | must_be_locked = rbd_dev->opts->lock_on_read && |
| 4156 | rbd_is_lock_supported(rbd_dev); |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4157 | } |
| 4158 | up_read(&rbd_dev->header_rwsem); |
| 4159 | |
| 4160 | if (offset + length > mapping_size) { |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4161 | rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4162 | length, mapping_size); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4163 | result = -EIO; |
| 4164 | goto err_rq; |
| 4165 | } |
| 4166 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4167 | if (must_be_locked) { |
| 4168 | down_read(&rbd_dev->lock_rwsem); |
| 4169 | if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED) |
| 4170 | rbd_wait_state_locked(rbd_dev); |
| 4171 | } |
| 4172 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4173 | img_request = rbd_img_request_create(rbd_dev, offset, length, op_type, |
Josh Durgin | 4e752f0 | 2014-04-08 11:12:11 -0700 | [diff] [blame] | 4174 | snapc); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4175 | if (!img_request) { |
| 4176 | result = -ENOMEM; |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4177 | goto err_unlock; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4178 | } |
| 4179 | img_request->rq = rq; |
Ilya Dryomov | 70b16db | 2015-11-27 19:23:24 +0100 | [diff] [blame] | 4180 | snapc = NULL; /* img_request consumes a ref */ |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4181 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4182 | if (op_type == OBJ_OP_DISCARD) |
| 4183 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA, |
| 4184 | NULL); |
| 4185 | else |
| 4186 | result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO, |
| 4187 | rq->bio); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4188 | if (result) |
| 4189 | goto err_img_request; |
| 4190 | |
| 4191 | result = rbd_img_request_submit(img_request); |
| 4192 | if (result) |
| 4193 | goto err_img_request; |
| 4194 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4195 | if (must_be_locked) |
| 4196 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4197 | return; |
| 4198 | |
| 4199 | err_img_request: |
| 4200 | rbd_img_request_put(img_request); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4201 | err_unlock: |
| 4202 | if (must_be_locked) |
| 4203 | up_read(&rbd_dev->lock_rwsem); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4204 | err_rq: |
| 4205 | if (result) |
| 4206 | rbd_warn(rbd_dev, "%s %llx at %llx result %d", |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4207 | obj_op_name(op_type), length, offset, result); |
SF Markus Elfring | e96a650 | 2014-11-02 15:20:59 +0100 | [diff] [blame] | 4208 | ceph_put_snap_context(snapc); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4209 | err: |
| 4210 | blk_mq_end_request(rq, result); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4211 | } |
| 4212 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4213 | static int rbd_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 4214 | const struct blk_mq_queue_data *bd) |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4215 | { |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4216 | struct request *rq = bd->rq; |
| 4217 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 4218 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4219 | queue_work(rbd_wq, work); |
| 4220 | return BLK_MQ_RQ_QUEUE_OK; |
Alex Elder | bf0d5f50 | 2012-11-22 00:00:08 -0600 | [diff] [blame] | 4221 | } |
| 4222 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4223 | static void rbd_free_disk(struct rbd_device *rbd_dev) |
| 4224 | { |
| 4225 | struct gendisk *disk = rbd_dev->disk; |
| 4226 | |
| 4227 | if (!disk) |
| 4228 | return; |
| 4229 | |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4230 | rbd_dev->disk = NULL; |
| 4231 | if (disk->flags & GENHD_FL_UP) { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4232 | del_gendisk(disk); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4233 | if (disk->queue) |
| 4234 | blk_cleanup_queue(disk->queue); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4235 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Alex Elder | a0cab92 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 4236 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4237 | put_disk(disk); |
| 4238 | } |
| 4239 | |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4240 | static int rbd_obj_read_sync(struct rbd_device *rbd_dev, |
| 4241 | const char *object_name, |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4242 | u64 offset, u64 length, void *buf) |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4243 | |
| 4244 | { |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4245 | struct rbd_obj_request *obj_request; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4246 | struct page **pages = NULL; |
| 4247 | u32 page_count; |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4248 | size_t size; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4249 | int ret; |
| 4250 | |
| 4251 | page_count = (u32) calc_pages_for(offset, length); |
| 4252 | pages = ceph_alloc_page_vector(page_count, GFP_KERNEL); |
| 4253 | if (IS_ERR(pages)) |
Jan Kara | a8d4205 | 2014-10-22 09:17:24 +0200 | [diff] [blame] | 4254 | return PTR_ERR(pages); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4255 | |
| 4256 | ret = -ENOMEM; |
| 4257 | obj_request = rbd_obj_request_create(object_name, offset, length, |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4258 | OBJ_REQUEST_PAGES); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4259 | if (!obj_request) |
| 4260 | goto out; |
| 4261 | |
| 4262 | obj_request->pages = pages; |
| 4263 | obj_request->page_count = page_count; |
| 4264 | |
Guangliang Zhao | 6d2940c | 2014-03-13 11:21:35 +0800 | [diff] [blame] | 4265 | obj_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1, |
Ilya Dryomov | deb236b | 2014-02-25 16:22:27 +0200 | [diff] [blame] | 4266 | obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4267 | if (!obj_request->osd_req) |
| 4268 | goto out; |
| 4269 | |
Alex Elder | c99d2d4 | 2013-04-05 01:27:11 -0500 | [diff] [blame] | 4270 | osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ, |
| 4271 | offset, length, 0, 0); |
Alex Elder | 406e2c9 | 2013-04-15 14:50:36 -0500 | [diff] [blame] | 4272 | osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0, |
Alex Elder | a4ce40a | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4273 | obj_request->pages, |
Alex Elder | 44cd188 | 2013-04-05 01:27:12 -0500 | [diff] [blame] | 4274 | obj_request->length, |
| 4275 | obj_request->offset & ~PAGE_MASK, |
| 4276 | false, false); |
Alex Elder | 430c28c | 2013-04-03 21:32:51 -0500 | [diff] [blame] | 4277 | |
Ilya Dryomov | 980917f | 2016-09-12 18:59:42 +0200 | [diff] [blame] | 4278 | rbd_obj_request_submit(obj_request); |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4279 | ret = rbd_obj_request_wait(obj_request); |
| 4280 | if (ret) |
| 4281 | goto out; |
| 4282 | |
| 4283 | ret = obj_request->result; |
| 4284 | if (ret < 0) |
| 4285 | goto out; |
Alex Elder | 1ceae7e | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4286 | |
| 4287 | rbd_assert(obj_request->xferred <= (u64) SIZE_MAX); |
| 4288 | size = (size_t) obj_request->xferred; |
Alex Elder | 903bb32 | 2013-02-06 13:11:38 -0600 | [diff] [blame] | 4289 | ceph_copy_from_page_vector(pages, buf, 0, size); |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4290 | rbd_assert(size <= (size_t)INT_MAX); |
| 4291 | ret = (int)size; |
Alex Elder | 788e2df | 2013-01-17 12:25:27 -0600 | [diff] [blame] | 4292 | out: |
| 4293 | if (obj_request) |
| 4294 | rbd_obj_request_put(obj_request); |
| 4295 | else |
| 4296 | ceph_release_page_vector(pages, page_count); |
| 4297 | |
| 4298 | return ret; |
| 4299 | } |
| 4300 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4301 | /* |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4302 | * Read the complete header for the given rbd device. On successful |
| 4303 | * return, the rbd_dev->header field will contain up-to-date |
| 4304 | * information about the image. |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4305 | */ |
Alex Elder | 99a41eb | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 4306 | static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4307 | { |
| 4308 | struct rbd_image_header_ondisk *ondisk = NULL; |
| 4309 | u32 snap_count = 0; |
| 4310 | u64 names_size = 0; |
| 4311 | u32 want_count; |
| 4312 | int ret; |
| 4313 | |
| 4314 | /* |
| 4315 | * The complete header will include an array of its 64-bit |
| 4316 | * snapshot ids, followed by the names of those snapshots as |
| 4317 | * a contiguous block of NUL-terminated strings. Note that |
| 4318 | * the number of snapshots could change by the time we read |
| 4319 | * it in, in which case we re-read it. |
| 4320 | */ |
| 4321 | do { |
| 4322 | size_t size; |
| 4323 | |
| 4324 | kfree(ondisk); |
| 4325 | |
| 4326 | size = sizeof (*ondisk); |
| 4327 | size += snap_count * sizeof (struct rbd_image_snap_ondisk); |
| 4328 | size += names_size; |
| 4329 | ondisk = kmalloc(size, GFP_KERNEL); |
| 4330 | if (!ondisk) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4331 | return -ENOMEM; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4332 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4333 | ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 7097f8d | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4334 | 0, size, ondisk); |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4335 | if (ret < 0) |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4336 | goto out; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 4337 | if ((size_t)ret < size) { |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4338 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4339 | rbd_warn(rbd_dev, "short header read (want %zd got %d)", |
| 4340 | size, ret); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4341 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4342 | } |
| 4343 | if (!rbd_dev_ondisk_valid(ondisk)) { |
| 4344 | ret = -ENXIO; |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 4345 | rbd_warn(rbd_dev, "invalid header"); |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4346 | goto out; |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4347 | } |
| 4348 | |
| 4349 | names_size = le64_to_cpu(ondisk->snap_names_len); |
| 4350 | want_count = snap_count; |
| 4351 | snap_count = le32_to_cpu(ondisk->snap_count); |
| 4352 | } while (snap_count != want_count); |
| 4353 | |
Alex Elder | 662518b | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 4354 | ret = rbd_header_from_disk(rbd_dev, ondisk); |
| 4355 | out: |
Alex Elder | 4156d99 | 2012-08-02 11:29:46 -0500 | [diff] [blame] | 4356 | kfree(ondisk); |
| 4357 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4358 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4359 | } |
| 4360 | |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4361 | /* |
| 4362 | * Clear the rbd device's EXISTS flag if the snapshot it's mapped to |
| 4363 | * has disappeared from the (just updated) snapshot context. |
| 4364 | */ |
| 4365 | static void rbd_exists_validate(struct rbd_device *rbd_dev) |
| 4366 | { |
| 4367 | u64 snap_id; |
| 4368 | |
| 4369 | if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) |
| 4370 | return; |
| 4371 | |
| 4372 | snap_id = rbd_dev->spec->snap_id; |
| 4373 | if (snap_id == CEPH_NOSNAP) |
| 4374 | return; |
| 4375 | |
| 4376 | if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX) |
| 4377 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
| 4378 | } |
| 4379 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4380 | static void rbd_dev_update_size(struct rbd_device *rbd_dev) |
| 4381 | { |
| 4382 | sector_t size; |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4383 | |
| 4384 | /* |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4385 | * If EXISTS is not set, rbd_dev->disk may be NULL, so don't |
| 4386 | * try to update its size. If REMOVING is set, updating size |
| 4387 | * is just useless work since the device can't be opened. |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4388 | */ |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 4389 | if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) && |
| 4390 | !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) { |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4391 | size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE; |
| 4392 | dout("setting size to %llu sectors", (unsigned long long)size); |
| 4393 | set_capacity(rbd_dev->disk, size); |
| 4394 | revalidate_disk(rbd_dev->disk); |
| 4395 | } |
| 4396 | } |
| 4397 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4398 | static int rbd_dev_refresh(struct rbd_device *rbd_dev) |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4399 | { |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4400 | u64 mapping_size; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4401 | int ret; |
| 4402 | |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4403 | down_write(&rbd_dev->header_rwsem); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 4404 | mapping_size = rbd_dev->mapping.size; |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 4405 | |
| 4406 | ret = rbd_dev_header_info(rbd_dev); |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4407 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4408 | goto out; |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4409 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4410 | /* |
| 4411 | * If there is a parent, see if it has disappeared due to the |
| 4412 | * mapped image getting flattened. |
| 4413 | */ |
| 4414 | if (rbd_dev->parent) { |
| 4415 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 4416 | if (ret) |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4417 | goto out; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 4418 | } |
| 4419 | |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4420 | if (rbd_dev->spec->snap_id == CEPH_NOSNAP) { |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4421 | rbd_dev->mapping.size = rbd_dev->header.image_size; |
Ilya Dryomov | 5ff1108 | 2014-07-23 17:11:21 +0400 | [diff] [blame] | 4422 | } else { |
| 4423 | /* validate mapped snapshot's EXISTS flag */ |
| 4424 | rbd_exists_validate(rbd_dev); |
| 4425 | } |
Alex Elder | 15228ed | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 4426 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4427 | out: |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 4428 | up_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4429 | if (!ret && mapping_size != rbd_dev->mapping.size) |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 4430 | rbd_dev_update_size(rbd_dev); |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4431 | |
Ilya Dryomov | 73e39e4 | 2015-01-08 20:18:22 +0300 | [diff] [blame] | 4432 | return ret; |
Alex Elder | 1fe5e99 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4433 | } |
| 4434 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4435 | static int rbd_init_request(void *data, struct request *rq, |
| 4436 | unsigned int hctx_idx, unsigned int request_idx, |
| 4437 | unsigned int numa_node) |
| 4438 | { |
| 4439 | struct work_struct *work = blk_mq_rq_to_pdu(rq); |
| 4440 | |
| 4441 | INIT_WORK(work, rbd_queue_workfn); |
| 4442 | return 0; |
| 4443 | } |
| 4444 | |
| 4445 | static struct blk_mq_ops rbd_mq_ops = { |
| 4446 | .queue_rq = rbd_queue_rq, |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4447 | .init_request = rbd_init_request, |
| 4448 | }; |
| 4449 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4450 | static int rbd_init_disk(struct rbd_device *rbd_dev) |
| 4451 | { |
| 4452 | struct gendisk *disk; |
| 4453 | struct request_queue *q; |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4454 | u64 segment_size; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4455 | int err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4456 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4457 | /* create gendisk info */ |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4458 | disk = alloc_disk(single_major ? |
| 4459 | (1 << RBD_SINGLE_MAJOR_PART_SHIFT) : |
| 4460 | RBD_MINORS_PER_MAJOR); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4461 | if (!disk) |
Alex Elder | 1fcdb8a | 2012-08-29 17:11:06 -0500 | [diff] [blame] | 4462 | return -ENOMEM; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4463 | |
Alex Elder | f0f8cef | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 4464 | snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d", |
Alex Elder | de71a29 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4465 | rbd_dev->dev_id); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4466 | disk->major = rbd_dev->major; |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4467 | disk->first_minor = rbd_dev->minor; |
Ilya Dryomov | 7e513d4 | 2013-12-16 19:26:32 +0200 | [diff] [blame] | 4468 | if (single_major) |
| 4469 | disk->flags |= GENHD_FL_EXT_DEVT; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4470 | disk->fops = &rbd_bd_ops; |
| 4471 | disk->private_data = rbd_dev; |
| 4472 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4473 | memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set)); |
| 4474 | rbd_dev->tag_set.ops = &rbd_mq_ops; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4475 | rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4476 | rbd_dev->tag_set.numa_node = NUMA_NO_NODE; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 4477 | rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4478 | rbd_dev->tag_set.nr_hw_queues = 1; |
| 4479 | rbd_dev->tag_set.cmd_size = sizeof(struct work_struct); |
| 4480 | |
| 4481 | err = blk_mq_alloc_tag_set(&rbd_dev->tag_set); |
| 4482 | if (err) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4483 | goto out_disk; |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4484 | |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4485 | q = blk_mq_init_queue(&rbd_dev->tag_set); |
| 4486 | if (IS_ERR(q)) { |
| 4487 | err = PTR_ERR(q); |
| 4488 | goto out_tag_set; |
| 4489 | } |
| 4490 | |
Ilya Dryomov | d8a2c89 | 2015-03-24 16:15:17 +0300 | [diff] [blame] | 4491 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q); |
| 4492 | /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4493 | |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4494 | /* set io sizes to object size */ |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4495 | segment_size = rbd_obj_bytes(&rbd_dev->header); |
| 4496 | blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE); |
Ilya Dryomov | 0d9fde4 | 2015-10-07 16:09:35 +0200 | [diff] [blame] | 4497 | q->limits.max_sectors = queue_max_hw_sectors(q); |
Ilya Dryomov | d3834fe | 2015-06-12 19:19:02 +0300 | [diff] [blame] | 4498 | blk_queue_max_segments(q, segment_size / SECTOR_SIZE); |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4499 | blk_queue_max_segment_size(q, segment_size); |
| 4500 | blk_queue_io_min(q, segment_size); |
| 4501 | blk_queue_io_opt(q, segment_size); |
Josh Durgin | 029bcbd | 2011-07-22 11:35:23 -0700 | [diff] [blame] | 4502 | |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4503 | /* enable the discard support */ |
| 4504 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 4505 | q->limits.discard_granularity = segment_size; |
| 4506 | q->limits.discard_alignment = segment_size; |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 4507 | blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE); |
Josh Durgin | b76f823 | 2014-04-07 16:52:03 -0700 | [diff] [blame] | 4508 | q->limits.discard_zeroes_data = 1; |
Guangliang Zhao | 90e98c5 | 2014-04-01 22:22:16 +0800 | [diff] [blame] | 4509 | |
Ronny Hegewald | bae818e | 2015-10-15 18:50:46 +0000 | [diff] [blame] | 4510 | if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC)) |
| 4511 | q->backing_dev_info.capabilities |= BDI_CAP_STABLE_WRITES; |
| 4512 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4513 | disk->queue = q; |
| 4514 | |
| 4515 | q->queuedata = rbd_dev; |
| 4516 | |
| 4517 | rbd_dev->disk = disk; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4518 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4519 | return 0; |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4520 | out_tag_set: |
| 4521 | blk_mq_free_tag_set(&rbd_dev->tag_set); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4522 | out_disk: |
| 4523 | put_disk(disk); |
Christoph Hellwig | 7ad18af | 2015-01-13 17:20:04 +0100 | [diff] [blame] | 4524 | return err; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4525 | } |
| 4526 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4527 | /* |
| 4528 | sysfs |
| 4529 | */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4530 | |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4531 | static struct rbd_device *dev_to_rbd_dev(struct device *dev) |
| 4532 | { |
| 4533 | return container_of(dev, struct rbd_device, dev); |
| 4534 | } |
| 4535 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4536 | static ssize_t rbd_size_show(struct device *dev, |
| 4537 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4538 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4539 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4540 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4541 | return sprintf(buf, "%llu\n", |
| 4542 | (unsigned long long)rbd_dev->mapping.size); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4545 | /* |
| 4546 | * Note this shows the features for whatever's mapped, which is not |
| 4547 | * necessarily the base image. |
| 4548 | */ |
| 4549 | static ssize_t rbd_features_show(struct device *dev, |
| 4550 | struct device_attribute *attr, char *buf) |
| 4551 | { |
| 4552 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4553 | |
| 4554 | return sprintf(buf, "0x%016llx\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4555 | (unsigned long long)rbd_dev->mapping.features); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4556 | } |
| 4557 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4558 | static ssize_t rbd_major_show(struct device *dev, |
| 4559 | struct device_attribute *attr, char *buf) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4560 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4561 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4562 | |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4563 | if (rbd_dev->major) |
| 4564 | return sprintf(buf, "%d\n", rbd_dev->major); |
| 4565 | |
| 4566 | return sprintf(buf, "(none)\n"); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4567 | } |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4568 | |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4569 | static ssize_t rbd_minor_show(struct device *dev, |
| 4570 | struct device_attribute *attr, char *buf) |
| 4571 | { |
| 4572 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4573 | |
| 4574 | return sprintf(buf, "%d\n", rbd_dev->minor); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4575 | } |
| 4576 | |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4577 | static ssize_t rbd_client_addr_show(struct device *dev, |
| 4578 | struct device_attribute *attr, char *buf) |
| 4579 | { |
| 4580 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4581 | struct ceph_entity_addr *client_addr = |
| 4582 | ceph_client_addr(rbd_dev->rbd_client->client); |
| 4583 | |
| 4584 | return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr, |
| 4585 | le32_to_cpu(client_addr->nonce)); |
| 4586 | } |
| 4587 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4588 | static ssize_t rbd_client_id_show(struct device *dev, |
| 4589 | struct device_attribute *attr, char *buf) |
| 4590 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4591 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4592 | |
Alex Elder | 1dbb439 | 2012-01-24 10:08:37 -0600 | [diff] [blame] | 4593 | return sprintf(buf, "client%lld\n", |
Ilya Dryomov | 033268a | 2016-08-12 14:59:58 +0200 | [diff] [blame] | 4594 | ceph_client_gid(rbd_dev->rbd_client->client)); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4595 | } |
| 4596 | |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4597 | static ssize_t rbd_cluster_fsid_show(struct device *dev, |
| 4598 | struct device_attribute *attr, char *buf) |
| 4599 | { |
| 4600 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4601 | |
| 4602 | return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid); |
| 4603 | } |
| 4604 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4605 | static ssize_t rbd_config_info_show(struct device *dev, |
| 4606 | struct device_attribute *attr, char *buf) |
| 4607 | { |
| 4608 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4609 | |
| 4610 | return sprintf(buf, "%s\n", rbd_dev->config_info); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4611 | } |
| 4612 | |
| 4613 | static ssize_t rbd_pool_show(struct device *dev, |
| 4614 | struct device_attribute *attr, char *buf) |
| 4615 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4616 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4617 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4618 | return sprintf(buf, "%s\n", rbd_dev->spec->pool_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4619 | } |
| 4620 | |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4621 | static ssize_t rbd_pool_id_show(struct device *dev, |
| 4622 | struct device_attribute *attr, char *buf) |
| 4623 | { |
| 4624 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4625 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4626 | return sprintf(buf, "%llu\n", |
Alex Elder | fc71d83 | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 4627 | (unsigned long long) rbd_dev->spec->pool_id); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4628 | } |
| 4629 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4630 | static ssize_t rbd_name_show(struct device *dev, |
| 4631 | struct device_attribute *attr, char *buf) |
| 4632 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4633 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4634 | |
Alex Elder | a92ffdf | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 4635 | if (rbd_dev->spec->image_name) |
| 4636 | return sprintf(buf, "%s\n", rbd_dev->spec->image_name); |
| 4637 | |
| 4638 | return sprintf(buf, "(unknown)\n"); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4639 | } |
| 4640 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4641 | static ssize_t rbd_image_id_show(struct device *dev, |
| 4642 | struct device_attribute *attr, char *buf) |
| 4643 | { |
| 4644 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4645 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4646 | return sprintf(buf, "%s\n", rbd_dev->spec->image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4647 | } |
| 4648 | |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4649 | /* |
| 4650 | * Shows the name of the currently-mapped snapshot (or |
| 4651 | * RBD_SNAP_HEAD_NAME for the base image). |
| 4652 | */ |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4653 | static ssize_t rbd_snap_show(struct device *dev, |
| 4654 | struct device_attribute *attr, |
| 4655 | char *buf) |
| 4656 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4657 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4658 | |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 4659 | return sprintf(buf, "%s\n", rbd_dev->spec->snap_name); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4660 | } |
| 4661 | |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4662 | static ssize_t rbd_snap_id_show(struct device *dev, |
| 4663 | struct device_attribute *attr, char *buf) |
| 4664 | { |
| 4665 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4666 | |
| 4667 | return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id); |
| 4668 | } |
| 4669 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4670 | /* |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4671 | * For a v2 image, shows the chain of parent images, separated by empty |
| 4672 | * lines. For v1 images or if there is no parent, shows "(no parent |
| 4673 | * image)". |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4674 | */ |
| 4675 | static ssize_t rbd_parent_show(struct device *dev, |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4676 | struct device_attribute *attr, |
| 4677 | char *buf) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4678 | { |
| 4679 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4680 | ssize_t count = 0; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4681 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4682 | if (!rbd_dev->parent) |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4683 | return sprintf(buf, "(no parent image)\n"); |
| 4684 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4685 | for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) { |
| 4686 | struct rbd_spec *spec = rbd_dev->parent_spec; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4687 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4688 | count += sprintf(&buf[count], "%s" |
| 4689 | "pool_id %llu\npool_name %s\n" |
| 4690 | "image_id %s\nimage_name %s\n" |
| 4691 | "snap_id %llu\nsnap_name %s\n" |
| 4692 | "overlap %llu\n", |
| 4693 | !count ? "" : "\n", /* first? */ |
| 4694 | spec->pool_id, spec->pool_name, |
| 4695 | spec->image_id, spec->image_name ?: "(unknown)", |
| 4696 | spec->snap_id, spec->snap_name, |
| 4697 | rbd_dev->parent_overlap); |
| 4698 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4699 | |
Ilya Dryomov | ff96128 | 2014-07-22 21:53:07 +0400 | [diff] [blame] | 4700 | return count; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4701 | } |
| 4702 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4703 | static ssize_t rbd_image_refresh(struct device *dev, |
| 4704 | struct device_attribute *attr, |
| 4705 | const char *buf, |
| 4706 | size_t size) |
| 4707 | { |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 4708 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4709 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4710 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4711 | ret = rbd_dev_refresh(rbd_dev); |
Alex Elder | e627db0 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 4712 | if (ret) |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4713 | return ret; |
Alex Elder | b813623 | 2012-07-25 09:32:41 -0500 | [diff] [blame] | 4714 | |
Ilya Dryomov | 52bb1f9 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4715 | return size; |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4716 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 4717 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4718 | static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL); |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4719 | static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4720 | static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL); |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4721 | static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL); |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4722 | static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4723 | static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL); |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4724 | static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4725 | static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4726 | static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL); |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4727 | static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4728 | static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4729 | static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4730 | static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh); |
| 4731 | static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL); |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4732 | static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4733 | static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4734 | |
| 4735 | static struct attribute *rbd_attrs[] = { |
| 4736 | &dev_attr_size.attr, |
Alex Elder | 34b1318 | 2012-07-13 20:35:12 -0500 | [diff] [blame] | 4737 | &dev_attr_features.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4738 | &dev_attr_major.attr, |
Ilya Dryomov | dd82fff | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 4739 | &dev_attr_minor.attr, |
Ilya Dryomov | 005a07bf | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4740 | &dev_attr_client_addr.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4741 | &dev_attr_client_id.attr, |
Mike Christie | 267fb90 | 2016-08-18 18:38:43 +0200 | [diff] [blame] | 4742 | &dev_attr_cluster_fsid.attr, |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4743 | &dev_attr_config_info.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4744 | &dev_attr_pool.attr, |
Alex Elder | 9bb2f33 | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 4745 | &dev_attr_pool_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4746 | &dev_attr_name.attr, |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 4747 | &dev_attr_image_id.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4748 | &dev_attr_current_snap.attr, |
Mike Christie | 92a5867 | 2016-08-18 18:38:44 +0200 | [diff] [blame] | 4749 | &dev_attr_snap_id.attr, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4750 | &dev_attr_parent.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4751 | &dev_attr_refresh.attr, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4752 | NULL |
| 4753 | }; |
| 4754 | |
| 4755 | static struct attribute_group rbd_attr_group = { |
| 4756 | .attrs = rbd_attrs, |
| 4757 | }; |
| 4758 | |
| 4759 | static const struct attribute_group *rbd_attr_groups[] = { |
| 4760 | &rbd_attr_group, |
| 4761 | NULL |
| 4762 | }; |
| 4763 | |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4764 | static void rbd_dev_release(struct device *dev); |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4765 | |
| 4766 | static struct device_type rbd_device_type = { |
| 4767 | .name = "rbd", |
| 4768 | .groups = rbd_attr_groups, |
Ilya Dryomov | 6cac469 | 2015-10-16 20:11:25 +0200 | [diff] [blame] | 4769 | .release = rbd_dev_release, |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4770 | }; |
| 4771 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4772 | static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec) |
| 4773 | { |
| 4774 | kref_get(&spec->kref); |
| 4775 | |
| 4776 | return spec; |
| 4777 | } |
| 4778 | |
| 4779 | static void rbd_spec_free(struct kref *kref); |
| 4780 | static void rbd_spec_put(struct rbd_spec *spec) |
| 4781 | { |
| 4782 | if (spec) |
| 4783 | kref_put(&spec->kref, rbd_spec_free); |
| 4784 | } |
| 4785 | |
| 4786 | static struct rbd_spec *rbd_spec_alloc(void) |
| 4787 | { |
| 4788 | struct rbd_spec *spec; |
| 4789 | |
| 4790 | spec = kzalloc(sizeof (*spec), GFP_KERNEL); |
| 4791 | if (!spec) |
| 4792 | return NULL; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 4793 | |
| 4794 | spec->pool_id = CEPH_NOPOOL; |
| 4795 | spec->snap_id = CEPH_NOSNAP; |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4796 | kref_init(&spec->kref); |
| 4797 | |
Alex Elder | 8b8fb99 | 2012-10-26 17:25:24 -0500 | [diff] [blame] | 4798 | return spec; |
| 4799 | } |
| 4800 | |
| 4801 | static void rbd_spec_free(struct kref *kref) |
| 4802 | { |
| 4803 | struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref); |
| 4804 | |
| 4805 | kfree(spec->pool_name); |
| 4806 | kfree(spec->image_id); |
| 4807 | kfree(spec->image_name); |
| 4808 | kfree(spec->snap_name); |
| 4809 | kfree(spec); |
| 4810 | } |
| 4811 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4812 | static void rbd_dev_free(struct rbd_device *rbd_dev) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4813 | { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4814 | WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED); |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4815 | WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4816 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4817 | ceph_oid_destroy(&rbd_dev->header_oid); |
Ilya Dryomov | 6b6dddb | 2016-08-05 16:15:38 +0200 | [diff] [blame] | 4818 | ceph_oloc_destroy(&rbd_dev->header_oloc); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 4819 | kfree(rbd_dev->config_info); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4820 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4821 | rbd_put_client(rbd_dev->rbd_client); |
| 4822 | rbd_spec_put(rbd_dev->spec); |
| 4823 | kfree(rbd_dev->opts); |
| 4824 | kfree(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | static void rbd_dev_release(struct device *dev) |
| 4828 | { |
| 4829 | struct rbd_device *rbd_dev = dev_to_rbd_dev(dev); |
| 4830 | bool need_put = !!rbd_dev->opts; |
| 4831 | |
| 4832 | if (need_put) { |
| 4833 | destroy_workqueue(rbd_dev->task_wq); |
| 4834 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4835 | } |
| 4836 | |
| 4837 | rbd_dev_free(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4838 | |
| 4839 | /* |
| 4840 | * This is racy, but way better than putting module outside of |
| 4841 | * the release callback. The race window is pretty small, so |
| 4842 | * doing something similar to dm (dm-builtin.c) is overkill. |
| 4843 | */ |
| 4844 | if (need_put) |
| 4845 | module_put(THIS_MODULE); |
| 4846 | } |
| 4847 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4848 | static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc, |
| 4849 | struct rbd_spec *spec) |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4850 | { |
| 4851 | struct rbd_device *rbd_dev; |
| 4852 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4853 | rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4854 | if (!rbd_dev) |
| 4855 | return NULL; |
| 4856 | |
| 4857 | spin_lock_init(&rbd_dev->lock); |
| 4858 | INIT_LIST_HEAD(&rbd_dev->node); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4859 | init_rwsem(&rbd_dev->header_rwsem); |
| 4860 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4861 | ceph_oid_init(&rbd_dev->header_oid); |
Ilya Dryomov | 922dab6 | 2016-05-26 01:15:02 +0200 | [diff] [blame] | 4862 | ceph_oloc_init(&rbd_dev->header_oloc); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4863 | |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 4864 | mutex_init(&rbd_dev->watch_mutex); |
| 4865 | rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED; |
| 4866 | INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch); |
| 4867 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 4868 | init_rwsem(&rbd_dev->lock_rwsem); |
| 4869 | rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED; |
| 4870 | INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock); |
| 4871 | INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock); |
| 4872 | INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock); |
| 4873 | INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work); |
| 4874 | init_waitqueue_head(&rbd_dev->lock_waitq); |
| 4875 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4876 | rbd_dev->dev.bus = &rbd_bus_type; |
| 4877 | rbd_dev->dev.type = &rbd_device_type; |
| 4878 | rbd_dev->dev.parent = &rbd_root_dev; |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4879 | device_initialize(&rbd_dev->dev); |
| 4880 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4881 | rbd_dev->rbd_client = rbdc; |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 4882 | rbd_dev->spec = spec; |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4883 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 4884 | rbd_dev->layout.stripe_unit = 1 << RBD_MAX_OBJ_ORDER; |
| 4885 | rbd_dev->layout.stripe_count = 1; |
| 4886 | rbd_dev->layout.object_size = 1 << RBD_MAX_OBJ_ORDER; |
| 4887 | rbd_dev->layout.pool_id = spec->pool_id; |
Yan, Zheng | 30c156d | 2016-02-14 11:24:31 +0800 | [diff] [blame] | 4888 | RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL); |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 4889 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4890 | return rbd_dev; |
| 4891 | } |
| 4892 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 4893 | /* |
| 4894 | * Create a mapping rbd_dev. |
| 4895 | */ |
| 4896 | static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc, |
| 4897 | struct rbd_spec *spec, |
| 4898 | struct rbd_options *opts) |
| 4899 | { |
| 4900 | struct rbd_device *rbd_dev; |
| 4901 | |
| 4902 | rbd_dev = __rbd_dev_create(rbdc, spec); |
| 4903 | if (!rbd_dev) |
| 4904 | return NULL; |
| 4905 | |
| 4906 | rbd_dev->opts = opts; |
| 4907 | |
| 4908 | /* get an id and fill in device name */ |
| 4909 | rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0, |
| 4910 | minor_to_rbd_dev_id(1 << MINORBITS), |
| 4911 | GFP_KERNEL); |
| 4912 | if (rbd_dev->dev_id < 0) |
| 4913 | goto fail_rbd_dev; |
| 4914 | |
| 4915 | sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id); |
| 4916 | rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM, |
| 4917 | rbd_dev->name); |
| 4918 | if (!rbd_dev->task_wq) |
| 4919 | goto fail_dev_id; |
| 4920 | |
| 4921 | /* we have a ref from do_rbd_add() */ |
| 4922 | __module_get(THIS_MODULE); |
| 4923 | |
| 4924 | dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id); |
| 4925 | return rbd_dev; |
| 4926 | |
| 4927 | fail_dev_id: |
| 4928 | ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id); |
| 4929 | fail_rbd_dev: |
| 4930 | rbd_dev_free(rbd_dev); |
| 4931 | return NULL; |
| 4932 | } |
| 4933 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4934 | static void rbd_dev_destroy(struct rbd_device *rbd_dev) |
| 4935 | { |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 4936 | if (rbd_dev) |
| 4937 | put_device(&rbd_dev->dev); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 4938 | } |
| 4939 | |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 4940 | /* |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4941 | * Get the size and object order for an image snapshot, or if |
| 4942 | * snap_id is CEPH_NOSNAP, gets this information for the base |
| 4943 | * image. |
| 4944 | */ |
| 4945 | static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id, |
| 4946 | u8 *order, u64 *snap_size) |
| 4947 | { |
| 4948 | __le64 snapid = cpu_to_le64(snap_id); |
| 4949 | int ret; |
| 4950 | struct { |
| 4951 | u8 order; |
| 4952 | __le64 size; |
| 4953 | } __attribute__ ((packed)) size_buf = { 0 }; |
| 4954 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4955 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4956 | "rbd", "get_size", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4957 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4958 | &size_buf, sizeof (size_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4959 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4960 | if (ret < 0) |
| 4961 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4962 | if (ret < sizeof (size_buf)) |
| 4963 | return -ERANGE; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4964 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4965 | if (order) { |
Alex Elder | c86f86e | 2013-04-25 15:09:41 -0500 | [diff] [blame] | 4966 | *order = size_buf.order; |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4967 | dout(" order %u", (unsigned int)*order); |
| 4968 | } |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4969 | *snap_size = le64_to_cpu(size_buf.size); |
| 4970 | |
Josh Durgin | c354557 | 2013-08-28 17:08:10 -0700 | [diff] [blame] | 4971 | dout(" snap_id 0x%016llx snap_size = %llu\n", |
| 4972 | (unsigned long long)snap_id, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4973 | (unsigned long long)*snap_size); |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4974 | |
| 4975 | return 0; |
| 4976 | } |
| 4977 | |
| 4978 | static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev) |
| 4979 | { |
| 4980 | return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP, |
| 4981 | &rbd_dev->header.obj_order, |
| 4982 | &rbd_dev->header.image_size); |
| 4983 | } |
| 4984 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4985 | static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev) |
| 4986 | { |
| 4987 | void *reply_buf; |
| 4988 | int ret; |
| 4989 | void *p; |
| 4990 | |
| 4991 | reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL); |
| 4992 | if (!reply_buf) |
| 4993 | return -ENOMEM; |
| 4994 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 4995 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 4996 | "rbd", "get_object_prefix", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 4997 | reply_buf, RBD_OBJ_PREFIX_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 4998 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 4999 | if (ret < 0) |
| 5000 | goto out; |
| 5001 | |
| 5002 | p = reply_buf; |
| 5003 | rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p, |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5004 | p + ret, NULL, GFP_NOIO); |
| 5005 | ret = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5006 | |
| 5007 | if (IS_ERR(rbd_dev->header.object_prefix)) { |
| 5008 | ret = PTR_ERR(rbd_dev->header.object_prefix); |
| 5009 | rbd_dev->header.object_prefix = NULL; |
| 5010 | } else { |
| 5011 | dout(" object_prefix = %s\n", rbd_dev->header.object_prefix); |
| 5012 | } |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5013 | out: |
| 5014 | kfree(reply_buf); |
| 5015 | |
| 5016 | return ret; |
| 5017 | } |
| 5018 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5019 | static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id, |
| 5020 | u64 *snap_features) |
| 5021 | { |
| 5022 | __le64 snapid = cpu_to_le64(snap_id); |
| 5023 | struct { |
| 5024 | __le64 features; |
| 5025 | __le64 incompat; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5026 | } __attribute__ ((packed)) features_buf = { 0 }; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5027 | u64 unsup; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5028 | int ret; |
| 5029 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5030 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5031 | "rbd", "get_features", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5032 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5033 | &features_buf, sizeof (features_buf)); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5034 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5035 | if (ret < 0) |
| 5036 | return ret; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5037 | if (ret < sizeof (features_buf)) |
| 5038 | return -ERANGE; |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 5039 | |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5040 | unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED; |
| 5041 | if (unsup) { |
| 5042 | rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx", |
| 5043 | unsup); |
Alex Elder | b8f5c6e | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5044 | return -ENXIO; |
Ilya Dryomov | d3767f0 | 2016-04-13 14:15:50 +0200 | [diff] [blame] | 5045 | } |
Alex Elder | d889140 | 2012-10-09 13:50:17 -0700 | [diff] [blame] | 5046 | |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5047 | *snap_features = le64_to_cpu(features_buf.features); |
| 5048 | |
| 5049 | dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5050 | (unsigned long long)snap_id, |
| 5051 | (unsigned long long)*snap_features, |
| 5052 | (unsigned long long)le64_to_cpu(features_buf.incompat)); |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5053 | |
| 5054 | return 0; |
| 5055 | } |
| 5056 | |
| 5057 | static int rbd_dev_v2_features(struct rbd_device *rbd_dev) |
| 5058 | { |
| 5059 | return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP, |
| 5060 | &rbd_dev->header.features); |
| 5061 | } |
| 5062 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5063 | static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev) |
| 5064 | { |
| 5065 | struct rbd_spec *parent_spec; |
| 5066 | size_t size; |
| 5067 | void *reply_buf = NULL; |
| 5068 | __le64 snapid; |
| 5069 | void *p; |
| 5070 | void *end; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5071 | u64 pool_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5072 | char *image_id; |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5073 | u64 snap_id; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5074 | u64 overlap; |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5075 | int ret; |
| 5076 | |
| 5077 | parent_spec = rbd_spec_alloc(); |
| 5078 | if (!parent_spec) |
| 5079 | return -ENOMEM; |
| 5080 | |
| 5081 | size = sizeof (__le64) + /* pool_id */ |
| 5082 | sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */ |
| 5083 | sizeof (__le64) + /* snap_id */ |
| 5084 | sizeof (__le64); /* overlap */ |
| 5085 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5086 | if (!reply_buf) { |
| 5087 | ret = -ENOMEM; |
| 5088 | goto out_err; |
| 5089 | } |
| 5090 | |
Ilya Dryomov | 4d9b67c | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 5091 | snapid = cpu_to_le64(rbd_dev->spec->snap_id); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5092 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5093 | "rbd", "get_parent", |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5094 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5095 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5096 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5097 | if (ret < 0) |
| 5098 | goto out_err; |
| 5099 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5100 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5101 | end = reply_buf + ret; |
| 5102 | ret = -ERANGE; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5103 | ceph_decode_64_safe(&p, end, pool_id, out_err); |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5104 | if (pool_id == CEPH_NOPOOL) { |
| 5105 | /* |
| 5106 | * Either the parent never existed, or we have |
| 5107 | * record of it but the image got flattened so it no |
| 5108 | * longer has a parent. When the parent of a |
| 5109 | * layered image disappears we immediately set the |
| 5110 | * overlap to 0. The effect of this is that all new |
| 5111 | * requests will be treated as if the image had no |
| 5112 | * parent. |
| 5113 | */ |
| 5114 | if (rbd_dev->parent_overlap) { |
| 5115 | rbd_dev->parent_overlap = 0; |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5116 | rbd_dev_parent_put(rbd_dev); |
| 5117 | pr_info("%s: clone image has been flattened\n", |
| 5118 | rbd_dev->disk->disk_name); |
| 5119 | } |
| 5120 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5121 | goto out; /* No parent? No problem. */ |
Alex Elder | 392a9da | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5122 | } |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5123 | |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5124 | /* The ceph file layout needs to fit pool id in 32 bits */ |
| 5125 | |
| 5126 | ret = -EIO; |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5127 | if (pool_id > (u64)U32_MAX) { |
Ilya Dryomov | 9584d50 | 2014-07-11 12:11:20 +0400 | [diff] [blame] | 5128 | rbd_warn(NULL, "parent pool id too large (%llu > %u)", |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5129 | (unsigned long long)pool_id, U32_MAX); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5130 | goto out_err; |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5131 | } |
Alex Elder | 0903e87 | 2012-11-14 12:25:19 -0600 | [diff] [blame] | 5132 | |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5133 | image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5134 | if (IS_ERR(image_id)) { |
| 5135 | ret = PTR_ERR(image_id); |
| 5136 | goto out_err; |
| 5137 | } |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5138 | ceph_decode_64_safe(&p, end, snap_id, out_err); |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5139 | ceph_decode_64_safe(&p, end, overlap, out_err); |
| 5140 | |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5141 | /* |
| 5142 | * The parent won't change (except when the clone is |
| 5143 | * flattened, already handled that). So we only need to |
| 5144 | * record the parent spec we have not already done so. |
| 5145 | */ |
| 5146 | if (!rbd_dev->parent_spec) { |
| 5147 | parent_spec->pool_id = pool_id; |
| 5148 | parent_spec->image_id = image_id; |
| 5149 | parent_spec->snap_id = snap_id; |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5150 | rbd_dev->parent_spec = parent_spec; |
| 5151 | parent_spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | fbba11b | 2014-06-27 21:46:33 +0400 | [diff] [blame] | 5152 | } else { |
| 5153 | kfree(image_id); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | /* |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5157 | * We always update the parent overlap. If it's zero we issue |
| 5158 | * a warning, as we will proceed as if there was no parent. |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5159 | */ |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5160 | if (!overlap) { |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5161 | if (parent_spec) { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5162 | /* refresh, careful to warn just once */ |
| 5163 | if (rbd_dev->parent_overlap) |
| 5164 | rbd_warn(rbd_dev, |
| 5165 | "clone now standalone (overlap became 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5166 | } else { |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5167 | /* initial probe */ |
| 5168 | rbd_warn(rbd_dev, "clone is standalone (overlap 0)"); |
Alex Elder | 3b5cf2a | 2013-05-29 11:18:59 -0500 | [diff] [blame] | 5169 | } |
Alex Elder | 70cf49c | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5170 | } |
Ilya Dryomov | cf32bd9 | 2015-01-19 22:57:39 +0300 | [diff] [blame] | 5171 | rbd_dev->parent_overlap = overlap; |
| 5172 | |
Alex Elder | 86b00e0 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5173 | out: |
| 5174 | ret = 0; |
| 5175 | out_err: |
| 5176 | kfree(reply_buf); |
| 5177 | rbd_spec_put(parent_spec); |
| 5178 | |
| 5179 | return ret; |
| 5180 | } |
| 5181 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5182 | static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev) |
| 5183 | { |
| 5184 | struct { |
| 5185 | __le64 stripe_unit; |
| 5186 | __le64 stripe_count; |
| 5187 | } __attribute__ ((packed)) striping_info_buf = { 0 }; |
| 5188 | size_t size = sizeof (striping_info_buf); |
| 5189 | void *p; |
| 5190 | u64 obj_size; |
| 5191 | u64 stripe_unit; |
| 5192 | u64 stripe_count; |
| 5193 | int ret; |
| 5194 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5195 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5196 | "rbd", "get_stripe_unit_count", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5197 | (char *)&striping_info_buf, size); |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5198 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
| 5199 | if (ret < 0) |
| 5200 | return ret; |
| 5201 | if (ret < size) |
| 5202 | return -ERANGE; |
| 5203 | |
| 5204 | /* |
| 5205 | * We don't actually support the "fancy striping" feature |
| 5206 | * (STRIPINGV2) yet, but if the striping sizes are the |
| 5207 | * defaults the behavior is the same as before. So find |
| 5208 | * out, and only fail if the image has non-default values. |
| 5209 | */ |
| 5210 | ret = -EINVAL; |
| 5211 | obj_size = (u64)1 << rbd_dev->header.obj_order; |
| 5212 | p = &striping_info_buf; |
| 5213 | stripe_unit = ceph_decode_64(&p); |
| 5214 | if (stripe_unit != obj_size) { |
| 5215 | rbd_warn(rbd_dev, "unsupported stripe unit " |
| 5216 | "(got %llu want %llu)", |
| 5217 | stripe_unit, obj_size); |
| 5218 | return -EINVAL; |
| 5219 | } |
| 5220 | stripe_count = ceph_decode_64(&p); |
| 5221 | if (stripe_count != 1) { |
| 5222 | rbd_warn(rbd_dev, "unsupported stripe count " |
| 5223 | "(got %llu want 1)", stripe_count); |
| 5224 | return -EINVAL; |
| 5225 | } |
Alex Elder | 500d0c0 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5226 | rbd_dev->header.stripe_unit = stripe_unit; |
| 5227 | rbd_dev->header.stripe_count = stripe_count; |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5228 | |
| 5229 | return 0; |
| 5230 | } |
| 5231 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5232 | static char *rbd_dev_image_name(struct rbd_device *rbd_dev) |
| 5233 | { |
| 5234 | size_t image_id_size; |
| 5235 | char *image_id; |
| 5236 | void *p; |
| 5237 | void *end; |
| 5238 | size_t size; |
| 5239 | void *reply_buf = NULL; |
| 5240 | size_t len = 0; |
| 5241 | char *image_name = NULL; |
| 5242 | int ret; |
| 5243 | |
| 5244 | rbd_assert(!rbd_dev->spec->image_name); |
| 5245 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5246 | len = strlen(rbd_dev->spec->image_id); |
| 5247 | image_id_size = sizeof (__le32) + len; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5248 | image_id = kmalloc(image_id_size, GFP_KERNEL); |
| 5249 | if (!image_id) |
| 5250 | return NULL; |
| 5251 | |
| 5252 | p = image_id; |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5253 | end = image_id + image_id_size; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5254 | ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5255 | |
| 5256 | size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX; |
| 5257 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5258 | if (!reply_buf) |
| 5259 | goto out; |
| 5260 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5261 | ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY, |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5262 | "rbd", "dir_get_name", |
| 5263 | image_id, image_id_size, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5264 | reply_buf, size); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5265 | if (ret < 0) |
| 5266 | goto out; |
| 5267 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5268 | end = reply_buf + ret; |
| 5269 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5270 | image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL); |
| 5271 | if (IS_ERR(image_name)) |
| 5272 | image_name = NULL; |
| 5273 | else |
| 5274 | dout("%s: name is %s len is %zd\n", __func__, image_name, len); |
| 5275 | out: |
| 5276 | kfree(reply_buf); |
| 5277 | kfree(image_id); |
| 5278 | |
| 5279 | return image_name; |
| 5280 | } |
| 5281 | |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5282 | static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5283 | { |
| 5284 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5285 | const char *snap_name; |
| 5286 | u32 which = 0; |
| 5287 | |
| 5288 | /* Skip over names until we find the one we are looking for */ |
| 5289 | |
| 5290 | snap_name = rbd_dev->header.snap_names; |
| 5291 | while (which < snapc->num_snaps) { |
| 5292 | if (!strcmp(name, snap_name)) |
| 5293 | return snapc->snaps[which]; |
| 5294 | snap_name += strlen(snap_name) + 1; |
| 5295 | which++; |
| 5296 | } |
| 5297 | return CEPH_NOSNAP; |
| 5298 | } |
| 5299 | |
| 5300 | static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5301 | { |
| 5302 | struct ceph_snap_context *snapc = rbd_dev->header.snapc; |
| 5303 | u32 which; |
| 5304 | bool found = false; |
| 5305 | u64 snap_id; |
| 5306 | |
| 5307 | for (which = 0; !found && which < snapc->num_snaps; which++) { |
| 5308 | const char *snap_name; |
| 5309 | |
| 5310 | snap_id = snapc->snaps[which]; |
| 5311 | snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id); |
Josh Durgin | efadc98 | 2013-08-29 19:16:42 -0700 | [diff] [blame] | 5312 | if (IS_ERR(snap_name)) { |
| 5313 | /* ignore no-longer existing snapshots */ |
| 5314 | if (PTR_ERR(snap_name) == -ENOENT) |
| 5315 | continue; |
| 5316 | else |
| 5317 | break; |
| 5318 | } |
Alex Elder | 2ad3d71 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5319 | found = !strcmp(name, snap_name); |
| 5320 | kfree(snap_name); |
| 5321 | } |
| 5322 | return found ? snap_id : CEPH_NOSNAP; |
| 5323 | } |
| 5324 | |
| 5325 | /* |
| 5326 | * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if |
| 5327 | * no snapshot by that name is found, or if an error occurs. |
| 5328 | */ |
| 5329 | static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name) |
| 5330 | { |
| 5331 | if (rbd_dev->image_format == 1) |
| 5332 | return rbd_v1_snap_id_by_name(rbd_dev, name); |
| 5333 | |
| 5334 | return rbd_v2_snap_id_by_name(rbd_dev, name); |
| 5335 | } |
| 5336 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5337 | /* |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5338 | * An image being mapped will have everything but the snap id. |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5339 | */ |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5340 | static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev) |
| 5341 | { |
| 5342 | struct rbd_spec *spec = rbd_dev->spec; |
| 5343 | |
| 5344 | rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name); |
| 5345 | rbd_assert(spec->image_id && spec->image_name); |
| 5346 | rbd_assert(spec->snap_name); |
| 5347 | |
| 5348 | if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) { |
| 5349 | u64 snap_id; |
| 5350 | |
| 5351 | snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name); |
| 5352 | if (snap_id == CEPH_NOSNAP) |
| 5353 | return -ENOENT; |
| 5354 | |
| 5355 | spec->snap_id = snap_id; |
| 5356 | } else { |
| 5357 | spec->snap_id = CEPH_NOSNAP; |
| 5358 | } |
| 5359 | |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
| 5363 | /* |
| 5364 | * A parent image will have all ids but none of the names. |
| 5365 | * |
| 5366 | * All names in an rbd spec are dynamically allocated. It's OK if we |
| 5367 | * can't figure out the name for an image id. |
| 5368 | */ |
| 5369 | static int rbd_spec_fill_names(struct rbd_device *rbd_dev) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5370 | { |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5371 | struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc; |
| 5372 | struct rbd_spec *spec = rbd_dev->spec; |
| 5373 | const char *pool_name; |
| 5374 | const char *image_name; |
| 5375 | const char *snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5376 | int ret; |
| 5377 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5378 | rbd_assert(spec->pool_id != CEPH_NOPOOL); |
| 5379 | rbd_assert(spec->image_id); |
| 5380 | rbd_assert(spec->snap_id != CEPH_NOSNAP); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5381 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5382 | /* 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] | 5383 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5384 | pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id); |
| 5385 | if (!pool_name) { |
| 5386 | rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id); |
Alex Elder | 935dc89 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5387 | return -EIO; |
| 5388 | } |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5389 | pool_name = kstrdup(pool_name, GFP_KERNEL); |
| 5390 | if (!pool_name) |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5391 | return -ENOMEM; |
| 5392 | |
| 5393 | /* Fetch the image name; tolerate failure here */ |
| 5394 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5395 | image_name = rbd_dev_image_name(rbd_dev); |
| 5396 | if (!image_name) |
Alex Elder | 06ecc6c | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5397 | rbd_warn(rbd_dev, "unable to get image name"); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5398 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5399 | /* Fetch the snapshot name */ |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5400 | |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5401 | snap_name = rbd_snap_name(rbd_dev, spec->snap_id); |
Josh Durgin | da6a6b6 | 2013-09-04 17:57:31 -0700 | [diff] [blame] | 5402 | if (IS_ERR(snap_name)) { |
| 5403 | ret = PTR_ERR(snap_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5404 | goto out_err; |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | spec->pool_name = pool_name; |
| 5408 | spec->image_name = image_name; |
| 5409 | spec->snap_name = snap_name; |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5410 | |
| 5411 | return 0; |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 5412 | |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5413 | out_err: |
Alex Elder | 2e9f7f1 | 2013-04-26 09:43:48 -0500 | [diff] [blame] | 5414 | kfree(image_name); |
| 5415 | kfree(pool_name); |
Alex Elder | 9e15b77 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5416 | return ret; |
| 5417 | } |
| 5418 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5419 | static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev) |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5420 | { |
| 5421 | size_t size; |
| 5422 | int ret; |
| 5423 | void *reply_buf; |
| 5424 | void *p; |
| 5425 | void *end; |
| 5426 | u64 seq; |
| 5427 | u32 snap_count; |
| 5428 | struct ceph_snap_context *snapc; |
| 5429 | u32 i; |
| 5430 | |
| 5431 | /* |
| 5432 | * We'll need room for the seq value (maximum snapshot id), |
| 5433 | * snapshot count, and array of that many snapshot ids. |
| 5434 | * For now we have a fixed upper limit on the number we're |
| 5435 | * prepared to receive. |
| 5436 | */ |
| 5437 | size = sizeof (__le64) + sizeof (__le32) + |
| 5438 | RBD_MAX_SNAP_COUNT * sizeof (__le64); |
| 5439 | reply_buf = kzalloc(size, GFP_KERNEL); |
| 5440 | if (!reply_buf) |
| 5441 | return -ENOMEM; |
| 5442 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5443 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5444 | "rbd", "get_snapcontext", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5445 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5446 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5447 | if (ret < 0) |
| 5448 | goto out; |
| 5449 | |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5450 | p = reply_buf; |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5451 | end = reply_buf + ret; |
| 5452 | ret = -ERANGE; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5453 | ceph_decode_64_safe(&p, end, seq, out); |
| 5454 | ceph_decode_32_safe(&p, end, snap_count, out); |
| 5455 | |
| 5456 | /* |
| 5457 | * Make sure the reported number of snapshot ids wouldn't go |
| 5458 | * beyond the end of our buffer. But before checking that, |
| 5459 | * make sure the computed size of the snapshot context we |
| 5460 | * allocate is representable in a size_t. |
| 5461 | */ |
| 5462 | if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context)) |
| 5463 | / sizeof (u64)) { |
| 5464 | ret = -EINVAL; |
| 5465 | goto out; |
| 5466 | } |
| 5467 | if (!ceph_has_room(&p, end, snap_count * sizeof (__le64))) |
| 5468 | goto out; |
Alex Elder | 468521c | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5469 | ret = 0; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5470 | |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5471 | snapc = ceph_create_snap_context(snap_count, GFP_KERNEL); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5472 | if (!snapc) { |
| 5473 | ret = -ENOMEM; |
| 5474 | goto out; |
| 5475 | } |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5476 | snapc->seq = seq; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5477 | for (i = 0; i < snap_count; i++) |
| 5478 | snapc->snaps[i] = ceph_decode_64(&p); |
| 5479 | |
Alex Elder | 49ece55 | 2013-05-06 08:37:00 -0500 | [diff] [blame] | 5480 | ceph_put_snap_context(rbd_dev->header.snapc); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5481 | rbd_dev->header.snapc = snapc; |
| 5482 | |
| 5483 | dout(" snap context seq = %llu, snap_count = %u\n", |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5484 | (unsigned long long)seq, (unsigned int)snap_count); |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5485 | out: |
| 5486 | kfree(reply_buf); |
| 5487 | |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5488 | return ret; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5489 | } |
| 5490 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5491 | static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, |
| 5492 | u64 snap_id) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5493 | { |
| 5494 | size_t size; |
| 5495 | void *reply_buf; |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5496 | __le64 snapid; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5497 | int ret; |
| 5498 | void *p; |
| 5499 | void *end; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5500 | char *snap_name; |
| 5501 | |
| 5502 | size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN; |
| 5503 | reply_buf = kmalloc(size, GFP_KERNEL); |
| 5504 | if (!reply_buf) |
| 5505 | return ERR_PTR(-ENOMEM); |
| 5506 | |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5507 | snapid = cpu_to_le64(snap_id); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 5508 | ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_oid.name, |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5509 | "rbd", "get_snapshot_name", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5510 | &snapid, sizeof (snapid), |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5511 | reply_buf, size); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5512 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5513 | if (ret < 0) { |
| 5514 | snap_name = ERR_PTR(ret); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5515 | goto out; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5516 | } |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5517 | |
| 5518 | p = reply_buf; |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5519 | end = reply_buf + ret; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5520 | snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL); |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5521 | if (IS_ERR(snap_name)) |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5522 | goto out; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5523 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5524 | dout(" snap_id 0x%016llx snap_name = %s\n", |
Alex Elder | 54cac61 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5525 | (unsigned long long)snap_id, snap_name); |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5526 | out: |
| 5527 | kfree(reply_buf); |
| 5528 | |
Alex Elder | f40eb34 | 2013-04-25 15:09:42 -0500 | [diff] [blame] | 5529 | return snap_name; |
Alex Elder | b8b1e2d | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5530 | } |
| 5531 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5532 | static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev) |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5533 | { |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5534 | bool first_time = rbd_dev->header.object_prefix == NULL; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5535 | int ret; |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5536 | |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5537 | ret = rbd_dev_v2_image_size(rbd_dev); |
| 5538 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5539 | return ret; |
Josh Durgin | 1617e40 | 2013-06-12 14:43:10 -0700 | [diff] [blame] | 5540 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5541 | if (first_time) { |
| 5542 | ret = rbd_dev_v2_header_onetime(rbd_dev); |
| 5543 | if (ret) |
Alex Elder | cfbf637 | 2013-05-31 17:40:45 -0500 | [diff] [blame] | 5544 | return ret; |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5545 | } |
| 5546 | |
Alex Elder | cc4a38bd | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5547 | ret = rbd_dev_v2_snap_context(rbd_dev); |
Ilya Dryomov | d194cd1 | 2015-08-31 18:22:10 +0300 | [diff] [blame] | 5548 | if (ret && first_time) { |
| 5549 | kfree(rbd_dev->header.object_prefix); |
| 5550 | rbd_dev->header.object_prefix = NULL; |
| 5551 | } |
Alex Elder | 117973f | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5552 | |
| 5553 | return ret; |
| 5554 | } |
| 5555 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 5556 | static int rbd_dev_header_info(struct rbd_device *rbd_dev) |
| 5557 | { |
| 5558 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 5559 | |
| 5560 | if (rbd_dev->image_format == 1) |
| 5561 | return rbd_dev_v1_header_info(rbd_dev); |
| 5562 | |
| 5563 | return rbd_dev_v2_header_info(rbd_dev); |
| 5564 | } |
| 5565 | |
Alex Elder | 1ddbe94 | 2012-01-29 13:57:44 -0600 | [diff] [blame] | 5566 | /* |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5567 | * Skips over white space at *buf, and updates *buf to point to the |
| 5568 | * first found non-space character (if any). Returns the length of |
Alex Elder | 593a9e7 | 2012-02-07 12:03:37 -0600 | [diff] [blame] | 5569 | * the token (string of non-white space characters) found. Note |
| 5570 | * that *buf must be terminated with '\0'. |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5571 | */ |
| 5572 | static inline size_t next_token(const char **buf) |
| 5573 | { |
| 5574 | /* |
| 5575 | * These are the characters that produce nonzero for |
| 5576 | * isspace() in the "C" and "POSIX" locales. |
| 5577 | */ |
| 5578 | const char *spaces = " \f\n\r\t\v"; |
| 5579 | |
| 5580 | *buf += strspn(*buf, spaces); /* Find start of token */ |
| 5581 | |
| 5582 | return strcspn(*buf, spaces); /* Return token length */ |
| 5583 | } |
| 5584 | |
| 5585 | /* |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5586 | * Finds the next token in *buf, dynamically allocates a buffer big |
| 5587 | * enough to hold a copy of it, and copies the token into the new |
| 5588 | * buffer. The copy is guaranteed to be terminated with '\0'. Note |
| 5589 | * that a duplicate buffer is created even for a zero-length token. |
| 5590 | * |
| 5591 | * Returns a pointer to the newly-allocated duplicate, or a null |
| 5592 | * pointer if memory for the duplicate was not available. If |
| 5593 | * the lenp argument is a non-null pointer, the length of the token |
| 5594 | * (not including the '\0') is returned in *lenp. |
| 5595 | * |
| 5596 | * If successful, the *buf pointer will be updated to point beyond |
| 5597 | * the end of the found token. |
| 5598 | * |
| 5599 | * Note: uses GFP_KERNEL for allocation. |
| 5600 | */ |
| 5601 | static inline char *dup_token(const char **buf, size_t *lenp) |
| 5602 | { |
| 5603 | char *dup; |
| 5604 | size_t len; |
| 5605 | |
| 5606 | len = next_token(buf); |
Alex Elder | 4caf35f | 2012-11-01 08:39:27 -0500 | [diff] [blame] | 5607 | dup = kmemdup(*buf, len + 1, GFP_KERNEL); |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5608 | if (!dup) |
| 5609 | return NULL; |
Alex Elder | ea3352f | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 5610 | *(dup + len) = '\0'; |
| 5611 | *buf += len; |
| 5612 | |
| 5613 | if (lenp) |
| 5614 | *lenp = len; |
| 5615 | |
| 5616 | return dup; |
| 5617 | } |
| 5618 | |
| 5619 | /* |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5620 | * Parse the options provided for an "rbd add" (i.e., rbd image |
| 5621 | * mapping) request. These arrive via a write to /sys/bus/rbd/add, |
| 5622 | * and the data written is passed here via a NUL-terminated buffer. |
| 5623 | * Returns 0 if successful or an error code otherwise. |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5624 | * |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5625 | * The information extracted from these options is recorded in |
| 5626 | * the other parameters which return dynamically-allocated |
| 5627 | * structures: |
| 5628 | * ceph_opts |
| 5629 | * The address of a pointer that will refer to a ceph options |
| 5630 | * structure. Caller must release the returned pointer using |
| 5631 | * ceph_destroy_options() when it is no longer needed. |
| 5632 | * rbd_opts |
| 5633 | * Address of an rbd options pointer. Fully initialized by |
| 5634 | * this function; caller must release with kfree(). |
| 5635 | * spec |
| 5636 | * Address of an rbd image specification pointer. Fully |
| 5637 | * initialized by this function based on parsed options. |
| 5638 | * Caller must release with rbd_spec_put(). |
| 5639 | * |
| 5640 | * The options passed take this form: |
| 5641 | * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>] |
| 5642 | * where: |
| 5643 | * <mon_addrs> |
| 5644 | * A comma-separated list of one or more monitor addresses. |
| 5645 | * A monitor address is an ip address, optionally followed |
| 5646 | * by a port number (separated by a colon). |
| 5647 | * I.e.: ip1[:port1][,ip2[:port2]...] |
| 5648 | * <options> |
| 5649 | * A comma-separated list of ceph and/or rbd options. |
| 5650 | * <pool_name> |
| 5651 | * The name of the rados pool containing the rbd image. |
| 5652 | * <image_name> |
| 5653 | * The name of the image in that pool to map. |
| 5654 | * <snap_id> |
| 5655 | * An optional snapshot id. If provided, the mapping will |
| 5656 | * present data from the image at the time that snapshot was |
| 5657 | * created. The image head is used if no snapshot id is |
| 5658 | * provided. Snapshot mappings are always read-only. |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5659 | */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5660 | static int rbd_add_parse_args(const char *buf, |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5661 | struct ceph_options **ceph_opts, |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5662 | struct rbd_options **opts, |
| 5663 | struct rbd_spec **rbd_spec) |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5664 | { |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5665 | size_t len; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5666 | char *options; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5667 | const char *mon_addrs; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5668 | char *snap_name; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5669 | size_t mon_addrs_size; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5670 | struct rbd_spec *spec = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5671 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5672 | struct ceph_options *copts; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5673 | int ret; |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5674 | |
| 5675 | /* The first four tokens are required */ |
| 5676 | |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5677 | len = next_token(&buf); |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5678 | if (!len) { |
| 5679 | rbd_warn(NULL, "no monitor address(es) provided"); |
| 5680 | return -EINVAL; |
| 5681 | } |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5682 | mon_addrs = buf; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5683 | mon_addrs_size = len + 1; |
Alex Elder | 7ef3214 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5684 | buf += len; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5685 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5686 | ret = -EINVAL; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5687 | options = dup_token(&buf, NULL); |
| 5688 | if (!options) |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5689 | return -ENOMEM; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5690 | if (!*options) { |
| 5691 | rbd_warn(NULL, "no options provided"); |
| 5692 | goto out_err; |
| 5693 | } |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5694 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5695 | spec = rbd_spec_alloc(); |
| 5696 | if (!spec) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5697 | goto out_mem; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5698 | |
| 5699 | spec->pool_name = dup_token(&buf, NULL); |
| 5700 | if (!spec->pool_name) |
| 5701 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5702 | if (!*spec->pool_name) { |
| 5703 | rbd_warn(NULL, "no pool name provided"); |
| 5704 | goto out_err; |
| 5705 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5706 | |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5707 | spec->image_name = dup_token(&buf, NULL); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5708 | if (!spec->image_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5709 | goto out_mem; |
Alex Elder | 4fb5d671 | 2012-11-01 10:17:15 -0500 | [diff] [blame] | 5710 | if (!*spec->image_name) { |
| 5711 | rbd_warn(NULL, "no image name provided"); |
| 5712 | goto out_err; |
| 5713 | } |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5714 | |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5715 | /* |
| 5716 | * Snapshot name is optional; default is to use "-" |
| 5717 | * (indicating the head/no snapshot). |
| 5718 | */ |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5719 | len = next_token(&buf); |
Alex Elder | 820a5f3 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5720 | if (!len) { |
Alex Elder | 3feeb894 | 2012-08-31 17:29:52 -0500 | [diff] [blame] | 5721 | buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */ |
| 5722 | len = sizeof (RBD_SNAP_HEAD_NAME) - 1; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5723 | } else if (len > RBD_MAX_SNAP_NAME_LEN) { |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5724 | ret = -ENAMETOOLONG; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5725 | goto out_err; |
Alex Elder | 849b426 | 2012-07-09 21:04:24 -0500 | [diff] [blame] | 5726 | } |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5727 | snap_name = kmemdup(buf, len + 1, GFP_KERNEL); |
| 5728 | if (!snap_name) |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5729 | goto out_mem; |
Alex Elder | ecb4dc2 | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 5730 | *(snap_name + len) = '\0'; |
| 5731 | spec->snap_name = snap_name; |
Alex Elder | e5c3553 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5732 | |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5733 | /* Initialize all rbd options to the defaults */ |
Alex Elder | e28fff26 | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5734 | |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5735 | rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL); |
| 5736 | if (!rbd_opts) |
| 5737 | goto out_mem; |
| 5738 | |
| 5739 | rbd_opts->read_only = RBD_READ_ONLY_DEFAULT; |
Ilya Dryomov | b558418 | 2015-06-23 16:21:19 +0300 | [diff] [blame] | 5740 | rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT; |
Ilya Dryomov | 80de191 | 2016-09-20 14:23:17 +0200 | [diff] [blame] | 5741 | rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5742 | |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5743 | copts = ceph_parse_options(options, mon_addrs, |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5744 | mon_addrs + mon_addrs_size - 1, |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5745 | parse_rbd_opts_token, rbd_opts); |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5746 | if (IS_ERR(copts)) { |
| 5747 | ret = PTR_ERR(copts); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5748 | goto out_err; |
| 5749 | } |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5750 | kfree(options); |
| 5751 | |
| 5752 | *ceph_opts = copts; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5753 | *opts = rbd_opts; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5754 | *rbd_spec = spec; |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5755 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5756 | return 0; |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5757 | out_mem: |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5758 | ret = -ENOMEM; |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5759 | out_err: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 5760 | kfree(rbd_opts); |
| 5761 | rbd_spec_put(spec); |
Alex Elder | f28e565 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5762 | kfree(options); |
Alex Elder | d22f76e | 2012-07-12 10:46:35 -0500 | [diff] [blame] | 5763 | |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5764 | return ret; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 5765 | } |
| 5766 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5767 | /* |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5768 | * Return pool id (>= 0) or a negative error code. |
| 5769 | */ |
| 5770 | static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name) |
| 5771 | { |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5772 | struct ceph_options *opts = rbdc->client->options; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5773 | u64 newest_epoch; |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5774 | int tries = 0; |
| 5775 | int ret; |
| 5776 | |
| 5777 | again: |
| 5778 | ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name); |
| 5779 | if (ret == -ENOENT && tries++ < 1) { |
Ilya Dryomov | d0b1970 | 2016-04-28 16:07:27 +0200 | [diff] [blame] | 5780 | ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap", |
| 5781 | &newest_epoch); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5782 | if (ret < 0) |
| 5783 | return ret; |
| 5784 | |
| 5785 | if (rbdc->client->osdc.osdmap->epoch < newest_epoch) { |
Ilya Dryomov | 7cca78c | 2016-04-28 16:07:28 +0200 | [diff] [blame] | 5786 | ceph_osdc_maybe_request_map(&rbdc->client->osdc); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5787 | (void) ceph_monc_wait_osdmap(&rbdc->client->monc, |
Ilya Dryomov | a319bf5 | 2015-05-15 12:02:17 +0300 | [diff] [blame] | 5788 | newest_epoch, |
| 5789 | opts->mount_timeout); |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 5790 | goto again; |
| 5791 | } else { |
| 5792 | /* the osdmap we have is new enough */ |
| 5793 | return -ENOENT; |
| 5794 | } |
| 5795 | } |
| 5796 | |
| 5797 | return ret; |
| 5798 | } |
| 5799 | |
| 5800 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5801 | * An rbd format 2 image has a unique identifier, distinct from the |
| 5802 | * name given to it by the user. Internally, that identifier is |
| 5803 | * what's used to specify the names of objects related to the image. |
| 5804 | * |
| 5805 | * A special "rbd id" object is used to map an rbd image name to its |
| 5806 | * id. If that object doesn't exist, then there is no v2 rbd image |
| 5807 | * with the supplied name. |
| 5808 | * |
| 5809 | * This function will record the given rbd_dev's image_id field if |
| 5810 | * it can be determined, and in that case will return 0. If any |
| 5811 | * errors occur a negative errno will be returned and the rbd_dev's |
| 5812 | * image_id field will be unchanged (and should be NULL). |
| 5813 | */ |
| 5814 | static int rbd_dev_image_id(struct rbd_device *rbd_dev) |
| 5815 | { |
| 5816 | int ret; |
| 5817 | size_t size; |
| 5818 | char *object_name; |
| 5819 | void *response; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5820 | char *image_id; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5821 | |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5822 | /* |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5823 | * When probing a parent image, the image id is already |
| 5824 | * known (and the image name likely is not). There's no |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5825 | * need to fetch the image id again in this case. We |
| 5826 | * do still need to set the image format though. |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5827 | */ |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5828 | if (rbd_dev->spec->image_id) { |
| 5829 | rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1; |
| 5830 | |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5831 | return 0; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5832 | } |
Alex Elder | 2c0d0a1 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5833 | |
| 5834 | /* |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5835 | * First, see if the format 2 image id file exists, and if |
| 5836 | * so, get the image's persistent id from it. |
| 5837 | */ |
Alex Elder | 69e7a02 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5838 | size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5839 | object_name = kmalloc(size, GFP_NOIO); |
| 5840 | if (!object_name) |
| 5841 | return -ENOMEM; |
Alex Elder | 0d7dbfc | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 5842 | sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5843 | dout("rbd id object name is %s\n", object_name); |
| 5844 | |
| 5845 | /* Response will be an encoded string, which includes a length */ |
| 5846 | |
| 5847 | size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX; |
| 5848 | response = kzalloc(size, GFP_NOIO); |
| 5849 | if (!response) { |
| 5850 | ret = -ENOMEM; |
| 5851 | goto out; |
| 5852 | } |
| 5853 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5854 | /* If it doesn't exist we'll assume it's a format 1 image */ |
| 5855 | |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5856 | ret = rbd_obj_method_sync(rbd_dev, object_name, |
Alex Elder | 4157976 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5857 | "rbd", "get_id", NULL, 0, |
Alex Elder | e2a58ee | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 5858 | response, RBD_IMAGE_ID_LEN_MAX); |
Alex Elder | 36be9a7 | 2013-01-19 00:30:28 -0600 | [diff] [blame] | 5859 | dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5860 | if (ret == -ENOENT) { |
| 5861 | image_id = kstrdup("", GFP_KERNEL); |
| 5862 | ret = image_id ? 0 : -ENOMEM; |
| 5863 | if (!ret) |
| 5864 | rbd_dev->image_format = 1; |
Ilya Dryomov | 7dd440c | 2014-09-11 18:49:18 +0400 | [diff] [blame] | 5865 | } else if (ret >= 0) { |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5866 | void *p = response; |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5867 | |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5868 | image_id = ceph_extract_encoded_string(&p, p + ret, |
Alex Elder | 979ed48 | 2012-11-01 08:39:26 -0500 | [diff] [blame] | 5869 | NULL, GFP_NOIO); |
Duan Jiong | 461f758 | 2014-04-11 16:38:12 +0800 | [diff] [blame] | 5870 | ret = PTR_ERR_OR_ZERO(image_id); |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5871 | if (!ret) |
| 5872 | rbd_dev->image_format = 2; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 5873 | } |
| 5874 | |
| 5875 | if (!ret) { |
| 5876 | rbd_dev->spec->image_id = image_id; |
| 5877 | dout("image_id is %s\n", image_id); |
Alex Elder | 589d30e | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5878 | } |
| 5879 | out: |
| 5880 | kfree(response); |
| 5881 | kfree(object_name); |
| 5882 | |
| 5883 | return ret; |
| 5884 | } |
| 5885 | |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 5886 | /* |
| 5887 | * Undo whatever state changes are made by v1 or v2 header info |
| 5888 | * call. |
| 5889 | */ |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5890 | static void rbd_dev_unprobe(struct rbd_device *rbd_dev) |
| 5891 | { |
| 5892 | struct rbd_image_header *header; |
| 5893 | |
Ilya Dryomov | e69b8d4 | 2015-01-19 12:06:14 +0300 | [diff] [blame] | 5894 | rbd_dev_parent_put(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5895 | |
| 5896 | /* Free dynamic fields from the header, then zero it out */ |
| 5897 | |
| 5898 | header = &rbd_dev->header; |
Alex Elder | 812164f8 | 2013-04-30 00:44:32 -0500 | [diff] [blame] | 5899 | ceph_put_snap_context(header->snapc); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5900 | kfree(header->snap_sizes); |
| 5901 | kfree(header->snap_names); |
| 5902 | kfree(header->object_prefix); |
| 5903 | memset(header, 0, sizeof (*header)); |
| 5904 | } |
| 5905 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5906 | static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev) |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5907 | { |
| 5908 | int ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5909 | |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5910 | ret = rbd_dev_v2_object_prefix(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5911 | if (ret) |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5912 | goto out_err; |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5913 | |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5914 | /* |
| 5915 | * Get the and check features for the image. Currently the |
| 5916 | * features are assumed to never change. |
| 5917 | */ |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5918 | ret = rbd_dev_v2_features(rbd_dev); |
Alex Elder | 57385b5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5919 | if (ret) |
Alex Elder | b1b5402 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5920 | goto out_err; |
Alex Elder | 35d489f | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5921 | |
Alex Elder | cc070d5 | 2013-04-21 12:14:45 -0500 | [diff] [blame] | 5922 | /* If the image supports fancy striping, get its parameters */ |
| 5923 | |
| 5924 | if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) { |
| 5925 | ret = rbd_dev_v2_striping_info(rbd_dev); |
| 5926 | if (ret < 0) |
| 5927 | goto out_err; |
| 5928 | } |
Alex Elder | 2df3fac | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 5929 | /* No support for crypto and compression type format 2 images */ |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5930 | |
Alex Elder | 3515297 | 2012-08-31 17:29:55 -0500 | [diff] [blame] | 5931 | return 0; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5932 | out_err: |
Alex Elder | 642a253 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 5933 | rbd_dev->header.features = 0; |
Alex Elder | 1e13019 | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5934 | kfree(rbd_dev->header.object_prefix); |
| 5935 | rbd_dev->header.object_prefix = NULL; |
Alex Elder | 9d475de | 2012-07-03 16:01:19 -0500 | [diff] [blame] | 5936 | |
| 5937 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 5938 | } |
| 5939 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5940 | /* |
| 5941 | * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() -> |
| 5942 | * rbd_dev_image_probe() recursion depth, which means it's also the |
| 5943 | * length of the already discovered part of the parent chain. |
| 5944 | */ |
| 5945 | 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] | 5946 | { |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 5947 | struct rbd_device *parent = NULL; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5948 | int ret; |
| 5949 | |
| 5950 | if (!rbd_dev->parent_spec) |
| 5951 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5952 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5953 | if (++depth > RBD_MAX_PARENT_CHAIN_LEN) { |
| 5954 | pr_info("parent chain is too long (%d)\n", depth); |
| 5955 | ret = -EINVAL; |
| 5956 | goto out_err; |
| 5957 | } |
| 5958 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5959 | parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec); |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5960 | if (!parent) { |
| 5961 | ret = -ENOMEM; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5962 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | /* |
| 5966 | * Images related by parent/child relationships always share |
| 5967 | * rbd_client and spec/parent_spec, so bump their refcounts. |
| 5968 | */ |
| 5969 | __rbd_get_client(rbd_dev->rbd_client); |
| 5970 | rbd_spec_get(rbd_dev->parent_spec); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5971 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5972 | ret = rbd_dev_image_probe(parent, depth); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5973 | if (ret < 0) |
| 5974 | goto out_err; |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5975 | |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5976 | rbd_dev->parent = parent; |
Alex Elder | a2acd00 | 2013-05-08 22:50:04 -0500 | [diff] [blame] | 5977 | atomic_set(&rbd_dev->parent_ref, 1); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5978 | return 0; |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5979 | |
Ilya Dryomov | 1f2c665 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 5980 | out_err: |
| 5981 | rbd_dev_unparent(rbd_dev); |
Markus Elfring | 1761b22 | 2015-11-23 20:16:45 +0100 | [diff] [blame] | 5982 | rbd_dev_destroy(parent); |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5983 | return ret; |
| 5984 | } |
| 5985 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 5986 | /* |
| 5987 | * rbd_dev->header_rwsem must be locked for write and will be unlocked |
| 5988 | * upon return. |
| 5989 | */ |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 5990 | static int rbd_dev_device_setup(struct rbd_device *rbd_dev) |
Alex Elder | 124afba | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 5991 | { |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5992 | int ret; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5993 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5994 | /* Record our major and minor device numbers. */ |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 5995 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 5996 | if (!single_major) { |
| 5997 | ret = register_blkdev(0, rbd_dev->name); |
| 5998 | if (ret < 0) |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 5999 | goto err_out_unlock; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6000 | |
| 6001 | rbd_dev->major = ret; |
| 6002 | rbd_dev->minor = 0; |
| 6003 | } else { |
| 6004 | rbd_dev->major = rbd_major; |
| 6005 | rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id); |
| 6006 | } |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6007 | |
| 6008 | /* Set up the blkdev mapping. */ |
| 6009 | |
| 6010 | ret = rbd_init_disk(rbd_dev); |
| 6011 | if (ret) |
| 6012 | goto err_out_blkdev; |
| 6013 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6014 | ret = rbd_dev_mapping_set(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6015 | if (ret) |
| 6016 | goto err_out_disk; |
Ilya Dryomov | bc1ecc6 | 2014-08-04 18:04:39 +0400 | [diff] [blame] | 6017 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6018 | set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE); |
Josh Durgin | 22001f6 | 2013-09-30 20:10:04 -0700 | [diff] [blame] | 6019 | set_disk_ro(rbd_dev->disk, rbd_dev->mapping.read_only); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6020 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6021 | dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id); |
| 6022 | ret = device_add(&rbd_dev->dev); |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6023 | if (ret) |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6024 | goto err_out_mapping; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6025 | |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6026 | /* Everything's ready. Announce the disk to the world. */ |
| 6027 | |
Alex Elder | 129b79d | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6028 | set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6029 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6030 | |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6031 | spin_lock(&rbd_dev_list_lock); |
| 6032 | list_add_tail(&rbd_dev->node, &rbd_dev_list); |
| 6033 | spin_unlock(&rbd_dev_list_lock); |
| 6034 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6035 | add_disk(rbd_dev->disk); |
Ilya Dryomov | ca7909e | 2016-08-18 18:38:41 +0200 | [diff] [blame] | 6036 | pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name, |
| 6037 | (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT, |
| 6038 | rbd_dev->header.features); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6039 | |
| 6040 | return ret; |
Alex Elder | 2f82ee5 | 2012-10-30 19:40:33 -0500 | [diff] [blame] | 6041 | |
Alex Elder | f35a4de | 2013-05-06 09:51:29 -0500 | [diff] [blame] | 6042 | err_out_mapping: |
| 6043 | rbd_dev_mapping_clear(rbd_dev); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6044 | err_out_disk: |
| 6045 | rbd_free_disk(rbd_dev); |
| 6046 | err_out_blkdev: |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6047 | if (!single_major) |
| 6048 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6049 | err_out_unlock: |
| 6050 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6051 | return ret; |
| 6052 | } |
| 6053 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6054 | static int rbd_dev_header_name(struct rbd_device *rbd_dev) |
| 6055 | { |
| 6056 | struct rbd_spec *spec = rbd_dev->spec; |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6057 | int ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6058 | |
| 6059 | /* Record the header object name for this rbd image. */ |
| 6060 | |
| 6061 | rbd_assert(rbd_image_format_valid(rbd_dev->image_format)); |
| 6062 | |
Yan, Zheng | 7627151 | 2016-02-03 21:24:49 +0800 | [diff] [blame] | 6063 | rbd_dev->header_oloc.pool = rbd_dev->layout.pool_id; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6064 | if (rbd_dev->image_format == 1) |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6065 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6066 | spec->image_name, RBD_SUFFIX); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6067 | else |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6068 | ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s", |
| 6069 | RBD_HEADER_PREFIX, spec->image_id); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6070 | |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6071 | return ret; |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6072 | } |
| 6073 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6074 | static void rbd_dev_image_release(struct rbd_device *rbd_dev) |
| 6075 | { |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6076 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6077 | rbd_dev->image_format = 0; |
| 6078 | kfree(rbd_dev->spec->image_id); |
| 6079 | rbd_dev->spec->image_id = NULL; |
| 6080 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6081 | rbd_dev_destroy(rbd_dev); |
| 6082 | } |
| 6083 | |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6084 | /* |
| 6085 | * Probe for the existence of the header object for the given rbd |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6086 | * device. If this image is the one being mapped (i.e., not a |
| 6087 | * parent), initiate a watch on its header object before using that |
| 6088 | * object to get detailed information about the rbd image. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6089 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6090 | 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] | 6091 | { |
| 6092 | int ret; |
| 6093 | |
| 6094 | /* |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6095 | * Get the id from the image id object. Unless there's an |
| 6096 | * error, rbd_dev->spec->image_id will be filled in with |
| 6097 | * a dynamically-allocated string, and rbd_dev->image_format |
| 6098 | * will be set to either 1 or 2. |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6099 | */ |
| 6100 | ret = rbd_dev_image_id(rbd_dev); |
| 6101 | if (ret) |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6102 | return ret; |
Alex Elder | c0fba36 | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6103 | |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6104 | ret = rbd_dev_header_name(rbd_dev); |
| 6105 | if (ret) |
| 6106 | goto err_out_format; |
| 6107 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6108 | if (!depth) { |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6109 | ret = rbd_register_watch(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6110 | if (ret) { |
| 6111 | if (ret == -ENOENT) |
| 6112 | pr_info("image %s/%s does not exist\n", |
| 6113 | rbd_dev->spec->pool_name, |
| 6114 | rbd_dev->spec->image_name); |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6115 | goto err_out_format; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6116 | } |
Alex Elder | 1f3ef78 | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6117 | } |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6118 | |
Ilya Dryomov | a720ae0 | 2014-07-23 17:11:19 +0400 | [diff] [blame] | 6119 | ret = rbd_dev_header_info(rbd_dev); |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6120 | if (ret) |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6121 | goto err_out_watch; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6122 | |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6123 | /* |
| 6124 | * If this image is the one being mapped, we have pool name and |
| 6125 | * id, image name and id, and snap name - need to fill snap id. |
| 6126 | * Otherwise this is a parent image, identified by pool, image |
| 6127 | * and snap ids - need to fill in names for those ids. |
| 6128 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6129 | if (!depth) |
Ilya Dryomov | 0407759 | 2014-07-23 17:11:20 +0400 | [diff] [blame] | 6130 | ret = rbd_spec_fill_snap_id(rbd_dev); |
| 6131 | else |
| 6132 | ret = rbd_spec_fill_names(rbd_dev); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6133 | if (ret) { |
| 6134 | if (ret == -ENOENT) |
| 6135 | pr_info("snap %s/%s@%s does not exist\n", |
| 6136 | rbd_dev->spec->pool_name, |
| 6137 | rbd_dev->spec->image_name, |
| 6138 | rbd_dev->spec->snap_name); |
Alex Elder | 33dca39 | 2013-04-30 00:44:33 -0500 | [diff] [blame] | 6139 | goto err_out_probe; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6140 | } |
Alex Elder | 9bb81c9 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6141 | |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6142 | if (rbd_dev->header.features & RBD_FEATURE_LAYERING) { |
| 6143 | ret = rbd_dev_v2_parent_info(rbd_dev); |
| 6144 | if (ret) |
| 6145 | goto err_out_probe; |
| 6146 | |
| 6147 | /* |
| 6148 | * Need to warn users if this image is the one being |
| 6149 | * mapped and has a parent. |
| 6150 | */ |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6151 | if (!depth && rbd_dev->parent_spec) |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6152 | rbd_warn(rbd_dev, |
| 6153 | "WARNING: kernel layering is EXPERIMENTAL!"); |
| 6154 | } |
| 6155 | |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6156 | ret = rbd_dev_probe_parent(rbd_dev, depth); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6157 | if (ret) |
| 6158 | goto err_out_probe; |
Alex Elder | 83a0626 | 2012-10-30 15:47:17 -0500 | [diff] [blame] | 6159 | |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6160 | dout("discovered format %u image, header name is %s\n", |
Ilya Dryomov | c41d13a | 2016-04-29 20:01:25 +0200 | [diff] [blame] | 6161 | rbd_dev->image_format, rbd_dev->header_oid.name); |
Alex Elder | 30d60ba | 2013-05-06 09:51:30 -0500 | [diff] [blame] | 6162 | return 0; |
Ilya Dryomov | e8f59b5 | 2014-07-24 10:42:13 +0400 | [diff] [blame] | 6163 | |
Alex Elder | 6fd48b3 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6164 | err_out_probe: |
| 6165 | rbd_dev_unprobe(rbd_dev); |
Alex Elder | b644de2 | 2013-04-27 09:59:31 -0500 | [diff] [blame] | 6166 | err_out_watch: |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6167 | if (!depth) |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6168 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 332bb12 | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6169 | err_out_format: |
| 6170 | rbd_dev->image_format = 0; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6171 | kfree(rbd_dev->spec->image_id); |
| 6172 | rbd_dev->spec->image_id = NULL; |
Alex Elder | 5655c4d | 2013-04-25 23:15:08 -0500 | [diff] [blame] | 6173 | return ret; |
Alex Elder | a30b71b | 2012-07-10 20:30:11 -0500 | [diff] [blame] | 6174 | } |
| 6175 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6176 | static ssize_t do_rbd_add(struct bus_type *bus, |
| 6177 | const char *buf, |
| 6178 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6179 | { |
Alex Elder | cb8627c | 2012-07-09 21:04:23 -0500 | [diff] [blame] | 6180 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6181 | struct ceph_options *ceph_opts = NULL; |
Alex Elder | 4e9afeb | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6182 | struct rbd_options *rbd_opts = NULL; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6183 | struct rbd_spec *spec = NULL; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6184 | struct rbd_client *rbdc; |
Alex Elder | 51344a3 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6185 | bool read_only; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6186 | int rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6187 | |
| 6188 | if (!try_module_get(THIS_MODULE)) |
| 6189 | return -ENODEV; |
| 6190 | |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6191 | /* parse add command */ |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6192 | rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec); |
Alex Elder | dc79b11 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6193 | if (rc < 0) |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6194 | goto out; |
Alex Elder | a725f65e | 2012-02-02 08:13:30 -0600 | [diff] [blame] | 6195 | |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6196 | rbdc = rbd_get_client(ceph_opts); |
| 6197 | if (IS_ERR(rbdc)) { |
| 6198 | rc = PTR_ERR(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6199 | goto err_out_args; |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6200 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6201 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6202 | /* pick the pool */ |
Ilya Dryomov | 30ba1f0 | 2014-05-13 11:19:27 +0400 | [diff] [blame] | 6203 | rc = rbd_add_get_pool_id(rbdc, spec->pool_name); |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6204 | if (rc < 0) { |
| 6205 | if (rc == -ENOENT) |
| 6206 | pr_info("pool %s does not exist\n", spec->pool_name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6207 | goto err_out_client; |
Ilya Dryomov | 1fe4802 | 2015-03-05 10:47:22 +0300 | [diff] [blame] | 6208 | } |
Alex Elder | c0cd10db | 2013-04-26 09:43:47 -0500 | [diff] [blame] | 6209 | spec->pool_id = (u64)rc; |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6210 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6211 | rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts); |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6212 | if (!rbd_dev) { |
| 6213 | rc = -ENOMEM; |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6214 | goto err_out_client; |
Ilya Dryomov | b51c83c | 2015-10-15 15:38:57 +0200 | [diff] [blame] | 6215 | } |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6216 | rbdc = NULL; /* rbd_dev now owns this */ |
| 6217 | spec = NULL; /* rbd_dev now owns this */ |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6218 | rbd_opts = NULL; /* rbd_dev now owns this */ |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6219 | |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6220 | rbd_dev->config_info = kstrdup(buf, GFP_KERNEL); |
| 6221 | if (!rbd_dev->config_info) { |
| 6222 | rc = -ENOMEM; |
| 6223 | goto err_out_rbd_dev; |
| 6224 | } |
| 6225 | |
Ilya Dryomov | 811c668 | 2016-04-15 16:22:16 +0200 | [diff] [blame] | 6226 | down_write(&rbd_dev->header_rwsem); |
Ilya Dryomov | 6d69bb53 | 2015-10-11 19:38:00 +0200 | [diff] [blame] | 6227 | rc = rbd_dev_image_probe(rbd_dev, 0); |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6228 | if (rc < 0) { |
| 6229 | up_write(&rbd_dev->header_rwsem); |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6230 | goto err_out_rbd_dev; |
Mike Christie | 0d6d1e9c | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6231 | } |
Alex Elder | 05fd6f6 | 2012-08-29 17:11:07 -0500 | [diff] [blame] | 6232 | |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6233 | /* If we are mapping a snapshot it must be marked read-only */ |
| 6234 | |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6235 | read_only = rbd_dev->opts->read_only; |
Alex Elder | 7ce4eef | 2013-05-06 17:40:33 -0500 | [diff] [blame] | 6236 | if (rbd_dev->spec->snap_id != CEPH_NOSNAP) |
| 6237 | read_only = true; |
| 6238 | rbd_dev->mapping.read_only = read_only; |
| 6239 | |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6240 | rc = rbd_dev_device_setup(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6241 | if (rc) { |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6242 | /* |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6243 | * rbd_unregister_watch() can't be moved into |
Ilya Dryomov | e37180c | 2013-12-16 18:02:41 +0200 | [diff] [blame] | 6244 | * rbd_dev_image_release() without refactoring, see |
| 6245 | * commit 1f3ef78861ac. |
| 6246 | */ |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6247 | rbd_unregister_watch(rbd_dev); |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6248 | rbd_dev_image_release(rbd_dev); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6249 | goto out; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6250 | } |
Alex Elder | b536f69 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6251 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6252 | rc = count; |
| 6253 | out: |
| 6254 | module_put(THIS_MODULE); |
| 6255 | return rc; |
Alex Elder | 3abef3b | 2013-05-13 20:35:37 -0500 | [diff] [blame] | 6256 | |
Alex Elder | c53d589 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6257 | err_out_rbd_dev: |
| 6258 | rbd_dev_destroy(rbd_dev); |
Alex Elder | bd4ba65 | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6259 | err_out_client: |
Alex Elder | 9d3997f | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6260 | rbd_put_client(rbdc); |
Alex Elder | 0ddebc0 | 2012-10-25 23:34:41 -0500 | [diff] [blame] | 6261 | err_out_args: |
Alex Elder | 859c31d | 2012-10-25 23:34:42 -0500 | [diff] [blame] | 6262 | rbd_spec_put(spec); |
Ilya Dryomov | d147543 | 2015-06-22 13:24:48 +0300 | [diff] [blame] | 6263 | kfree(rbd_opts); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6264 | goto out; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6265 | } |
| 6266 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6267 | static ssize_t rbd_add(struct bus_type *bus, |
| 6268 | const char *buf, |
| 6269 | size_t count) |
| 6270 | { |
| 6271 | if (single_major) |
| 6272 | return -EINVAL; |
| 6273 | |
| 6274 | return do_rbd_add(bus, buf, count); |
| 6275 | } |
| 6276 | |
| 6277 | static ssize_t rbd_add_single_major(struct bus_type *bus, |
| 6278 | const char *buf, |
| 6279 | size_t count) |
| 6280 | { |
| 6281 | return do_rbd_add(bus, buf, count); |
| 6282 | } |
| 6283 | |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6284 | static void rbd_dev_device_release(struct rbd_device *rbd_dev) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6285 | { |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6286 | rbd_free_disk(rbd_dev); |
Ilya Dryomov | 1643dfa | 2016-08-12 15:45:52 +0200 | [diff] [blame] | 6287 | |
| 6288 | spin_lock(&rbd_dev_list_lock); |
| 6289 | list_del_init(&rbd_dev->node); |
| 6290 | spin_unlock(&rbd_dev_list_lock); |
| 6291 | |
Alex Elder | 200a6a8 | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6292 | clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags); |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6293 | device_del(&rbd_dev->dev); |
Alex Elder | 6d80b13 | 2013-05-06 07:40:30 -0500 | [diff] [blame] | 6294 | rbd_dev_mapping_clear(rbd_dev); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6295 | if (!single_major) |
| 6296 | unregister_blkdev(rbd_dev->major, rbd_dev->name); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6297 | } |
| 6298 | |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6299 | static void rbd_dev_remove_parent(struct rbd_device *rbd_dev) |
| 6300 | { |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6301 | while (rbd_dev->parent) { |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6302 | struct rbd_device *first = rbd_dev; |
| 6303 | struct rbd_device *second = first->parent; |
| 6304 | struct rbd_device *third; |
| 6305 | |
| 6306 | /* |
| 6307 | * Follow to the parent with no grandparent and |
| 6308 | * remove it. |
| 6309 | */ |
| 6310 | while (second && (third = second->parent)) { |
| 6311 | first = second; |
| 6312 | second = third; |
| 6313 | } |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6314 | rbd_assert(second); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6315 | rbd_dev_image_release(second); |
Alex Elder | ad945fc | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6316 | first->parent = NULL; |
| 6317 | first->parent_overlap = 0; |
| 6318 | |
| 6319 | rbd_assert(first->parent_spec); |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6320 | rbd_spec_put(first->parent_spec); |
| 6321 | first->parent_spec = NULL; |
Alex Elder | 05a46af | 2013-04-26 15:44:36 -0500 | [diff] [blame] | 6322 | } |
| 6323 | } |
| 6324 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6325 | static ssize_t do_rbd_remove(struct bus_type *bus, |
| 6326 | const char *buf, |
| 6327 | size_t count) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6328 | { |
| 6329 | struct rbd_device *rbd_dev = NULL; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6330 | struct list_head *tmp; |
| 6331 | int dev_id; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6332 | char opt_buf[6]; |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6333 | bool already = false; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6334 | bool force = false; |
Alex Elder | 0d8189e | 2013-04-27 09:59:30 -0500 | [diff] [blame] | 6335 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6336 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6337 | dev_id = -1; |
| 6338 | opt_buf[0] = '\0'; |
| 6339 | sscanf(buf, "%d %5s", &dev_id, opt_buf); |
| 6340 | if (dev_id < 0) { |
| 6341 | pr_err("dev_id out of range\n"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6342 | return -EINVAL; |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6343 | } |
| 6344 | if (opt_buf[0] != '\0') { |
| 6345 | if (!strcmp(opt_buf, "force")) { |
| 6346 | force = true; |
| 6347 | } else { |
| 6348 | pr_err("bad remove option at '%s'\n", opt_buf); |
| 6349 | return -EINVAL; |
| 6350 | } |
| 6351 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6352 | |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6353 | ret = -ENOENT; |
| 6354 | spin_lock(&rbd_dev_list_lock); |
| 6355 | list_for_each(tmp, &rbd_dev_list) { |
| 6356 | rbd_dev = list_entry(tmp, struct rbd_device, node); |
| 6357 | if (rbd_dev->dev_id == dev_id) { |
| 6358 | ret = 0; |
| 6359 | break; |
| 6360 | } |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6361 | } |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6362 | if (!ret) { |
| 6363 | spin_lock_irq(&rbd_dev->lock); |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6364 | if (rbd_dev->open_count && !force) |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6365 | ret = -EBUSY; |
| 6366 | else |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6367 | already = test_and_set_bit(RBD_DEV_FLAG_REMOVING, |
| 6368 | &rbd_dev->flags); |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6369 | spin_unlock_irq(&rbd_dev->lock); |
| 6370 | } |
| 6371 | spin_unlock(&rbd_dev_list_lock); |
Alex Elder | 82a442d | 2013-05-31 17:40:44 -0500 | [diff] [blame] | 6372 | if (ret < 0 || already) |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6373 | return ret; |
Alex Elder | 751cc0e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6374 | |
Mike Christie | 0276dca | 2016-08-18 18:38:45 +0200 | [diff] [blame] | 6375 | if (force) { |
| 6376 | /* |
| 6377 | * Prevent new IO from being queued and wait for existing |
| 6378 | * IO to complete/fail. |
| 6379 | */ |
| 6380 | blk_mq_freeze_queue(rbd_dev->disk->queue); |
| 6381 | blk_set_queue_dying(rbd_dev->disk->queue); |
| 6382 | } |
| 6383 | |
Ilya Dryomov | ed95b21 | 2016-08-12 16:40:02 +0200 | [diff] [blame] | 6384 | down_write(&rbd_dev->lock_rwsem); |
| 6385 | if (__rbd_is_lock_owner(rbd_dev)) |
| 6386 | rbd_unlock(rbd_dev); |
| 6387 | up_write(&rbd_dev->lock_rwsem); |
Ilya Dryomov | 99d1694 | 2016-08-12 16:11:41 +0200 | [diff] [blame] | 6388 | rbd_unregister_watch(rbd_dev); |
Ilya Dryomov | fca2706 | 2013-12-16 18:02:40 +0200 | [diff] [blame] | 6389 | |
Josh Durgin | 9875201 | 2013-08-29 17:26:31 -0700 | [diff] [blame] | 6390 | /* |
| 6391 | * Don't free anything from rbd_dev->disk until after all |
| 6392 | * notifies are completely processed. Otherwise |
| 6393 | * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting |
| 6394 | * in a potential use after free of rbd_dev->disk or rbd_dev. |
| 6395 | */ |
Ilya Dryomov | dd5ac32 | 2015-10-16 17:09:24 +0200 | [diff] [blame] | 6396 | rbd_dev_device_release(rbd_dev); |
Alex Elder | 8ad42cd | 2013-04-28 23:32:34 -0500 | [diff] [blame] | 6397 | rbd_dev_image_release(rbd_dev); |
Alex Elder | aafb230 | 2012-09-06 16:00:54 -0500 | [diff] [blame] | 6398 | |
Alex Elder | 1ba0f1e | 2013-05-31 15:17:01 -0500 | [diff] [blame] | 6399 | return count; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6400 | } |
| 6401 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6402 | static ssize_t rbd_remove(struct bus_type *bus, |
| 6403 | const char *buf, |
| 6404 | size_t count) |
| 6405 | { |
| 6406 | if (single_major) |
| 6407 | return -EINVAL; |
| 6408 | |
| 6409 | return do_rbd_remove(bus, buf, count); |
| 6410 | } |
| 6411 | |
| 6412 | static ssize_t rbd_remove_single_major(struct bus_type *bus, |
| 6413 | const char *buf, |
| 6414 | size_t count) |
| 6415 | { |
| 6416 | return do_rbd_remove(bus, buf, count); |
| 6417 | } |
| 6418 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6419 | /* |
| 6420 | * create control files in sysfs |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6421 | * /sys/bus/rbd/... |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6422 | */ |
| 6423 | static int rbd_sysfs_init(void) |
| 6424 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6425 | int ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6426 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6427 | ret = device_register(&rbd_root_dev); |
Alex Elder | 2107978 | 2012-01-24 10:08:36 -0600 | [diff] [blame] | 6428 | if (ret < 0) |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6429 | return ret; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6430 | |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6431 | ret = bus_register(&rbd_bus_type); |
| 6432 | if (ret < 0) |
| 6433 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6434 | |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6435 | return ret; |
| 6436 | } |
| 6437 | |
| 6438 | static void rbd_sysfs_cleanup(void) |
| 6439 | { |
Yehuda Sadeh | dfc5606 | 2010-11-19 14:51:04 -0800 | [diff] [blame] | 6440 | bus_unregister(&rbd_bus_type); |
Alex Elder | fed4c14 | 2012-02-07 12:03:36 -0600 | [diff] [blame] | 6441 | device_unregister(&rbd_root_dev); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6442 | } |
| 6443 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6444 | static int rbd_slab_init(void) |
| 6445 | { |
| 6446 | rbd_assert(!rbd_img_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6447 | rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0); |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6448 | if (!rbd_img_request_cache) |
| 6449 | return -ENOMEM; |
| 6450 | |
| 6451 | rbd_assert(!rbd_obj_request_cache); |
Geliang Tang | 03d9440 | 2016-03-13 15:17:32 +0800 | [diff] [blame] | 6452 | rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6453 | if (!rbd_obj_request_cache) |
| 6454 | goto out_err; |
| 6455 | |
| 6456 | rbd_assert(!rbd_segment_name_cache); |
| 6457 | rbd_segment_name_cache = kmem_cache_create("rbd_segment_name", |
Ilya Dryomov | 2d0ebc5 | 2014-01-27 17:40:18 +0200 | [diff] [blame] | 6458 | CEPH_MAX_OID_NAME_LEN + 1, 1, 0, NULL); |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6459 | if (rbd_segment_name_cache) |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6460 | return 0; |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6461 | out_err: |
Julia Lawall | 13bf283 | 2015-09-13 14:15:26 +0200 | [diff] [blame] | 6462 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6463 | rbd_obj_request_cache = NULL; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6464 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6465 | kmem_cache_destroy(rbd_img_request_cache); |
| 6466 | rbd_img_request_cache = NULL; |
| 6467 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6468 | return -ENOMEM; |
| 6469 | } |
| 6470 | |
| 6471 | static void rbd_slab_exit(void) |
| 6472 | { |
Alex Elder | 78c2a44 | 2013-05-01 12:43:04 -0500 | [diff] [blame] | 6473 | rbd_assert(rbd_segment_name_cache); |
| 6474 | kmem_cache_destroy(rbd_segment_name_cache); |
| 6475 | rbd_segment_name_cache = NULL; |
| 6476 | |
Alex Elder | 868311b | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6477 | rbd_assert(rbd_obj_request_cache); |
| 6478 | kmem_cache_destroy(rbd_obj_request_cache); |
| 6479 | rbd_obj_request_cache = NULL; |
| 6480 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6481 | rbd_assert(rbd_img_request_cache); |
| 6482 | kmem_cache_destroy(rbd_img_request_cache); |
| 6483 | rbd_img_request_cache = NULL; |
| 6484 | } |
| 6485 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6486 | static int __init rbd_init(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6487 | { |
| 6488 | int rc; |
| 6489 | |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6490 | if (!libceph_compatible(NULL)) { |
| 6491 | rbd_warn(NULL, "libceph incompatibility (quitting)"); |
Alex Elder | 1e32d34 | 2013-01-30 11:13:33 -0600 | [diff] [blame] | 6492 | return -EINVAL; |
| 6493 | } |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6494 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6495 | rc = rbd_slab_init(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6496 | if (rc) |
| 6497 | return rc; |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6498 | |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6499 | /* |
| 6500 | * The number of active work items is limited by the number of |
Ilya Dryomov | f77303b | 2015-04-22 18:28:13 +0300 | [diff] [blame] | 6501 | * rbd devices * queue depth, so leave @max_active at default. |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6502 | */ |
| 6503 | rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0); |
| 6504 | if (!rbd_wq) { |
| 6505 | rc = -ENOMEM; |
| 6506 | goto err_out_slab; |
| 6507 | } |
| 6508 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6509 | if (single_major) { |
| 6510 | rbd_major = register_blkdev(0, RBD_DRV_NAME); |
| 6511 | if (rbd_major < 0) { |
| 6512 | rc = rbd_major; |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6513 | goto err_out_wq; |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6514 | } |
| 6515 | } |
| 6516 | |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6517 | rc = rbd_sysfs_init(); |
| 6518 | if (rc) |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6519 | goto err_out_blkdev; |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6520 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6521 | if (single_major) |
| 6522 | pr_info("loaded (major %d)\n", rbd_major); |
| 6523 | else |
| 6524 | pr_info("loaded\n"); |
| 6525 | |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6526 | return 0; |
| 6527 | |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6528 | err_out_blkdev: |
| 6529 | if (single_major) |
| 6530 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6531 | err_out_wq: |
| 6532 | destroy_workqueue(rbd_wq); |
Ilya Dryomov | e1b4d96 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6533 | err_out_slab: |
| 6534 | rbd_slab_exit(); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6535 | return rc; |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6536 | } |
| 6537 | |
Alex Elder | cc344fa | 2013-02-19 12:25:56 -0600 | [diff] [blame] | 6538 | static void __exit rbd_exit(void) |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6539 | { |
Ilya Dryomov | ffe312c | 2014-05-20 15:46:04 +0400 | [diff] [blame] | 6540 | ida_destroy(&rbd_dev_id_ida); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6541 | rbd_sysfs_cleanup(); |
Ilya Dryomov | 9b60e70 | 2013-12-13 15:28:57 +0200 | [diff] [blame] | 6542 | if (single_major) |
| 6543 | unregister_blkdev(rbd_major, RBD_DRV_NAME); |
Ilya Dryomov | f5ee37b | 2014-10-09 17:06:01 +0400 | [diff] [blame] | 6544 | destroy_workqueue(rbd_wq); |
Alex Elder | 1c2a9df | 2013-05-01 12:43:03 -0500 | [diff] [blame] | 6545 | rbd_slab_exit(); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | module_init(rbd_init); |
| 6549 | module_exit(rbd_exit); |
| 6550 | |
Alex Elder | d552c61 | 2013-05-31 20:13:09 -0500 | [diff] [blame] | 6551 | MODULE_AUTHOR("Alex Elder <elder@inktank.com>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6552 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); |
| 6553 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6554 | /* following authorship retained from original osdblk.c */ |
| 6555 | MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>"); |
| 6556 | |
Ilya Dryomov | 90da258 | 2013-12-13 15:28:56 +0200 | [diff] [blame] | 6557 | MODULE_DESCRIPTION("RADOS Block Device (RBD) driver"); |
Yehuda Sadeh | 602adf4 | 2010-08-12 16:11:25 -0700 | [diff] [blame] | 6558 | MODULE_LICENSE("GPL"); |