blob: 722422e62b368943586cee49006b494b29611c12 [file] [log] [blame]
Alex Eldere2a58ee2013-04-30 00:44:33 -05001
Yehuda Sadeh602adf42010-08-12 16:11:25 -07002/*
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 Sadehdfc56062010-11-19 14:51:04 -080025 For usage instructions, please refer to:
Yehuda Sadeh602adf42010-08-12 16:11:25 -070026
Yehuda Sadehdfc56062010-11-19 14:51:04 -080027 Documentation/ABI/testing/sysfs-bus-rbd
Yehuda Sadeh602adf42010-08-12 16:11:25 -070028
29 */
30
31#include <linux/ceph/libceph.h>
32#include <linux/ceph/osd_client.h>
33#include <linux/ceph/mon_client.h>
Ilya Dryomoved95b212016-08-12 16:40:02 +020034#include <linux/ceph/cls_lock_client.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070035#include <linux/ceph/decode.h>
Yehuda Sadeh59c2be12011-03-21 15:10:11 -070036#include <linux/parser.h>
Alex Elder30d1cff2013-05-01 12:43:03 -050037#include <linux/bsearch.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070038
39#include <linux/kernel.h>
40#include <linux/device.h>
41#include <linux/module.h>
Christoph Hellwig7ad18af2015-01-13 17:20:04 +010042#include <linux/blk-mq.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070043#include <linux/fs.h>
44#include <linux/blkdev.h>
Alex Elder1c2a9df2013-05-01 12:43:03 -050045#include <linux/slab.h>
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +020046#include <linux/idr.h>
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +040047#include <linux/workqueue.h>
Yehuda Sadeh602adf42010-08-12 16:11:25 -070048
49#include "rbd_types.h"
50
Alex Elderaafb2302012-09-06 16:00:54 -050051#define RBD_DEBUG /* Activate rbd_assert() calls */
52
Alex Elder593a9e72012-02-07 12:03:37 -060053/*
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 Eldera2acd002013-05-08 22:50:04 -050062/*
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 */
68static 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 */
82static 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 Elderf0f8cef2012-01-29 13:57:44 -060095#define RBD_DRV_NAME "rbd"
Yehuda Sadeh602adf42010-08-12 16:11:25 -070096
Ilya Dryomov7e513d42013-12-16 19:26:32 +020097#define RBD_MINORS_PER_MAJOR 256
98#define RBD_SINGLE_MAJOR_PART_SHIFT 4
Yehuda Sadeh602adf42010-08-12 16:11:25 -070099
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200100#define RBD_MAX_PARENT_CHAIN_LEN 16
101
Alex Elderd4b125e2012-07-03 16:01:19 -0500102#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 Elder35d489f2012-07-03 16:01:19 -0500106#define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700107
108#define RBD_SNAP_HEAD_NAME "-"
109
Alex Elder9682fc62013-04-30 00:44:33 -0500110#define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
111
Alex Elder9e15b772012-10-30 19:40:33 -0500112/* 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 Elder1e130192012-07-03 16:01:19 -0500114#define RBD_IMAGE_ID_LEN_MAX 64
Alex Elder9e15b772012-10-30 19:40:33 -0500115
Alex Elder1e130192012-07-03 16:01:19 -0500116#define RBD_OBJ_PREFIX_LEN_MAX 64
Alex Elder589d30e2012-07-10 20:30:11 -0500117
Ilya Dryomoved95b212016-08-12 16:40:02 +0200118#define RBD_NOTIFY_TIMEOUT 5 /* seconds */
Ilya Dryomov99d16942016-08-12 16:11:41 +0200119#define RBD_RETRY_DELAY msecs_to_jiffies(1000)
120
Alex Elderd8891402012-10-09 13:50:17 -0700121/* Feature bits */
122
Ilya Dryomov8767b292017-03-02 19:56:57 +0100123#define RBD_FEATURE_LAYERING (1ULL<<0)
124#define RBD_FEATURE_STRIPINGV2 (1ULL<<1)
125#define RBD_FEATURE_EXCLUSIVE_LOCK (1ULL<<2)
126#define RBD_FEATURE_DATA_POOL (1ULL<<7)
Ilya Dryomove5734272018-01-16 15:41:54 +0100127#define RBD_FEATURE_OPERATIONS (1ULL<<8)
Ilya Dryomov8767b292017-03-02 19:56:57 +0100128
Ilya Dryomoved95b212016-08-12 16:40:02 +0200129#define RBD_FEATURES_ALL (RBD_FEATURE_LAYERING | \
130 RBD_FEATURE_STRIPINGV2 | \
Ilya Dryomov7e973322017-01-25 18:16:22 +0100131 RBD_FEATURE_EXCLUSIVE_LOCK | \
Ilya Dryomove5734272018-01-16 15:41:54 +0100132 RBD_FEATURE_DATA_POOL | \
133 RBD_FEATURE_OPERATIONS)
Alex Elderd8891402012-10-09 13:50:17 -0700134
135/* Features supported by this (client software) implementation. */
136
Alex Elder770eba62012-10-25 23:34:40 -0500137#define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
Alex Elderd8891402012-10-09 13:50:17 -0700138
Alex Elder81a89792012-02-02 08:13:30 -0600139/*
140 * An RBD device name will be "rbd#", where the "rbd" comes from
141 * RBD_DRV_NAME above, and # is a unique integer identifier.
Alex Elder81a89792012-02-02 08:13:30 -0600142 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700143#define DEV_NAME_LEN 32
144
145/*
146 * block device image metadata (in-memory version)
147 */
148struct rbd_image_header {
Alex Elderf35a4de2013-05-06 09:51:29 -0500149 /* These six fields never change for a given rbd image */
Alex Elder849b4262012-07-09 21:04:24 -0500150 char *object_prefix;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700151 __u8 obj_order;
Alex Elderf35a4de2013-05-06 09:51:29 -0500152 u64 stripe_unit;
153 u64 stripe_count;
Ilya Dryomov7e973322017-01-25 18:16:22 +0100154 s64 data_pool_id;
Alex Elderf35a4de2013-05-06 09:51:29 -0500155 u64 features; /* Might be changeable someday? */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700156
Alex Elderf84344f2012-08-31 17:29:51 -0500157 /* The remaining fields need to be updated occasionally */
158 u64 image_size;
159 struct ceph_snap_context *snapc;
Alex Elderf35a4de2013-05-06 09:51:29 -0500160 char *snap_names; /* format 1 only */
161 u64 *snap_sizes; /* format 1 only */
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700162};
163
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500164/*
165 * An rbd image specification.
166 *
167 * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
Alex Elderc66c6e02012-11-01 08:39:26 -0500168 * identify an image. Each rbd_dev structure includes a pointer to
169 * an rbd_spec structure that encapsulates this identity.
170 *
171 * Each of the id's in an rbd_spec has an associated name. For a
172 * user-mapped image, the names are supplied and the id's associated
173 * with them are looked up. For a layered image, a parent image is
174 * defined by the tuple, and the names are looked up.
175 *
176 * An rbd_dev structure contains a parent_spec pointer which is
177 * non-null if the image it represents is a child in a layered
178 * image. This pointer will refer to the rbd_spec structure used
179 * by the parent rbd_dev for its own identity (i.e., the structure
180 * is shared between the parent and child).
181 *
182 * Since these structures are populated once, during the discovery
183 * phase of image construction, they are effectively immutable so
184 * we make no effort to synchronize access to them.
185 *
186 * Note that code herein does not assume the image name is known (it
187 * could be a null pointer).
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500188 */
189struct rbd_spec {
190 u64 pool_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500191 const char *pool_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500192
Alex Elderecb4dc22013-04-26 09:43:47 -0500193 const char *image_id;
194 const char *image_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500195
196 u64 snap_id;
Alex Elderecb4dc22013-04-26 09:43:47 -0500197 const char *snap_name;
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500198
199 struct kref kref;
200};
201
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700202/*
Alex Elderf0f8cef2012-01-29 13:57:44 -0600203 * an instance of the client. multiple devices may share an rbd client.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700204 */
205struct rbd_client {
206 struct ceph_client *client;
207 struct kref kref;
208 struct list_head node;
209};
210
Alex Elderbf0d5f502012-11-22 00:00:08 -0600211struct rbd_img_request;
212typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
213
214#define BAD_WHICH U32_MAX /* Good which or bad which, which? */
215
216struct rbd_obj_request;
217typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
218
Alex Elder9969ebc2013-01-18 12:31:10 -0600219enum obj_request_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100220 OBJ_REQUEST_NODATA = 1,
Ilya Dryomov5359a172018-01-20 10:30:10 +0100221 OBJ_REQUEST_BIO, /* pointer into provided bio (list) */
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100222 OBJ_REQUEST_PAGES,
Alex Elder9969ebc2013-01-18 12:31:10 -0600223};
Alex Elderbf0d5f502012-11-22 00:00:08 -0600224
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800225enum obj_operation_type {
Ilya Dryomova1fbb5e2018-01-16 12:15:02 +0100226 OBJ_OP_READ = 1,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800227 OBJ_OP_WRITE,
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800228 OBJ_OP_DISCARD,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800229};
230
Alex Elder926f9b32013-02-11 12:33:24 -0600231enum obj_req_flags {
232 OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
Alex Elder6365d332013-02-11 12:33:24 -0600233 OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
Alex Elder5679c592013-02-11 12:33:24 -0600234 OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
235 OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
Alex Elder926f9b32013-02-11 12:33:24 -0600236};
237
Alex Elderbf0d5f502012-11-22 00:00:08 -0600238struct rbd_obj_request {
Ilya Dryomova90bb0c2017-01-25 18:16:23 +0100239 u64 object_no;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600240 u64 offset; /* object start byte */
241 u64 length; /* bytes from offset */
Alex Elder926f9b32013-02-11 12:33:24 -0600242 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600243
Alex Elderc5b5ef62013-02-11 12:33:24 -0600244 /*
245 * An object request associated with an image will have its
246 * img_data flag set; a standalone object request will not.
247 *
248 * A standalone object request will have which == BAD_WHICH
249 * and a null obj_request pointer.
250 *
251 * An object request initiated in support of a layered image
252 * object (to check for its existence before a write) will
253 * have which == BAD_WHICH and a non-null obj_request pointer.
254 *
255 * Finally, an object request for rbd image data will have
256 * which != BAD_WHICH, and will have a non-null img_request
257 * pointer. The value of which will be in the range
258 * 0..(img_request->obj_request_count-1).
259 */
260 union {
261 struct rbd_obj_request *obj_request; /* STAT op */
262 struct {
263 struct rbd_img_request *img_request;
264 u64 img_offset;
265 /* links for img_request->obj_requests list */
266 struct list_head links;
267 };
268 };
Alex Elderbf0d5f502012-11-22 00:00:08 -0600269 u32 which; /* posn image request list */
270
271 enum obj_request_type type;
Alex Elder788e2df2013-01-17 12:25:27 -0600272 union {
Ilya Dryomov5359a172018-01-20 10:30:10 +0100273 struct ceph_bio_iter bio_pos;
Alex Elder788e2df2013-01-17 12:25:27 -0600274 struct {
275 struct page **pages;
276 u32 page_count;
277 };
278 };
Alex Elder0eefd472013-04-19 15:34:50 -0500279 struct page **copyup_pages;
Alex Elderebda6402013-05-10 16:29:22 -0500280 u32 copyup_page_count;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600281
282 struct ceph_osd_request *osd_req;
283
284 u64 xferred; /* bytes transferred */
Sage Weil1b83bef2013-02-25 16:11:12 -0800285 int result;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600286
287 rbd_obj_callback_t callback;
288
289 struct kref kref;
290};
291
Alex Elder0c425242013-02-08 09:55:49 -0600292enum img_req_flags {
Alex Elder9849e982013-01-24 16:13:36 -0600293 IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
294 IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
Alex Elderd0b2e942013-01-24 16:13:36 -0600295 IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800296 IMG_REQ_DISCARD, /* discard: normal = 0, discard request = 1 */
Alex Elder0c425242013-02-08 09:55:49 -0600297};
298
Alex Elderbf0d5f502012-11-22 00:00:08 -0600299struct rbd_img_request {
Alex Elderbf0d5f502012-11-22 00:00:08 -0600300 struct rbd_device *rbd_dev;
301 u64 offset; /* starting image byte offset */
302 u64 length; /* byte count from offset */
Alex Elder0c425242013-02-08 09:55:49 -0600303 unsigned long flags;
Alex Elderbf0d5f502012-11-22 00:00:08 -0600304 union {
Alex Elder9849e982013-01-24 16:13:36 -0600305 u64 snap_id; /* for reads */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600306 struct ceph_snap_context *snapc; /* for writes */
Alex Elder9849e982013-01-24 16:13:36 -0600307 };
308 union {
309 struct request *rq; /* block request */
310 struct rbd_obj_request *obj_request; /* obj req initiator */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600311 };
312 spinlock_t completion_lock;/* protects next_completion */
313 u32 next_completion;
314 rbd_img_callback_t callback;
Alex Elder55f27e02013-04-10 12:34:25 -0500315 u64 xferred;/* aggregate bytes transferred */
Alex Eldera5a337d2013-01-24 16:13:36 -0600316 int result; /* first nonzero obj_request result */
Alex Elderbf0d5f502012-11-22 00:00:08 -0600317
318 u32 obj_request_count;
319 struct list_head obj_requests; /* rbd_obj_request structs */
320
321 struct kref kref;
322};
323
324#define for_each_obj_request(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600325 list_for_each_entry(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600326#define for_each_obj_request_from(ireq, oreq) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600327 list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600328#define for_each_obj_request_safe(ireq, oreq, n) \
Alex Elderef06f4d32013-02-08 09:55:48 -0600329 list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
Alex Elderbf0d5f502012-11-22 00:00:08 -0600330
Ilya Dryomov99d16942016-08-12 16:11:41 +0200331enum rbd_watch_state {
332 RBD_WATCH_STATE_UNREGISTERED,
333 RBD_WATCH_STATE_REGISTERED,
334 RBD_WATCH_STATE_ERROR,
335};
336
Ilya Dryomoved95b212016-08-12 16:40:02 +0200337enum rbd_lock_state {
338 RBD_LOCK_STATE_UNLOCKED,
339 RBD_LOCK_STATE_LOCKED,
340 RBD_LOCK_STATE_RELEASING,
341};
342
343/* WatchNotify::ClientId */
344struct rbd_client_id {
345 u64 gid;
346 u64 handle;
347};
348
Alex Elderf84344f2012-08-31 17:29:51 -0500349struct rbd_mapping {
Alex Elder99c1f082012-08-30 14:42:15 -0500350 u64 size;
Alex Elder34b13182012-07-13 20:35:12 -0500351 u64 features;
Alex Elderf84344f2012-08-31 17:29:51 -0500352};
353
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700354/*
355 * a single device
356 */
357struct rbd_device {
Alex Elderde71a292012-07-03 16:01:19 -0500358 int dev_id; /* blkdev unique id */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700359
360 int major; /* blkdev assigned major */
Ilya Dryomovdd82fff2013-12-13 15:28:57 +0200361 int minor;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700362 struct gendisk *disk; /* blkdev's gendisk and rq */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700363
Alex Eldera30b71b2012-07-10 20:30:11 -0500364 u32 image_format; /* Either 1 or 2 */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700365 struct rbd_client *rbd_client;
366
367 char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
368
Alex Elderb82d1672013-01-14 12:43:31 -0600369 spinlock_t lock; /* queue, flags, open_count */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700370
371 struct rbd_image_header header;
Alex Elderb82d1672013-01-14 12:43:31 -0600372 unsigned long flags; /* possibly lock protected */
Alex Elder0d7dbfc2012-10-25 23:34:41 -0500373 struct rbd_spec *spec;
Ilya Dryomovd1475432015-06-22 13:24:48 +0300374 struct rbd_options *opts;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +0200375 char *config_info; /* add{,_single_major} string */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700376
Ilya Dryomovc41d13a2016-04-29 20:01:25 +0200377 struct ceph_object_id header_oid;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200378 struct ceph_object_locator header_oloc;
Alex Elder971f8392012-10-25 23:34:41 -0500379
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200380 struct ceph_file_layout layout; /* used for all rbd requests */
Alex Elder0903e872012-11-14 12:25:19 -0600381
Ilya Dryomov99d16942016-08-12 16:11:41 +0200382 struct mutex watch_mutex;
383 enum rbd_watch_state watch_state;
Ilya Dryomov922dab62016-05-26 01:15:02 +0200384 struct ceph_osd_linger_request *watch_handle;
Ilya Dryomov99d16942016-08-12 16:11:41 +0200385 u64 watch_cookie;
386 struct delayed_work watch_dwork;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700387
Ilya Dryomoved95b212016-08-12 16:40:02 +0200388 struct rw_semaphore lock_rwsem;
389 enum rbd_lock_state lock_state;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +0200390 char lock_cookie[32];
Ilya Dryomoved95b212016-08-12 16:40:02 +0200391 struct rbd_client_id owner_cid;
392 struct work_struct acquired_lock_work;
393 struct work_struct released_lock_work;
394 struct delayed_work lock_dwork;
395 struct work_struct unlock_work;
396 wait_queue_head_t lock_waitq;
397
Ilya Dryomov1643dfa2016-08-12 15:45:52 +0200398 struct workqueue_struct *task_wq;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700399
Alex Elder86b00e02012-10-25 23:34:42 -0500400 struct rbd_spec *parent_spec;
401 u64 parent_overlap;
Alex Eldera2acd002013-05-08 22:50:04 -0500402 atomic_t parent_ref;
Alex Elder2f82ee52012-10-30 19:40:33 -0500403 struct rbd_device *parent;
Alex Elder86b00e02012-10-25 23:34:42 -0500404
Christoph Hellwig7ad18af2015-01-13 17:20:04 +0100405 /* Block layer tags. */
406 struct blk_mq_tag_set tag_set;
407
Josh Durginc6666012011-11-21 17:11:12 -0800408 /* protects updating the header */
409 struct rw_semaphore header_rwsem;
Alex Elderf84344f2012-08-31 17:29:51 -0500410
411 struct rbd_mapping mapping;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700412
413 struct list_head node;
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800414
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800415 /* sysfs related */
416 struct device dev;
Alex Elderb82d1672013-01-14 12:43:31 -0600417 unsigned long open_count; /* protected by lock */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800418};
419
Alex Elderb82d1672013-01-14 12:43:31 -0600420/*
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200421 * Flag bits for rbd_dev->flags:
422 * - REMOVING (which is coupled with rbd_dev->open_count) is protected
423 * by rbd_dev->lock
424 * - BLACKLISTED is protected by rbd_dev->lock_rwsem
Alex Elderb82d1672013-01-14 12:43:31 -0600425 */
Alex Elder6d292902013-01-14 12:43:31 -0600426enum rbd_dev_flags {
427 RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
Alex Elderb82d1672013-01-14 12:43:31 -0600428 RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
Ilya Dryomov87c0fde2016-09-29 13:41:05 +0200429 RBD_DEV_FLAG_BLACKLISTED, /* our ceph_client is blacklisted */
Alex Elder6d292902013-01-14 12:43:31 -0600430};
431
Alex Eldercfbf6372013-05-31 17:40:45 -0500432static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
Alex Eldere124a82f2012-01-29 13:57:44 -0600433
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700434static LIST_HEAD(rbd_dev_list); /* devices */
Alex Eldere124a82f2012-01-29 13:57:44 -0600435static DEFINE_SPINLOCK(rbd_dev_list_lock);
436
Alex Elder432b8582012-01-29 13:57:44 -0600437static LIST_HEAD(rbd_client_list); /* clients */
438static DEFINE_SPINLOCK(rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700439
Alex Elder78c2a442013-05-01 12:43:04 -0500440/* Slab caches for frequently-allocated structures */
441
Alex Elder1c2a9df2013-05-01 12:43:03 -0500442static struct kmem_cache *rbd_img_request_cache;
Alex Elder868311b2013-05-01 12:43:03 -0500443static struct kmem_cache *rbd_obj_request_cache;
Alex Elder1c2a9df2013-05-01 12:43:03 -0500444
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200445static int rbd_major;
Ilya Dryomovf8a22fc2013-12-13 15:28:57 +0200446static DEFINE_IDA(rbd_dev_id_ida);
447
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +0400448static struct workqueue_struct *rbd_wq;
449
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200450/*
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100451 * single-major requires >= 0.75 version of userspace rbd utility.
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200452 */
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100453static bool single_major = true;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200454module_param(single_major, bool, S_IRUGO);
Ilya Dryomov3cfa3b12017-11-13 10:35:40 +0100455MODULE_PARM_DESC(single_major, "Use a single major number for all rbd devices (default: true)");
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200456
Alex Elder3d7efd12013-04-19 15:34:50 -0500457static int rbd_img_request_submit(struct rbd_img_request *img_request);
458
Alex Elderf0f8cef2012-01-29 13:57:44 -0600459static ssize_t rbd_add(struct bus_type *bus, const char *buf,
460 size_t count);
461static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
462 size_t count);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200463static ssize_t rbd_add_single_major(struct bus_type *bus, const char *buf,
464 size_t count);
465static ssize_t rbd_remove_single_major(struct bus_type *bus, const char *buf,
466 size_t count);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +0200467static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth);
Alex Eldera2acd002013-05-08 22:50:04 -0500468static void rbd_spec_put(struct rbd_spec *spec);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600469
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200470static int rbd_dev_id_to_minor(int dev_id)
471{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200472 return dev_id << RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200473}
474
475static int minor_to_rbd_dev_id(int minor)
476{
Ilya Dryomov7e513d42013-12-16 19:26:32 +0200477 return minor >> RBD_SINGLE_MAJOR_PART_SHIFT;
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200478}
479
Ilya Dryomoved95b212016-08-12 16:40:02 +0200480static bool __rbd_is_lock_owner(struct rbd_device *rbd_dev)
481{
482 return rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED ||
483 rbd_dev->lock_state == RBD_LOCK_STATE_RELEASING;
484}
485
486static bool rbd_is_lock_owner(struct rbd_device *rbd_dev)
487{
488 bool is_lock_owner;
489
490 down_read(&rbd_dev->lock_rwsem);
491 is_lock_owner = __rbd_is_lock_owner(rbd_dev);
492 up_read(&rbd_dev->lock_rwsem);
493 return is_lock_owner;
494}
495
Ilya Dryomov8767b292017-03-02 19:56:57 +0100496static ssize_t rbd_supported_features_show(struct bus_type *bus, char *buf)
497{
498 return sprintf(buf, "0x%llx\n", RBD_FEATURES_SUPPORTED);
499}
500
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700501static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
502static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200503static BUS_ATTR(add_single_major, S_IWUSR, NULL, rbd_add_single_major);
504static BUS_ATTR(remove_single_major, S_IWUSR, NULL, rbd_remove_single_major);
Ilya Dryomov8767b292017-03-02 19:56:57 +0100505static BUS_ATTR(supported_features, S_IRUGO, rbd_supported_features_show, NULL);
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700506
507static struct attribute *rbd_bus_attrs[] = {
508 &bus_attr_add.attr,
509 &bus_attr_remove.attr,
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200510 &bus_attr_add_single_major.attr,
511 &bus_attr_remove_single_major.attr,
Ilya Dryomov8767b292017-03-02 19:56:57 +0100512 &bus_attr_supported_features.attr,
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700513 NULL,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600514};
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200515
516static umode_t rbd_bus_is_visible(struct kobject *kobj,
517 struct attribute *attr, int index)
518{
Ilya Dryomov9b60e702013-12-13 15:28:57 +0200519 if (!single_major &&
520 (attr == &bus_attr_add_single_major.attr ||
521 attr == &bus_attr_remove_single_major.attr))
522 return 0;
523
Ilya Dryomov92c76dc2013-12-13 15:28:57 +0200524 return attr->mode;
525}
526
527static const struct attribute_group rbd_bus_group = {
528 .attrs = rbd_bus_attrs,
529 .is_visible = rbd_bus_is_visible,
530};
531__ATTRIBUTE_GROUPS(rbd_bus);
Alex Elderf0f8cef2012-01-29 13:57:44 -0600532
533static struct bus_type rbd_bus_type = {
534 .name = "rbd",
Greg Kroah-Hartmanb15a21d2013-08-23 14:24:28 -0700535 .bus_groups = rbd_bus_groups,
Alex Elderf0f8cef2012-01-29 13:57:44 -0600536};
537
538static void rbd_root_dev_release(struct device *dev)
539{
540}
541
542static struct device rbd_root_dev = {
543 .init_name = "rbd",
544 .release = rbd_root_dev_release,
545};
546
Alex Elder06ecc6c2012-11-01 10:17:15 -0500547static __printf(2, 3)
548void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
549{
550 struct va_format vaf;
551 va_list args;
552
553 va_start(args, fmt);
554 vaf.fmt = fmt;
555 vaf.va = &args;
556
557 if (!rbd_dev)
558 printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
559 else if (rbd_dev->disk)
560 printk(KERN_WARNING "%s: %s: %pV\n",
561 RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
562 else if (rbd_dev->spec && rbd_dev->spec->image_name)
563 printk(KERN_WARNING "%s: image %s: %pV\n",
564 RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
565 else if (rbd_dev->spec && rbd_dev->spec->image_id)
566 printk(KERN_WARNING "%s: id %s: %pV\n",
567 RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
568 else /* punt */
569 printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
570 RBD_DRV_NAME, rbd_dev, &vaf);
571 va_end(args);
572}
573
Alex Elderaafb2302012-09-06 16:00:54 -0500574#ifdef RBD_DEBUG
575#define rbd_assert(expr) \
576 if (unlikely(!(expr))) { \
577 printk(KERN_ERR "\nAssertion failure in %s() " \
578 "at line %d:\n\n" \
579 "\trbd_assert(%s);\n\n", \
580 __func__, __LINE__, #expr); \
581 BUG(); \
582 }
583#else /* !RBD_DEBUG */
584# define rbd_assert(expr) ((void) 0)
585#endif /* !RBD_DEBUG */
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800586
Ilya Dryomov27617132015-07-16 17:36:11 +0300587static void rbd_osd_copyup_callback(struct rbd_obj_request *obj_request);
Alex Elderb454e362013-04-19 15:34:50 -0500588static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
Alex Elder05a46af2013-04-26 15:44:36 -0500589static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
590static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
Alex Elder8b3e1a52013-01-24 16:13:36 -0600591
Alex Eldercc4a38bd2013-04-30 00:44:33 -0500592static int rbd_dev_refresh(struct rbd_device *rbd_dev);
Alex Elder2df3fac2013-05-06 09:51:30 -0500593static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
Ilya Dryomova720ae02014-07-23 17:11:19 +0400594static int rbd_dev_header_info(struct rbd_device *rbd_dev);
Ilya Dryomove8f59b52014-07-24 10:42:13 +0400595static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev);
Alex Elder54cac612013-04-30 00:44:33 -0500596static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
597 u64 snap_id);
Alex Elder2ad3d712013-04-30 00:44:33 -0500598static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
599 u8 *order, u64 *snap_size);
600static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
601 u64 *snap_features);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700602
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700603static int rbd_open(struct block_device *bdev, fmode_t mode)
604{
Alex Elderf0f8cef2012-01-29 13:57:44 -0600605 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600606 bool removing = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700607
Alex Eldera14ea262013-02-05 13:23:12 -0600608 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600609 if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
610 removing = true;
611 else
612 rbd_dev->open_count++;
Alex Eldera14ea262013-02-05 13:23:12 -0600613 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600614 if (removing)
615 return -ENOENT;
616
Alex Elderc3e946c2012-11-16 09:29:16 -0600617 (void) get_device(&rbd_dev->dev);
Alex Elder340c7a22012-08-10 13:12:07 -0700618
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700619 return 0;
620}
621
Al Virodb2a1442013-05-05 21:52:57 -0400622static void rbd_release(struct gendisk *disk, fmode_t mode)
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800623{
624 struct rbd_device *rbd_dev = disk->private_data;
Alex Elderb82d1672013-01-14 12:43:31 -0600625 unsigned long open_count_before;
626
Alex Eldera14ea262013-02-05 13:23:12 -0600627 spin_lock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600628 open_count_before = rbd_dev->open_count--;
Alex Eldera14ea262013-02-05 13:23:12 -0600629 spin_unlock_irq(&rbd_dev->lock);
Alex Elderb82d1672013-01-14 12:43:31 -0600630 rbd_assert(open_count_before > 0);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800631
Alex Elderc3e946c2012-11-16 09:29:16 -0600632 put_device(&rbd_dev->dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800633}
634
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800635static int rbd_ioctl_set_ro(struct rbd_device *rbd_dev, unsigned long arg)
636{
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200637 int ro;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800638
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200639 if (get_user(ro, (int __user *)arg))
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800640 return -EFAULT;
641
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200642 /* Snapshots can't be marked read-write */
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800643 if (rbd_dev->spec->snap_id != CEPH_NOSNAP && !ro)
644 return -EROFS;
645
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200646 /* Let blkdev_roset() handle it */
647 return -ENOTTY;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800648}
649
650static int rbd_ioctl(struct block_device *bdev, fmode_t mode,
651 unsigned int cmd, unsigned long arg)
652{
653 struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
Ilya Dryomov1de797b2017-10-12 12:35:19 +0200654 int ret;
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800655
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800656 switch (cmd) {
657 case BLKROSET:
658 ret = rbd_ioctl_set_ro(rbd_dev, arg);
659 break;
660 default:
661 ret = -ENOTTY;
662 }
663
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800664 return ret;
665}
666
667#ifdef CONFIG_COMPAT
668static int rbd_compat_ioctl(struct block_device *bdev, fmode_t mode,
669 unsigned int cmd, unsigned long arg)
670{
671 return rbd_ioctl(bdev, mode, cmd, arg);
672}
673#endif /* CONFIG_COMPAT */
674
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700675static const struct block_device_operations rbd_bd_ops = {
676 .owner = THIS_MODULE,
677 .open = rbd_open,
Yehuda Sadehdfc56062010-11-19 14:51:04 -0800678 .release = rbd_release,
Guangliang Zhao131fd9f2013-09-24 11:25:36 +0800679 .ioctl = rbd_ioctl,
680#ifdef CONFIG_COMPAT
681 .compat_ioctl = rbd_compat_ioctl,
682#endif
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700683};
684
685/*
Alex Elder7262cfc2013-05-16 15:04:20 -0500686 * Initialize an rbd client instance. Success or not, this function
Alex Eldercfbf6372013-05-31 17:40:45 -0500687 * consumes ceph_opts. Caller holds client_mutex.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700688 */
Alex Elderf8c38922012-08-10 13:12:07 -0700689static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700690{
691 struct rbd_client *rbdc;
692 int ret = -ENOMEM;
693
Alex Elder37206ee2013-02-20 17:32:08 -0600694 dout("%s:\n", __func__);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700695 rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
696 if (!rbdc)
697 goto out_opt;
698
699 kref_init(&rbdc->kref);
700 INIT_LIST_HEAD(&rbdc->node);
701
Ilya Dryomov74da4a0f2017-03-03 18:16:07 +0100702 rbdc->client = ceph_create_client(ceph_opts, rbdc);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700703 if (IS_ERR(rbdc->client))
Alex Elder08f75462013-05-29 11:19:00 -0500704 goto out_rbdc;
Alex Elder43ae4702012-07-03 16:01:18 -0500705 ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700706
707 ret = ceph_open_session(rbdc->client);
708 if (ret < 0)
Alex Elder08f75462013-05-29 11:19:00 -0500709 goto out_client;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700710
Alex Elder432b8582012-01-29 13:57:44 -0600711 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700712 list_add_tail(&rbdc->node, &rbd_client_list);
Alex Elder432b8582012-01-29 13:57:44 -0600713 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700714
Alex Elder37206ee2013-02-20 17:32:08 -0600715 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Elderbc534d82012-01-29 13:57:44 -0600716
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700717 return rbdc;
Alex Elder08f75462013-05-29 11:19:00 -0500718out_client:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700719 ceph_destroy_client(rbdc->client);
Alex Elder08f75462013-05-29 11:19:00 -0500720out_rbdc:
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700721 kfree(rbdc);
722out_opt:
Alex Elder43ae4702012-07-03 16:01:18 -0500723 if (ceph_opts)
724 ceph_destroy_options(ceph_opts);
Alex Elder37206ee2013-02-20 17:32:08 -0600725 dout("%s: error %d\n", __func__, ret);
726
Vasiliy Kulikov28f259b2010-09-26 12:59:37 +0400727 return ERR_PTR(ret);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700728}
729
Alex Elder2f82ee52012-10-30 19:40:33 -0500730static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
731{
732 kref_get(&rbdc->kref);
733
734 return rbdc;
735}
736
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700737/*
Alex Elder1f7ba332012-08-10 13:12:07 -0700738 * Find a ceph client with specific addr and configuration. If
739 * found, bump its reference count.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700740 */
Alex Elder1f7ba332012-08-10 13:12:07 -0700741static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700742{
743 struct rbd_client *client_node;
Alex Elder1f7ba332012-08-10 13:12:07 -0700744 bool found = false;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700745
Alex Elder43ae4702012-07-03 16:01:18 -0500746 if (ceph_opts->flags & CEPH_OPT_NOSHARE)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700747 return NULL;
748
Alex Elder1f7ba332012-08-10 13:12:07 -0700749 spin_lock(&rbd_client_list_lock);
750 list_for_each_entry(client_node, &rbd_client_list, node) {
751 if (!ceph_compare_options(ceph_opts, client_node->client)) {
Alex Elder2f82ee52012-10-30 19:40:33 -0500752 __rbd_get_client(client_node);
753
Alex Elder1f7ba332012-08-10 13:12:07 -0700754 found = true;
755 break;
756 }
757 }
758 spin_unlock(&rbd_client_list_lock);
759
760 return found ? client_node : NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700761}
762
763/*
Ilya Dryomov210c1042015-06-22 13:24:48 +0300764 * (Per device) rbd map options
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700765 */
766enum {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300767 Opt_queue_depth,
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700768 Opt_last_int,
769 /* int args above */
770 Opt_last_string,
771 /* string args above */
Alex Eldercc0538b2012-08-10 13:12:07 -0700772 Opt_read_only,
773 Opt_read_write,
Ilya Dryomov80de1912016-09-20 14:23:17 +0200774 Opt_lock_on_read,
Ilya Dryomove010dd02017-04-13 12:17:39 +0200775 Opt_exclusive,
Ilya Dryomov210c1042015-06-22 13:24:48 +0300776 Opt_err
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700777};
778
Alex Elder43ae4702012-07-03 16:01:18 -0500779static match_table_t rbd_opts_tokens = {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300780 {Opt_queue_depth, "queue_depth=%d"},
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700781 /* int args above */
782 /* string args above */
Alex Elderbe466c12012-10-22 11:31:26 -0500783 {Opt_read_only, "read_only"},
Alex Eldercc0538b2012-08-10 13:12:07 -0700784 {Opt_read_only, "ro"}, /* Alternate spelling */
785 {Opt_read_write, "read_write"},
786 {Opt_read_write, "rw"}, /* Alternate spelling */
Ilya Dryomov80de1912016-09-20 14:23:17 +0200787 {Opt_lock_on_read, "lock_on_read"},
Ilya Dryomove010dd02017-04-13 12:17:39 +0200788 {Opt_exclusive, "exclusive"},
Ilya Dryomov210c1042015-06-22 13:24:48 +0300789 {Opt_err, NULL}
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700790};
791
Alex Elder98571b52013-01-20 14:44:42 -0600792struct rbd_options {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300793 int queue_depth;
Alex Elder98571b52013-01-20 14:44:42 -0600794 bool read_only;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200795 bool lock_on_read;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200796 bool exclusive;
Alex Elder98571b52013-01-20 14:44:42 -0600797};
798
Ilya Dryomovb5584182015-06-23 16:21:19 +0300799#define RBD_QUEUE_DEPTH_DEFAULT BLKDEV_MAX_RQ
Alex Elder98571b52013-01-20 14:44:42 -0600800#define RBD_READ_ONLY_DEFAULT false
Ilya Dryomov80de1912016-09-20 14:23:17 +0200801#define RBD_LOCK_ON_READ_DEFAULT false
Ilya Dryomove010dd02017-04-13 12:17:39 +0200802#define RBD_EXCLUSIVE_DEFAULT false
Alex Elder98571b52013-01-20 14:44:42 -0600803
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700804static int parse_rbd_opts_token(char *c, void *private)
805{
Alex Elder43ae4702012-07-03 16:01:18 -0500806 struct rbd_options *rbd_opts = private;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700807 substring_t argstr[MAX_OPT_ARGS];
808 int token, intval, ret;
809
Alex Elder43ae4702012-07-03 16:01:18 -0500810 token = match_token(c, rbd_opts_tokens, argstr);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700811 if (token < Opt_last_int) {
812 ret = match_int(&argstr[0], &intval);
813 if (ret < 0) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300814 pr_err("bad mount option arg (not int) at '%s'\n", c);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700815 return ret;
816 }
817 dout("got int token %d val %d\n", token, intval);
818 } else if (token > Opt_last_int && token < Opt_last_string) {
Ilya Dryomov210c1042015-06-22 13:24:48 +0300819 dout("got string token %d val %s\n", token, argstr[0].from);
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700820 } else {
821 dout("got token %d\n", token);
822 }
823
824 switch (token) {
Ilya Dryomovb5584182015-06-23 16:21:19 +0300825 case Opt_queue_depth:
826 if (intval < 1) {
827 pr_err("queue_depth out of range\n");
828 return -EINVAL;
829 }
830 rbd_opts->queue_depth = intval;
831 break;
Alex Eldercc0538b2012-08-10 13:12:07 -0700832 case Opt_read_only:
833 rbd_opts->read_only = true;
834 break;
835 case Opt_read_write:
836 rbd_opts->read_only = false;
837 break;
Ilya Dryomov80de1912016-09-20 14:23:17 +0200838 case Opt_lock_on_read:
839 rbd_opts->lock_on_read = true;
840 break;
Ilya Dryomove010dd02017-04-13 12:17:39 +0200841 case Opt_exclusive:
842 rbd_opts->exclusive = true;
843 break;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700844 default:
Ilya Dryomov210c1042015-06-22 13:24:48 +0300845 /* libceph prints "bad option" msg */
846 return -EINVAL;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700847 }
Ilya Dryomov210c1042015-06-22 13:24:48 +0300848
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700849 return 0;
850}
851
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800852static char* obj_op_name(enum obj_operation_type op_type)
853{
854 switch (op_type) {
855 case OBJ_OP_READ:
856 return "read";
857 case OBJ_OP_WRITE:
858 return "write";
Guangliang Zhao90e98c52014-04-01 22:22:16 +0800859 case OBJ_OP_DISCARD:
860 return "discard";
Guangliang Zhao6d2940c2014-03-13 11:21:35 +0800861 default:
862 return "???";
863 }
864}
865
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700866/*
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700867 * Get a ceph client with specific addr and configuration, if one does
Alex Elder7262cfc2013-05-16 15:04:20 -0500868 * not exist create it. Either way, ceph_opts is consumed by this
869 * function.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700870 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500871static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700872{
Alex Elderf8c38922012-08-10 13:12:07 -0700873 struct rbd_client *rbdc;
Yehuda Sadeh59c2be12011-03-21 15:10:11 -0700874
Alex Eldercfbf6372013-05-31 17:40:45 -0500875 mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
Alex Elder1f7ba332012-08-10 13:12:07 -0700876 rbdc = rbd_client_find(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500877 if (rbdc) /* using an existing client */
Alex Elder43ae4702012-07-03 16:01:18 -0500878 ceph_destroy_options(ceph_opts);
Alex Elder9d3997f2012-10-25 23:34:42 -0500879 else
Alex Elderf8c38922012-08-10 13:12:07 -0700880 rbdc = rbd_client_create(ceph_opts);
Alex Eldercfbf6372013-05-31 17:40:45 -0500881 mutex_unlock(&client_mutex);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700882
Alex Elder9d3997f2012-10-25 23:34:42 -0500883 return rbdc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700884}
885
886/*
887 * Destroy ceph client
Alex Elderd23a4b32012-01-29 13:57:43 -0600888 *
Alex Elder432b8582012-01-29 13:57:44 -0600889 * Caller must hold rbd_client_list_lock.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700890 */
891static void rbd_client_release(struct kref *kref)
892{
893 struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
894
Alex Elder37206ee2013-02-20 17:32:08 -0600895 dout("%s: rbdc %p\n", __func__, rbdc);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500896 spin_lock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700897 list_del(&rbdc->node);
Alex Eldercd9d9f52012-04-04 13:35:44 -0500898 spin_unlock(&rbd_client_list_lock);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700899
900 ceph_destroy_client(rbdc->client);
901 kfree(rbdc);
902}
903
904/*
905 * Drop reference to ceph client node. If it's not referenced anymore, release
906 * it.
907 */
Alex Elder9d3997f2012-10-25 23:34:42 -0500908static void rbd_put_client(struct rbd_client *rbdc)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700909{
Alex Elderc53d5892012-10-25 23:34:42 -0500910 if (rbdc)
911 kref_put(&rbdc->kref, rbd_client_release);
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700912}
913
Alex Eldera30b71b2012-07-10 20:30:11 -0500914static bool rbd_image_format_valid(u32 image_format)
915{
916 return image_format == 1 || image_format == 2;
917}
918
Alex Elder8e94af82012-07-25 09:32:40 -0500919static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
920{
Alex Elder103a1502012-08-02 11:29:45 -0500921 size_t size;
922 u32 snap_count;
923
924 /* The header has to start with the magic rbd header text */
925 if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
926 return false;
927
Alex Elderdb2388b2012-10-20 22:17:27 -0500928 /* The bio layer requires at least sector-sized I/O */
929
930 if (ondisk->options.order < SECTOR_SHIFT)
931 return false;
932
933 /* If we use u64 in a few spots we may be able to loosen this */
934
935 if (ondisk->options.order > 8 * sizeof (int) - 1)
936 return false;
937
Alex Elder103a1502012-08-02 11:29:45 -0500938 /*
939 * The size of a snapshot header has to fit in a size_t, and
940 * that limits the number of snapshots.
941 */
942 snap_count = le32_to_cpu(ondisk->snap_count);
943 size = SIZE_MAX - sizeof (struct ceph_snap_context);
944 if (snap_count > size / sizeof (__le64))
945 return false;
946
947 /*
948 * Not only that, but the size of the entire the snapshot
949 * header must also be representable in a size_t.
950 */
951 size -= snap_count * sizeof (__le64);
952 if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
953 return false;
954
955 return true;
Alex Elder8e94af82012-07-25 09:32:40 -0500956}
957
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700958/*
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100959 * returns the size of an object in the image
960 */
961static u32 rbd_obj_bytes(struct rbd_image_header *header)
962{
963 return 1U << header->obj_order;
964}
965
Ilya Dryomov263423f2017-01-25 18:16:22 +0100966static void rbd_init_layout(struct rbd_device *rbd_dev)
967{
968 if (rbd_dev->header.stripe_unit == 0 ||
969 rbd_dev->header.stripe_count == 0) {
970 rbd_dev->header.stripe_unit = rbd_obj_bytes(&rbd_dev->header);
971 rbd_dev->header.stripe_count = 1;
972 }
973
974 rbd_dev->layout.stripe_unit = rbd_dev->header.stripe_unit;
975 rbd_dev->layout.stripe_count = rbd_dev->header.stripe_count;
976 rbd_dev->layout.object_size = rbd_obj_bytes(&rbd_dev->header);
Ilya Dryomov7e973322017-01-25 18:16:22 +0100977 rbd_dev->layout.pool_id = rbd_dev->header.data_pool_id == CEPH_NOPOOL ?
978 rbd_dev->spec->pool_id : rbd_dev->header.data_pool_id;
Ilya Dryomov263423f2017-01-25 18:16:22 +0100979 RCU_INIT_POINTER(rbd_dev->layout.pool_ns, NULL);
980}
981
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +0100982/*
Alex Elderbb23e372013-05-06 09:51:29 -0500983 * Fill an rbd image header with information from the given format 1
984 * on-disk header.
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700985 */
Alex Elder662518b2013-05-06 09:51:29 -0500986static int rbd_header_from_disk(struct rbd_device *rbd_dev,
Alex Elder4156d992012-08-02 11:29:46 -0500987 struct rbd_image_header_ondisk *ondisk)
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700988{
Alex Elder662518b2013-05-06 09:51:29 -0500989 struct rbd_image_header *header = &rbd_dev->header;
Alex Elderbb23e372013-05-06 09:51:29 -0500990 bool first_time = header->object_prefix == NULL;
991 struct ceph_snap_context *snapc;
992 char *object_prefix = NULL;
993 char *snap_names = NULL;
994 u64 *snap_sizes = NULL;
Alex Elderccece232012-07-10 20:30:10 -0500995 u32 snap_count;
Alex Elderbb23e372013-05-06 09:51:29 -0500996 int ret = -ENOMEM;
Alex Elder621901d2012-08-23 23:22:06 -0500997 u32 i;
Yehuda Sadeh602adf42010-08-12 16:11:25 -0700998
Alex Elderbb23e372013-05-06 09:51:29 -0500999 /* Allocate this now to avoid having to handle failure below */
1000
1001 if (first_time) {
Ilya Dryomov848d7962017-01-25 18:16:21 +01001002 object_prefix = kstrndup(ondisk->object_prefix,
1003 sizeof(ondisk->object_prefix),
1004 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001005 if (!object_prefix)
1006 return -ENOMEM;
Alex Elderbb23e372013-05-06 09:51:29 -05001007 }
1008
1009 /* Allocate the snapshot context and fill it in */
Alex Elder6a523252012-07-19 17:12:59 -05001010
Alex Elder103a1502012-08-02 11:29:45 -05001011 snap_count = le32_to_cpu(ondisk->snap_count);
Alex Elderbb23e372013-05-06 09:51:29 -05001012 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
1013 if (!snapc)
1014 goto out_err;
1015 snapc->seq = le64_to_cpu(ondisk->snap_seq);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001016 if (snap_count) {
Alex Elderbb23e372013-05-06 09:51:29 -05001017 struct rbd_image_snap_ondisk *snaps;
Alex Elderf785cc12012-08-23 23:22:06 -05001018 u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
1019
Alex Elderbb23e372013-05-06 09:51:29 -05001020 /* We'll keep a copy of the snapshot names... */
Alex Elder621901d2012-08-23 23:22:06 -05001021
Alex Elderbb23e372013-05-06 09:51:29 -05001022 if (snap_names_len > (u64)SIZE_MAX)
1023 goto out_2big;
1024 snap_names = kmalloc(snap_names_len, GFP_KERNEL);
1025 if (!snap_names)
Alex Elder6a523252012-07-19 17:12:59 -05001026 goto out_err;
Alex Elderbb23e372013-05-06 09:51:29 -05001027
1028 /* ...as well as the array of their sizes. */
Markus Elfring88a25a52016-09-11 12:21:25 +02001029 snap_sizes = kmalloc_array(snap_count,
1030 sizeof(*header->snap_sizes),
1031 GFP_KERNEL);
Alex Elderbb23e372013-05-06 09:51:29 -05001032 if (!snap_sizes)
1033 goto out_err;
1034
Alex Elderf785cc12012-08-23 23:22:06 -05001035 /*
Alex Elderbb23e372013-05-06 09:51:29 -05001036 * Copy the names, and fill in each snapshot's id
1037 * and size.
1038 *
Alex Elder99a41eb2013-05-06 09:51:30 -05001039 * Note that rbd_dev_v1_header_info() guarantees the
Alex Elderbb23e372013-05-06 09:51:29 -05001040 * ondisk buffer we're working with has
Alex Elderf785cc12012-08-23 23:22:06 -05001041 * snap_names_len bytes beyond the end of the
1042 * snapshot id array, this memcpy() is safe.
1043 */
Alex Elderbb23e372013-05-06 09:51:29 -05001044 memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
1045 snaps = ondisk->snaps;
1046 for (i = 0; i < snap_count; i++) {
1047 snapc->snaps[i] = le64_to_cpu(snaps[i].id);
1048 snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
1049 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001050 }
Alex Elder849b4262012-07-09 21:04:24 -05001051
Alex Elderbb23e372013-05-06 09:51:29 -05001052 /* We won't fail any more, fill in the header */
Alex Elder6a523252012-07-19 17:12:59 -05001053
Alex Elderbb23e372013-05-06 09:51:29 -05001054 if (first_time) {
1055 header->object_prefix = object_prefix;
1056 header->obj_order = ondisk->options.order;
Ilya Dryomov263423f2017-01-25 18:16:22 +01001057 rbd_init_layout(rbd_dev);
Alex Elder662518b2013-05-06 09:51:29 -05001058 } else {
1059 ceph_put_snap_context(header->snapc);
1060 kfree(header->snap_names);
1061 kfree(header->snap_sizes);
Alex Elderbb23e372013-05-06 09:51:29 -05001062 }
1063
1064 /* The remaining fields always get updated (when we refresh) */
Alex Elder621901d2012-08-23 23:22:06 -05001065
Alex Elderf84344f2012-08-31 17:29:51 -05001066 header->image_size = le64_to_cpu(ondisk->image_size);
Alex Elderbb23e372013-05-06 09:51:29 -05001067 header->snapc = snapc;
1068 header->snap_names = snap_names;
1069 header->snap_sizes = snap_sizes;
Alex Elder468521c2013-04-26 09:43:47 -05001070
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001071 return 0;
Alex Elderbb23e372013-05-06 09:51:29 -05001072out_2big:
1073 ret = -EIO;
Alex Elder6a523252012-07-19 17:12:59 -05001074out_err:
Alex Elderbb23e372013-05-06 09:51:29 -05001075 kfree(snap_sizes);
1076 kfree(snap_names);
1077 ceph_put_snap_context(snapc);
1078 kfree(object_prefix);
Alex Elderccece232012-07-10 20:30:10 -05001079
Alex Elderbb23e372013-05-06 09:51:29 -05001080 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001081}
1082
Alex Elder9682fc62013-04-30 00:44:33 -05001083static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
1084{
1085 const char *snap_name;
1086
1087 rbd_assert(which < rbd_dev->header.snapc->num_snaps);
1088
1089 /* Skip over names until we find the one we are looking for */
1090
1091 snap_name = rbd_dev->header.snap_names;
1092 while (which--)
1093 snap_name += strlen(snap_name) + 1;
1094
1095 return kstrdup(snap_name, GFP_KERNEL);
1096}
1097
Alex Elder30d1cff2013-05-01 12:43:03 -05001098/*
1099 * Snapshot id comparison function for use with qsort()/bsearch().
1100 * Note that result is for snapshots in *descending* order.
1101 */
1102static int snapid_compare_reverse(const void *s1, const void *s2)
1103{
1104 u64 snap_id1 = *(u64 *)s1;
1105 u64 snap_id2 = *(u64 *)s2;
1106
1107 if (snap_id1 < snap_id2)
1108 return 1;
1109 return snap_id1 == snap_id2 ? 0 : -1;
1110}
1111
1112/*
1113 * Search a snapshot context to see if the given snapshot id is
1114 * present.
1115 *
1116 * Returns the position of the snapshot id in the array if it's found,
1117 * or BAD_SNAP_INDEX otherwise.
1118 *
1119 * Note: The snapshot array is in kept sorted (by the osd) in
1120 * reverse order, highest snapshot id first.
1121 */
Alex Elder9682fc62013-04-30 00:44:33 -05001122static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
1123{
1124 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
Alex Elder30d1cff2013-05-01 12:43:03 -05001125 u64 *found;
Alex Elder9682fc62013-04-30 00:44:33 -05001126
Alex Elder30d1cff2013-05-01 12:43:03 -05001127 found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
1128 sizeof (snap_id), snapid_compare_reverse);
Alex Elder9682fc62013-04-30 00:44:33 -05001129
Alex Elder30d1cff2013-05-01 12:43:03 -05001130 return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
Alex Elder9682fc62013-04-30 00:44:33 -05001131}
1132
Alex Elder2ad3d712013-04-30 00:44:33 -05001133static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
1134 u64 snap_id)
Alex Elder54cac612013-04-30 00:44:33 -05001135{
1136 u32 which;
Josh Durginda6a6b62013-09-04 17:57:31 -07001137 const char *snap_name;
Alex Elder54cac612013-04-30 00:44:33 -05001138
1139 which = rbd_dev_snap_index(rbd_dev, snap_id);
1140 if (which == BAD_SNAP_INDEX)
Josh Durginda6a6b62013-09-04 17:57:31 -07001141 return ERR_PTR(-ENOENT);
Alex Elder54cac612013-04-30 00:44:33 -05001142
Josh Durginda6a6b62013-09-04 17:57:31 -07001143 snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
1144 return snap_name ? snap_name : ERR_PTR(-ENOMEM);
Alex Elder54cac612013-04-30 00:44:33 -05001145}
1146
Alex Elder9e15b772012-10-30 19:40:33 -05001147static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
1148{
Alex Elder9e15b772012-10-30 19:40:33 -05001149 if (snap_id == CEPH_NOSNAP)
1150 return RBD_SNAP_HEAD_NAME;
1151
Alex Elder54cac612013-04-30 00:44:33 -05001152 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1153 if (rbd_dev->image_format == 1)
1154 return rbd_dev_v1_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001155
Alex Elder54cac612013-04-30 00:44:33 -05001156 return rbd_dev_v2_snap_name(rbd_dev, snap_id);
Alex Elder9e15b772012-10-30 19:40:33 -05001157}
1158
Alex Elder2ad3d712013-04-30 00:44:33 -05001159static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
1160 u64 *snap_size)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001161{
Alex Elder2ad3d712013-04-30 00:44:33 -05001162 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1163 if (snap_id == CEPH_NOSNAP) {
1164 *snap_size = rbd_dev->header.image_size;
1165 } else if (rbd_dev->image_format == 1) {
1166 u32 which;
Alex Elder00f1f362012-02-07 12:03:36 -06001167
Alex Elder2ad3d712013-04-30 00:44:33 -05001168 which = rbd_dev_snap_index(rbd_dev, snap_id);
1169 if (which == BAD_SNAP_INDEX)
1170 return -ENOENT;
Alex Elder00f1f362012-02-07 12:03:36 -06001171
Alex Elder2ad3d712013-04-30 00:44:33 -05001172 *snap_size = rbd_dev->header.snap_sizes[which];
1173 } else {
1174 u64 size = 0;
1175 int ret;
1176
1177 ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
1178 if (ret)
1179 return ret;
1180
1181 *snap_size = size;
1182 }
1183 return 0;
1184}
1185
1186static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
1187 u64 *snap_features)
1188{
1189 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
1190 if (snap_id == CEPH_NOSNAP) {
1191 *snap_features = rbd_dev->header.features;
1192 } else if (rbd_dev->image_format == 1) {
1193 *snap_features = 0; /* No features for format 1 */
1194 } else {
1195 u64 features = 0;
1196 int ret;
1197
1198 ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
1199 if (ret)
1200 return ret;
1201
1202 *snap_features = features;
1203 }
1204 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001205}
1206
Alex Elderd1cf5782013-04-27 09:59:30 -05001207static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001208{
Alex Elder8f4b7d92013-05-06 07:40:30 -05001209 u64 snap_id = rbd_dev->spec->snap_id;
Alex Elder2ad3d712013-04-30 00:44:33 -05001210 u64 size = 0;
1211 u64 features = 0;
1212 int ret;
Alex Elder8b0241f2013-04-25 23:15:08 -05001213
Alex Elder2ad3d712013-04-30 00:44:33 -05001214 ret = rbd_snap_size(rbd_dev, snap_id, &size);
1215 if (ret)
1216 return ret;
1217 ret = rbd_snap_features(rbd_dev, snap_id, &features);
1218 if (ret)
1219 return ret;
1220
1221 rbd_dev->mapping.size = size;
1222 rbd_dev->mapping.features = features;
1223
Alex Elder8b0241f2013-04-25 23:15:08 -05001224 return 0;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001225}
1226
Alex Elderd1cf5782013-04-27 09:59:30 -05001227static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
1228{
1229 rbd_dev->mapping.size = 0;
1230 rbd_dev->mapping.features = 0;
Alex Elder200a6a82013-04-28 23:32:34 -05001231}
1232
Alex Elder65ccfe22012-08-09 10:33:26 -07001233static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
1234{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001235 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001236
Alex Elder65ccfe22012-08-09 10:33:26 -07001237 return offset & (segment_size - 1);
1238}
1239
1240static u64 rbd_segment_length(struct rbd_device *rbd_dev,
1241 u64 offset, u64 length)
1242{
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01001243 u64 segment_size = rbd_obj_bytes(&rbd_dev->header);
Alex Elder65ccfe22012-08-09 10:33:26 -07001244
1245 offset &= segment_size - 1;
1246
Alex Elderaafb2302012-09-06 16:00:54 -05001247 rbd_assert(length <= U64_MAX - offset);
Alex Elder65ccfe22012-08-09 10:33:26 -07001248 if (offset + length > segment_size)
1249 length = segment_size - offset;
1250
1251 return length;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001252}
1253
Ilya Dryomov5359a172018-01-20 10:30:10 +01001254static void zero_bvec(struct bio_vec *bv)
1255{
1256 void *buf;
1257 unsigned long flags;
1258
1259 buf = bvec_kmap_irq(bv, &flags);
1260 memset(buf, 0, bv->bv_len);
1261 flush_dcache_page(bv->bv_page);
1262 bvec_kunmap_irq(buf, &flags);
1263}
1264
1265static void zero_bios(struct ceph_bio_iter *bio_pos, u32 off, u32 bytes)
1266{
1267 struct ceph_bio_iter it = *bio_pos;
1268
1269 ceph_bio_iter_advance(&it, off);
1270 ceph_bio_iter_advance_step(&it, bytes, ({
1271 zero_bvec(&bv);
1272 }));
1273}
1274
Yehuda Sadeh602adf42010-08-12 16:11:25 -07001275/*
Alex Elderb9434c52013-04-19 15:34:50 -05001276 * similar to zero_bio_chain(), zeros data defined by a page array,
1277 * starting at the given byte offset from the start of the array and
1278 * continuing up to the given end offset. The pages array is
1279 * assumed to be big enough to hold all bytes up to the end.
1280 */
1281static void zero_pages(struct page **pages, u64 offset, u64 end)
1282{
1283 struct page **page = &pages[offset >> PAGE_SHIFT];
1284
1285 rbd_assert(end > offset);
1286 rbd_assert(end - offset <= (u64)SIZE_MAX);
1287 while (offset < end) {
1288 size_t page_offset;
1289 size_t length;
1290 unsigned long flags;
1291 void *kaddr;
1292
Geert Uytterhoeven491205a2013-05-13 20:35:37 -05001293 page_offset = offset & ~PAGE_MASK;
1294 length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
Alex Elderb9434c52013-04-19 15:34:50 -05001295 local_irq_save(flags);
1296 kaddr = kmap_atomic(*page);
1297 memset(kaddr + page_offset, 0, length);
Alex Eldere2156052013-05-22 20:54:25 -05001298 flush_dcache_page(*page);
Alex Elderb9434c52013-04-19 15:34:50 -05001299 kunmap_atomic(kaddr);
1300 local_irq_restore(flags);
1301
1302 offset += length;
1303 page++;
1304 }
1305}
1306
1307/*
Alex Elder926f9b32013-02-11 12:33:24 -06001308 * The default/initial value for all object request flags is 0. For
1309 * each flag, once its value is set to 1 it is never reset to 0
1310 * again.
1311 */
Alex Elder6365d332013-02-11 12:33:24 -06001312static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
1313{
1314 if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
Alex Elder6365d332013-02-11 12:33:24 -06001315 struct rbd_device *rbd_dev;
1316
Alex Elder57acbaa2013-02-11 12:33:24 -06001317 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001318 rbd_warn(rbd_dev, "obj_request %p already marked img_data",
Alex Elder6365d332013-02-11 12:33:24 -06001319 obj_request);
1320 }
1321}
1322
1323static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
1324{
1325 smp_mb();
1326 return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
1327}
1328
Alex Elder57acbaa2013-02-11 12:33:24 -06001329static void obj_request_done_set(struct rbd_obj_request *obj_request)
1330{
1331 if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
1332 struct rbd_device *rbd_dev = NULL;
1333
1334 if (obj_request_img_data_test(obj_request))
1335 rbd_dev = obj_request->img_request->rbd_dev;
Ilya Dryomov9584d502014-07-11 12:11:20 +04001336 rbd_warn(rbd_dev, "obj_request %p already marked done",
Alex Elder57acbaa2013-02-11 12:33:24 -06001337 obj_request);
1338 }
1339}
1340
1341static bool obj_request_done_test(struct rbd_obj_request *obj_request)
1342{
1343 smp_mb();
1344 return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
1345}
1346
Alex Elder5679c592013-02-11 12:33:24 -06001347/*
1348 * This sets the KNOWN flag after (possibly) setting the EXISTS
1349 * flag. The latter is set based on the "exists" value provided.
1350 *
1351 * Note that for our purposes once an object exists it never goes
1352 * away again. It's possible that the response from two existence
1353 * checks are separated by the creation of the target object, and
1354 * the first ("doesn't exist") response arrives *after* the second
1355 * ("does exist"). In that case we ignore the second one.
1356 */
1357static void obj_request_existence_set(struct rbd_obj_request *obj_request,
1358 bool exists)
1359{
1360 if (exists)
1361 set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
1362 set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
1363 smp_mb();
1364}
1365
1366static bool obj_request_known_test(struct rbd_obj_request *obj_request)
1367{
1368 smp_mb();
1369 return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
1370}
1371
1372static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
1373{
1374 smp_mb();
1375 return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
1376}
1377
Ilya Dryomov96385562014-06-10 13:53:29 +04001378static bool obj_request_overlaps_parent(struct rbd_obj_request *obj_request)
1379{
1380 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
1381
1382 return obj_request->img_offset <
1383 round_up(rbd_dev->parent_overlap, rbd_obj_bytes(&rbd_dev->header));
1384}
1385
Alex Elderbf0d5f502012-11-22 00:00:08 -06001386static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
1387{
Alex Elder37206ee2013-02-20 17:32:08 -06001388 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001389 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001390 kref_get(&obj_request->kref);
1391}
1392
1393static void rbd_obj_request_destroy(struct kref *kref);
1394static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
1395{
1396 rbd_assert(obj_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001397 dout("%s: obj %p (was %d)\n", __func__, obj_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001398 kref_read(&obj_request->kref));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001399 kref_put(&obj_request->kref, rbd_obj_request_destroy);
1400}
1401
Alex Elder0f2d5be2014-04-26 14:21:44 +04001402static void rbd_img_request_get(struct rbd_img_request *img_request)
1403{
1404 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001405 kref_read(&img_request->kref));
Alex Elder0f2d5be2014-04-26 14:21:44 +04001406 kref_get(&img_request->kref);
1407}
1408
Alex Eldere93f3152013-05-08 22:50:04 -05001409static bool img_request_child_test(struct rbd_img_request *img_request);
1410static void rbd_parent_request_destroy(struct kref *kref);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001411static void rbd_img_request_destroy(struct kref *kref);
1412static void rbd_img_request_put(struct rbd_img_request *img_request)
1413{
1414 rbd_assert(img_request != NULL);
Alex Elder37206ee2013-02-20 17:32:08 -06001415 dout("%s: img %p (was %d)\n", __func__, img_request,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01001416 kref_read(&img_request->kref));
Alex Eldere93f3152013-05-08 22:50:04 -05001417 if (img_request_child_test(img_request))
1418 kref_put(&img_request->kref, rbd_parent_request_destroy);
1419 else
1420 kref_put(&img_request->kref, rbd_img_request_destroy);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001421}
1422
1423static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
1424 struct rbd_obj_request *obj_request)
1425{
Alex Elder25dcf952013-01-25 17:08:55 -06001426 rbd_assert(obj_request->img_request == NULL);
1427
Alex Elderb155e862013-04-15 14:50:37 -05001428 /* Image request now owns object's original reference */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001429 obj_request->img_request = img_request;
Alex Elder25dcf952013-01-25 17:08:55 -06001430 obj_request->which = img_request->obj_request_count;
Alex Elder6365d332013-02-11 12:33:24 -06001431 rbd_assert(!obj_request_img_data_test(obj_request));
1432 obj_request_img_data_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001433 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001434 img_request->obj_request_count++;
1435 list_add_tail(&obj_request->links, &img_request->obj_requests);
Alex Elder37206ee2013-02-20 17:32:08 -06001436 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1437 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001438}
1439
1440static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
1441 struct rbd_obj_request *obj_request)
1442{
1443 rbd_assert(obj_request->which != BAD_WHICH);
Alex Elder25dcf952013-01-25 17:08:55 -06001444
Alex Elder37206ee2013-02-20 17:32:08 -06001445 dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
1446 obj_request->which);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001447 list_del(&obj_request->links);
Alex Elder25dcf952013-01-25 17:08:55 -06001448 rbd_assert(img_request->obj_request_count > 0);
1449 img_request->obj_request_count--;
1450 rbd_assert(obj_request->which == img_request->obj_request_count);
1451 obj_request->which = BAD_WHICH;
Alex Elder6365d332013-02-11 12:33:24 -06001452 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elderbf0d5f502012-11-22 00:00:08 -06001453 rbd_assert(obj_request->img_request == img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001454 obj_request->img_request = NULL;
Alex Elder25dcf952013-01-25 17:08:55 -06001455 obj_request->callback = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001456 rbd_obj_request_put(obj_request);
1457}
1458
1459static bool obj_request_type_valid(enum obj_request_type type)
1460{
1461 switch (type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001462 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001463 case OBJ_REQUEST_BIO:
Alex Elder788e2df2013-01-17 12:25:27 -06001464 case OBJ_REQUEST_PAGES:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001465 return true;
1466 default:
1467 return false;
1468 }
1469}
1470
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001471static void rbd_img_obj_callback(struct rbd_obj_request *obj_request);
1472
Ilya Dryomov980917f2016-09-12 18:59:42 +02001473static void rbd_obj_request_submit(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001474{
Ilya Dryomov980917f2016-09-12 18:59:42 +02001475 struct ceph_osd_request *osd_req = obj_request->osd_req;
1476
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001477 dout("%s %p object_no %016llx %llu~%llu osd_req %p\n", __func__,
1478 obj_request, obj_request->object_no, obj_request->offset,
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001479 obj_request->length, osd_req);
Ilya Dryomov4a17dad2016-09-13 21:08:10 +02001480 if (obj_request_img_data_test(obj_request)) {
1481 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1482 rbd_img_request_get(obj_request->img_request);
1483 }
Ilya Dryomov980917f2016-09-12 18:59:42 +02001484 ceph_osdc_start_request(osd_req->r_osdc, osd_req, false);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001485}
1486
1487static void rbd_img_request_complete(struct rbd_img_request *img_request)
1488{
Alex Elder55f27e02013-04-10 12:34:25 -05001489
Alex Elder37206ee2013-02-20 17:32:08 -06001490 dout("%s: img %p\n", __func__, img_request);
Alex Elder55f27e02013-04-10 12:34:25 -05001491
1492 /*
1493 * If no error occurred, compute the aggregate transfer
1494 * count for the image request. We could instead use
1495 * atomic64_cmpxchg() to update it as each object request
1496 * completes; not clear which way is better off hand.
1497 */
1498 if (!img_request->result) {
1499 struct rbd_obj_request *obj_request;
1500 u64 xferred = 0;
1501
1502 for_each_obj_request(img_request, obj_request)
1503 xferred += obj_request->xferred;
1504 img_request->xferred = xferred;
1505 }
1506
Alex Elderbf0d5f502012-11-22 00:00:08 -06001507 if (img_request->callback)
1508 img_request->callback(img_request);
1509 else
1510 rbd_img_request_put(img_request);
1511}
1512
Alex Elder0c425242013-02-08 09:55:49 -06001513/*
1514 * The default/initial value for all image request flags is 0. Each
1515 * is conditionally set to 1 at image request initialization time
1516 * and currently never change thereafter.
1517 */
1518static void img_request_write_set(struct rbd_img_request *img_request)
1519{
1520 set_bit(IMG_REQ_WRITE, &img_request->flags);
1521 smp_mb();
1522}
1523
1524static bool img_request_write_test(struct rbd_img_request *img_request)
1525{
1526 smp_mb();
1527 return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
1528}
1529
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001530/*
1531 * Set the discard flag when the img_request is an discard request
1532 */
1533static void img_request_discard_set(struct rbd_img_request *img_request)
1534{
1535 set_bit(IMG_REQ_DISCARD, &img_request->flags);
1536 smp_mb();
1537}
1538
1539static bool img_request_discard_test(struct rbd_img_request *img_request)
1540{
1541 smp_mb();
1542 return test_bit(IMG_REQ_DISCARD, &img_request->flags) != 0;
1543}
1544
Alex Elder9849e982013-01-24 16:13:36 -06001545static void img_request_child_set(struct rbd_img_request *img_request)
1546{
1547 set_bit(IMG_REQ_CHILD, &img_request->flags);
1548 smp_mb();
1549}
1550
Alex Eldere93f3152013-05-08 22:50:04 -05001551static void img_request_child_clear(struct rbd_img_request *img_request)
1552{
1553 clear_bit(IMG_REQ_CHILD, &img_request->flags);
1554 smp_mb();
1555}
1556
Alex Elder9849e982013-01-24 16:13:36 -06001557static bool img_request_child_test(struct rbd_img_request *img_request)
1558{
1559 smp_mb();
1560 return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
1561}
1562
Alex Elderd0b2e942013-01-24 16:13:36 -06001563static void img_request_layered_set(struct rbd_img_request *img_request)
1564{
1565 set_bit(IMG_REQ_LAYERED, &img_request->flags);
1566 smp_mb();
1567}
1568
Alex Eldera2acd002013-05-08 22:50:04 -05001569static void img_request_layered_clear(struct rbd_img_request *img_request)
1570{
1571 clear_bit(IMG_REQ_LAYERED, &img_request->flags);
1572 smp_mb();
1573}
1574
Alex Elderd0b2e942013-01-24 16:13:36 -06001575static bool img_request_layered_test(struct rbd_img_request *img_request)
1576{
1577 smp_mb();
1578 return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
1579}
1580
Josh Durgin3b434a2a2014-04-04 17:32:15 -07001581static enum obj_operation_type
1582rbd_img_request_op_type(struct rbd_img_request *img_request)
1583{
1584 if (img_request_write_test(img_request))
1585 return OBJ_OP_WRITE;
1586 else if (img_request_discard_test(img_request))
1587 return OBJ_OP_DISCARD;
1588 else
1589 return OBJ_OP_READ;
1590}
1591
Alex Elder6e2a4502013-03-27 09:16:30 -05001592static void
1593rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
1594{
Alex Elderb9434c52013-04-19 15:34:50 -05001595 u64 xferred = obj_request->xferred;
1596 u64 length = obj_request->length;
1597
Alex Elder6e2a4502013-03-27 09:16:30 -05001598 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1599 obj_request, obj_request->img_request, obj_request->result,
Alex Elderb9434c52013-04-19 15:34:50 -05001600 xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001601 /*
Josh Durgin17c1cc12013-08-26 17:55:38 -07001602 * ENOENT means a hole in the image. We zero-fill the entire
1603 * length of the request. A short read also implies zero-fill
1604 * to the end of the request. An error requires the whole
1605 * length of the request to be reported finished with an error
1606 * to the block layer. In each case we update the xferred
1607 * count to indicate the whole request was satisfied.
Alex Elder6e2a4502013-03-27 09:16:30 -05001608 */
Alex Elderb9434c52013-04-19 15:34:50 -05001609 rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
Alex Elder6e2a4502013-03-27 09:16:30 -05001610 if (obj_request->result == -ENOENT) {
Alex Elderb9434c52013-04-19 15:34:50 -05001611 if (obj_request->type == OBJ_REQUEST_BIO)
Ilya Dryomov5359a172018-01-20 10:30:10 +01001612 zero_bios(&obj_request->bio_pos, 0, length);
Alex Elderb9434c52013-04-19 15:34:50 -05001613 else
1614 zero_pages(obj_request->pages, 0, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001615 obj_request->result = 0;
Alex Elderb9434c52013-04-19 15:34:50 -05001616 } else if (xferred < length && !obj_request->result) {
1617 if (obj_request->type == OBJ_REQUEST_BIO)
Ilya Dryomov5359a172018-01-20 10:30:10 +01001618 zero_bios(&obj_request->bio_pos, xferred,
1619 length - xferred);
Alex Elderb9434c52013-04-19 15:34:50 -05001620 else
1621 zero_pages(obj_request->pages, xferred, length);
Alex Elder6e2a4502013-03-27 09:16:30 -05001622 }
Josh Durgin17c1cc12013-08-26 17:55:38 -07001623 obj_request->xferred = length;
Alex Elder6e2a4502013-03-27 09:16:30 -05001624 obj_request_done_set(obj_request);
1625}
1626
Alex Elderbf0d5f502012-11-22 00:00:08 -06001627static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
1628{
Alex Elder37206ee2013-02-20 17:32:08 -06001629 dout("%s: obj %p cb %p\n", __func__, obj_request,
1630 obj_request->callback);
Ilya Dryomov2e584bc2018-01-15 17:24:51 +01001631 obj_request->callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001632}
1633
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02001634static void rbd_obj_request_error(struct rbd_obj_request *obj_request, int err)
1635{
1636 obj_request->result = err;
1637 obj_request->xferred = 0;
1638 /*
1639 * kludge - mirror rbd_obj_request_submit() to match a put in
1640 * rbd_img_obj_callback()
1641 */
1642 if (obj_request_img_data_test(obj_request)) {
1643 WARN_ON(obj_request->callback != rbd_img_obj_callback);
1644 rbd_img_request_get(obj_request->img_request);
1645 }
1646 obj_request_done_set(obj_request);
1647 rbd_obj_request_complete(obj_request);
1648}
1649
Alex Elderc47f9372013-02-26 14:23:07 -06001650static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001651{
Alex Elder57acbaa2013-02-11 12:33:24 -06001652 struct rbd_img_request *img_request = NULL;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001653 struct rbd_device *rbd_dev = NULL;
Alex Elder57acbaa2013-02-11 12:33:24 -06001654 bool layered = false;
1655
1656 if (obj_request_img_data_test(obj_request)) {
1657 img_request = obj_request->img_request;
1658 layered = img_request && img_request_layered_test(img_request);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001659 rbd_dev = img_request->rbd_dev;
Alex Elder57acbaa2013-02-11 12:33:24 -06001660 }
Alex Elder8b3e1a52013-01-24 16:13:36 -06001661
1662 dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
1663 obj_request, img_request, obj_request->result,
1664 obj_request->xferred, obj_request->length);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05001665 if (layered && obj_request->result == -ENOENT &&
1666 obj_request->img_offset < rbd_dev->parent_overlap)
Alex Elder8b3e1a52013-01-24 16:13:36 -06001667 rbd_img_parent_read(obj_request);
1668 else if (img_request)
Alex Elder6e2a4502013-03-27 09:16:30 -05001669 rbd_img_obj_request_read_callback(obj_request);
1670 else
1671 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001672}
1673
Alex Elderc47f9372013-02-26 14:23:07 -06001674static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001675{
Sage Weil1b83bef2013-02-25 16:11:12 -08001676 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1677 obj_request->result, obj_request->length);
1678 /*
Alex Elder8b3e1a52013-01-24 16:13:36 -06001679 * There is no such thing as a successful short write. Set
1680 * it to our originally-requested length.
Sage Weil1b83bef2013-02-25 16:11:12 -08001681 */
1682 obj_request->xferred = obj_request->length;
Alex Elder07741302013-02-05 23:41:50 -06001683 obj_request_done_set(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001684}
1685
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001686static void rbd_osd_discard_callback(struct rbd_obj_request *obj_request)
1687{
1688 dout("%s: obj %p result %d %llu\n", __func__, obj_request,
1689 obj_request->result, obj_request->length);
1690 /*
1691 * There is no such thing as a successful short discard. Set
1692 * it to our originally-requested length.
1693 */
1694 obj_request->xferred = obj_request->length;
Josh Durgind0265de2014-04-07 16:54:10 -07001695 /* discarding a non-existent object is not a problem */
1696 if (obj_request->result == -ENOENT)
1697 obj_request->result = 0;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001698 obj_request_done_set(obj_request);
1699}
1700
Alex Elderfbfab532013-02-08 09:55:48 -06001701/*
1702 * For a simple stat call there's nothing to do. We'll do more if
1703 * this is part of a write sequence for a layered image.
1704 */
Alex Elderc47f9372013-02-26 14:23:07 -06001705static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
Alex Elderfbfab532013-02-08 09:55:48 -06001706{
Alex Elder37206ee2013-02-20 17:32:08 -06001707 dout("%s: obj %p\n", __func__, obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001708 obj_request_done_set(obj_request);
1709}
1710
Ilya Dryomov27617132015-07-16 17:36:11 +03001711static void rbd_osd_call_callback(struct rbd_obj_request *obj_request)
1712{
1713 dout("%s: obj %p\n", __func__, obj_request);
1714
1715 if (obj_request_img_data_test(obj_request))
1716 rbd_osd_copyup_callback(obj_request);
1717 else
1718 obj_request_done_set(obj_request);
1719}
1720
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001721static void rbd_osd_req_callback(struct ceph_osd_request *osd_req)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001722{
1723 struct rbd_obj_request *obj_request = osd_req->r_priv;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001724 u16 opcode;
1725
Ilya Dryomov85e084f2016-04-28 16:07:24 +02001726 dout("%s: osd_req %p\n", __func__, osd_req);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001727 rbd_assert(osd_req == obj_request->osd_req);
Alex Elder57acbaa2013-02-11 12:33:24 -06001728 if (obj_request_img_data_test(obj_request)) {
1729 rbd_assert(obj_request->img_request);
1730 rbd_assert(obj_request->which != BAD_WHICH);
1731 } else {
1732 rbd_assert(obj_request->which == BAD_WHICH);
1733 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06001734
Sage Weil1b83bef2013-02-25 16:11:12 -08001735 if (osd_req->r_result < 0)
1736 obj_request->result = osd_req->r_result;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001737
Alex Elderc47f9372013-02-26 14:23:07 -06001738 /*
1739 * We support a 64-bit length, but ultimately it has to be
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01001740 * passed to the block layer, which just supports a 32-bit
1741 * length field.
Alex Elderc47f9372013-02-26 14:23:07 -06001742 */
Yan, Zheng7665d852016-01-07 16:48:57 +08001743 obj_request->xferred = osd_req->r_ops[0].outdata_len;
Alex Elder8b3e1a52013-01-24 16:13:36 -06001744 rbd_assert(obj_request->xferred < (u64)UINT_MAX);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001745
Alex Elder79528732013-04-03 21:32:51 -05001746 opcode = osd_req->r_ops[0].op;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001747 switch (opcode) {
1748 case CEPH_OSD_OP_READ:
Alex Elderc47f9372013-02-26 14:23:07 -06001749 rbd_osd_read_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001750 break;
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001751 case CEPH_OSD_OP_SETALLOCHINT:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001752 rbd_assert(osd_req->r_ops[1].op == CEPH_OSD_OP_WRITE ||
1753 osd_req->r_ops[1].op == CEPH_OSD_OP_WRITEFULL);
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001754 /* fall through */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001755 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +02001756 case CEPH_OSD_OP_WRITEFULL:
Alex Elderc47f9372013-02-26 14:23:07 -06001757 rbd_osd_write_callback(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001758 break;
Alex Elderfbfab532013-02-08 09:55:48 -06001759 case CEPH_OSD_OP_STAT:
Alex Elderc47f9372013-02-26 14:23:07 -06001760 rbd_osd_stat_callback(obj_request);
Alex Elderfbfab532013-02-08 09:55:48 -06001761 break;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001762 case CEPH_OSD_OP_DELETE:
1763 case CEPH_OSD_OP_TRUNCATE:
1764 case CEPH_OSD_OP_ZERO:
1765 rbd_osd_discard_callback(obj_request);
1766 break;
Alex Elder36be9a72013-01-19 00:30:28 -06001767 case CEPH_OSD_OP_CALL:
Ilya Dryomov27617132015-07-16 17:36:11 +03001768 rbd_osd_call_callback(obj_request);
1769 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001770 default:
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001771 rbd_warn(NULL, "unexpected OSD op: object_no %016llx opcode %d",
1772 obj_request->object_no, opcode);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001773 break;
1774 }
1775
Alex Elder07741302013-02-05 23:41:50 -06001776 if (obj_request_done_test(obj_request))
Alex Elderbf0d5f502012-11-22 00:00:08 -06001777 rbd_obj_request_complete(obj_request);
1778}
1779
Alex Elder9d4df012013-04-19 15:34:50 -05001780static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
Alex Elder430c28c2013-04-03 21:32:51 -05001781{
Alex Elder8c042b02013-04-03 01:28:58 -05001782 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder430c28c2013-04-03 21:32:51 -05001783
Ilya Dryomov7c848832016-09-15 17:56:39 +02001784 rbd_assert(obj_request_img_data_test(obj_request));
1785 osd_req->r_snapid = obj_request->img_request->snap_id;
Alex Elder9d4df012013-04-19 15:34:50 -05001786}
1787
1788static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
1789{
Alex Elder9d4df012013-04-19 15:34:50 -05001790 struct ceph_osd_request *osd_req = obj_request->osd_req;
Alex Elder9d4df012013-04-19 15:34:50 -05001791
Deepa Dinamani1134e092017-05-08 15:59:19 -07001792 ktime_get_real_ts(&osd_req->r_mtime);
Ilya Dryomovbb873b5392016-05-26 00:29:52 +02001793 osd_req->r_data_offset = obj_request->offset;
Alex Elder430c28c2013-04-03 21:32:51 -05001794}
1795
Ilya Dryomovbc812072017-01-25 18:16:23 +01001796static struct ceph_osd_request *
1797__rbd_osd_req_create(struct rbd_device *rbd_dev,
1798 struct ceph_snap_context *snapc,
1799 int num_ops, unsigned int flags,
1800 struct rbd_obj_request *obj_request)
1801{
1802 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
1803 struct ceph_osd_request *req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001804 const char *name_format = rbd_dev->image_format == 1 ?
1805 RBD_V1_DATA_FORMAT : RBD_V2_DATA_FORMAT;
Ilya Dryomovbc812072017-01-25 18:16:23 +01001806
1807 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, false, GFP_NOIO);
1808 if (!req)
1809 return NULL;
1810
1811 req->r_flags = flags;
1812 req->r_callback = rbd_osd_req_callback;
1813 req->r_priv = obj_request;
1814
1815 req->r_base_oloc.pool = rbd_dev->layout.pool_id;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01001816 if (ceph_oid_aprintf(&req->r_base_oid, GFP_NOIO, name_format,
1817 rbd_dev->header.object_prefix, obj_request->object_no))
Ilya Dryomovbc812072017-01-25 18:16:23 +01001818 goto err_req;
1819
1820 if (ceph_osdc_alloc_messages(req, GFP_NOIO))
1821 goto err_req;
1822
1823 return req;
1824
1825err_req:
1826 ceph_osdc_put_request(req);
1827 return NULL;
1828}
1829
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02001830/*
1831 * Create an osd request. A read request has one osd op (read).
1832 * A write request has either one (watch) or two (hint+write) osd ops.
1833 * (All rbd data writes are prefixed with an allocation hint op, but
1834 * technically osd watch is a write request, hence this distinction.)
1835 */
Alex Elderbf0d5f502012-11-22 00:00:08 -06001836static struct ceph_osd_request *rbd_osd_req_create(
1837 struct rbd_device *rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001838 enum obj_operation_type op_type,
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001839 unsigned int num_ops,
Alex Elder430c28c2013-04-03 21:32:51 -05001840 struct rbd_obj_request *obj_request)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001841{
Alex Elderbf0d5f502012-11-22 00:00:08 -06001842 struct ceph_snap_context *snapc = NULL;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001843
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001844 if (obj_request_img_data_test(obj_request) &&
1845 (op_type == OBJ_OP_DISCARD || op_type == OBJ_OP_WRITE)) {
Alex Elder6365d332013-02-11 12:33:24 -06001846 struct rbd_img_request *img_request = obj_request->img_request;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08001847 if (op_type == OBJ_OP_WRITE) {
1848 rbd_assert(img_request_write_test(img_request));
1849 } else {
1850 rbd_assert(img_request_discard_test(img_request));
1851 }
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001852 snapc = img_request->snapc;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001853 }
1854
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08001855 rbd_assert(num_ops == 1 || ((op_type == OBJ_OP_WRITE) && num_ops == 2));
Ilya Dryomovdeb236b2014-02-25 16:22:27 +02001856
Ilya Dryomovbc812072017-01-25 18:16:23 +01001857 return __rbd_osd_req_create(rbd_dev, snapc, num_ops,
1858 (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD) ?
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001859 CEPH_OSD_FLAG_WRITE : CEPH_OSD_FLAG_READ, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001860}
1861
Alex Elder0eefd472013-04-19 15:34:50 -05001862/*
Josh Durgind3246fb2014-04-07 16:49:21 -07001863 * Create a copyup osd request based on the information in the object
1864 * request supplied. A copyup request has two or three osd ops, a
1865 * copyup method call, potentially a hint op, and a write or truncate
1866 * or zero op.
Alex Elder0eefd472013-04-19 15:34:50 -05001867 */
1868static struct ceph_osd_request *
1869rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
1870{
1871 struct rbd_img_request *img_request;
Josh Durgind3246fb2014-04-07 16:49:21 -07001872 int num_osd_ops = 3;
Alex Elder0eefd472013-04-19 15:34:50 -05001873
1874 rbd_assert(obj_request_img_data_test(obj_request));
1875 img_request = obj_request->img_request;
1876 rbd_assert(img_request);
Josh Durgind3246fb2014-04-07 16:49:21 -07001877 rbd_assert(img_request_write_test(img_request) ||
1878 img_request_discard_test(img_request));
Alex Elder0eefd472013-04-19 15:34:50 -05001879
Josh Durgind3246fb2014-04-07 16:49:21 -07001880 if (img_request_discard_test(img_request))
1881 num_osd_ops = 2;
1882
Ilya Dryomovbc812072017-01-25 18:16:23 +01001883 return __rbd_osd_req_create(img_request->rbd_dev,
1884 img_request->snapc, num_osd_ops,
Ilya Dryomov54ea0042017-02-11 18:48:41 +01001885 CEPH_OSD_FLAG_WRITE, obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05001886}
1887
Alex Elderbf0d5f502012-11-22 00:00:08 -06001888static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
1889{
1890 ceph_osdc_put_request(osd_req);
1891}
1892
Ilya Dryomov6c696d82017-01-25 18:16:23 +01001893static struct rbd_obj_request *
1894rbd_obj_request_create(enum obj_request_type type)
Alex Elderbf0d5f502012-11-22 00:00:08 -06001895{
1896 struct rbd_obj_request *obj_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001897
1898 rbd_assert(obj_request_type_valid(type));
1899
Ilya Dryomov5a60e872015-06-24 17:24:33 +03001900 obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_NOIO);
Ilya Dryomov6c696d82017-01-25 18:16:23 +01001901 if (!obj_request)
Alex Elderf907ad52013-05-01 12:43:03 -05001902 return NULL;
Alex Elderf907ad52013-05-01 12:43:03 -05001903
Alex Elderbf0d5f502012-11-22 00:00:08 -06001904 obj_request->which = BAD_WHICH;
1905 obj_request->type = type;
1906 INIT_LIST_HEAD(&obj_request->links);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001907 kref_init(&obj_request->kref);
1908
Ilya Dryomov67e2b652017-01-25 18:16:22 +01001909 dout("%s %p\n", __func__, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001910 return obj_request;
1911}
1912
1913static void rbd_obj_request_destroy(struct kref *kref)
1914{
1915 struct rbd_obj_request *obj_request;
1916
1917 obj_request = container_of(kref, struct rbd_obj_request, kref);
1918
Alex Elder37206ee2013-02-20 17:32:08 -06001919 dout("%s: obj %p\n", __func__, obj_request);
1920
Alex Elderbf0d5f502012-11-22 00:00:08 -06001921 rbd_assert(obj_request->img_request == NULL);
1922 rbd_assert(obj_request->which == BAD_WHICH);
1923
1924 if (obj_request->osd_req)
1925 rbd_osd_req_destroy(obj_request->osd_req);
1926
1927 rbd_assert(obj_request_type_valid(obj_request->type));
1928 switch (obj_request->type) {
Alex Elder9969ebc2013-01-18 12:31:10 -06001929 case OBJ_REQUEST_NODATA:
Alex Elderbf0d5f502012-11-22 00:00:08 -06001930 case OBJ_REQUEST_BIO:
Ilya Dryomov5359a172018-01-20 10:30:10 +01001931 break; /* Nothing to do */
Alex Elder788e2df2013-01-17 12:25:27 -06001932 case OBJ_REQUEST_PAGES:
Ilya Dryomov04dc9232016-09-15 18:05:16 +02001933 /* img_data requests don't own their page array */
1934 if (obj_request->pages &&
1935 !obj_request_img_data_test(obj_request))
Alex Elder788e2df2013-01-17 12:25:27 -06001936 ceph_release_page_vector(obj_request->pages,
1937 obj_request->page_count);
1938 break;
Alex Elderbf0d5f502012-11-22 00:00:08 -06001939 }
1940
Ilya Dryomovf9dcbc42018-01-20 10:30:11 +01001941 ceph_release_page_vector(obj_request->copyup_pages,
1942 obj_request->copyup_page_count);
1943
Alex Elder868311b2013-05-01 12:43:03 -05001944 kmem_cache_free(rbd_obj_request_cache, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06001945}
1946
Alex Elderfb65d2282013-05-08 22:50:04 -05001947/* It's OK to call this for a device with no parent */
1948
1949static void rbd_spec_put(struct rbd_spec *spec);
1950static void rbd_dev_unparent(struct rbd_device *rbd_dev)
1951{
1952 rbd_dev_remove_parent(rbd_dev);
1953 rbd_spec_put(rbd_dev->parent_spec);
1954 rbd_dev->parent_spec = NULL;
1955 rbd_dev->parent_overlap = 0;
1956}
1957
Alex Elderbf0d5f502012-11-22 00:00:08 -06001958/*
Alex Eldera2acd002013-05-08 22:50:04 -05001959 * Parent image reference counting is used to determine when an
1960 * image's parent fields can be safely torn down--after there are no
1961 * more in-flight requests to the parent image. When the last
1962 * reference is dropped, cleaning them up is safe.
1963 */
1964static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
1965{
1966 int counter;
1967
1968 if (!rbd_dev->parent_spec)
1969 return;
1970
1971 counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
1972 if (counter > 0)
1973 return;
1974
1975 /* Last reference; clean up parent data structures */
1976
1977 if (!counter)
1978 rbd_dev_unparent(rbd_dev);
1979 else
Ilya Dryomov9584d502014-07-11 12:11:20 +04001980 rbd_warn(rbd_dev, "parent reference underflow");
Alex Eldera2acd002013-05-08 22:50:04 -05001981}
1982
1983/*
1984 * If an image has a non-zero parent overlap, get a reference to its
1985 * parent.
1986 *
1987 * Returns true if the rbd device has a parent with a non-zero
1988 * overlap and a reference for it was successfully taken, or
1989 * false otherwise.
1990 */
1991static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
1992{
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001993 int counter = 0;
Alex Eldera2acd002013-05-08 22:50:04 -05001994
1995 if (!rbd_dev->parent_spec)
1996 return false;
1997
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03001998 down_read(&rbd_dev->header_rwsem);
1999 if (rbd_dev->parent_overlap)
2000 counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
2001 up_read(&rbd_dev->header_rwsem);
Alex Eldera2acd002013-05-08 22:50:04 -05002002
2003 if (counter < 0)
Ilya Dryomov9584d502014-07-11 12:11:20 +04002004 rbd_warn(rbd_dev, "parent reference overflow");
Alex Eldera2acd002013-05-08 22:50:04 -05002005
Ilya Dryomovae43e9d2015-01-19 18:13:43 +03002006 return counter > 0;
Alex Eldera2acd002013-05-08 22:50:04 -05002007}
2008
Alex Elderbf0d5f502012-11-22 00:00:08 -06002009/*
2010 * Caller is responsible for filling in the list of object requests
2011 * that comprises the image request, and the Linux request pointer
2012 * (if there is one).
2013 */
Alex Eldercc344fa2013-02-19 12:25:56 -06002014static struct rbd_img_request *rbd_img_request_create(
2015 struct rbd_device *rbd_dev,
Alex Elderbf0d5f502012-11-22 00:00:08 -06002016 u64 offset, u64 length,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002017 enum obj_operation_type op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07002018 struct ceph_snap_context *snapc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002019{
2020 struct rbd_img_request *img_request;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002021
Ilya Dryomova0c58952018-01-22 16:03:06 +01002022 img_request = kmem_cache_zalloc(rbd_img_request_cache, GFP_NOIO);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002023 if (!img_request)
2024 return NULL;
2025
Alex Elderbf0d5f502012-11-22 00:00:08 -06002026 img_request->rbd_dev = rbd_dev;
2027 img_request->offset = offset;
2028 img_request->length = length;
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002029 if (op_type == OBJ_OP_DISCARD) {
2030 img_request_discard_set(img_request);
2031 img_request->snapc = snapc;
2032 } else if (op_type == OBJ_OP_WRITE) {
Alex Elder0c425242013-02-08 09:55:49 -06002033 img_request_write_set(img_request);
Josh Durgin4e752f02014-04-08 11:12:11 -07002034 img_request->snapc = snapc;
Alex Elder0c425242013-02-08 09:55:49 -06002035 } else {
Alex Elderbf0d5f502012-11-22 00:00:08 -06002036 img_request->snap_id = rbd_dev->spec->snap_id;
Alex Elder0c425242013-02-08 09:55:49 -06002037 }
Alex Eldera2acd002013-05-08 22:50:04 -05002038 if (rbd_dev_parent_get(rbd_dev))
Alex Elderd0b2e942013-01-24 16:13:36 -06002039 img_request_layered_set(img_request);
Ilya Dryomova0c58952018-01-22 16:03:06 +01002040
Alex Elderbf0d5f502012-11-22 00:00:08 -06002041 spin_lock_init(&img_request->completion_lock);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002042 INIT_LIST_HEAD(&img_request->obj_requests);
2043 kref_init(&img_request->kref);
2044
Alex Elder37206ee2013-02-20 17:32:08 -06002045 dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002046 obj_op_name(op_type), offset, length, img_request);
Alex Elder37206ee2013-02-20 17:32:08 -06002047
Alex Elderbf0d5f502012-11-22 00:00:08 -06002048 return img_request;
2049}
2050
2051static void rbd_img_request_destroy(struct kref *kref)
2052{
2053 struct rbd_img_request *img_request;
2054 struct rbd_obj_request *obj_request;
2055 struct rbd_obj_request *next_obj_request;
2056
2057 img_request = container_of(kref, struct rbd_img_request, kref);
2058
Alex Elder37206ee2013-02-20 17:32:08 -06002059 dout("%s: img %p\n", __func__, img_request);
2060
Alex Elderbf0d5f502012-11-22 00:00:08 -06002061 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
2062 rbd_img_obj_request_del(img_request, obj_request);
Alex Elder25dcf952013-01-25 17:08:55 -06002063 rbd_assert(img_request->obj_request_count == 0);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002064
Alex Eldera2acd002013-05-08 22:50:04 -05002065 if (img_request_layered_test(img_request)) {
2066 img_request_layered_clear(img_request);
2067 rbd_dev_parent_put(img_request->rbd_dev);
2068 }
2069
Josh Durginbef95452014-04-04 17:47:52 -07002070 if (img_request_write_test(img_request) ||
2071 img_request_discard_test(img_request))
Alex Elder812164f82013-04-30 00:44:32 -05002072 ceph_put_snap_context(img_request->snapc);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002073
Alex Elder1c2a9df2013-05-01 12:43:03 -05002074 kmem_cache_free(rbd_img_request_cache, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002075}
2076
Alex Eldere93f3152013-05-08 22:50:04 -05002077static struct rbd_img_request *rbd_parent_request_create(
2078 struct rbd_obj_request *obj_request,
2079 u64 img_offset, u64 length)
2080{
2081 struct rbd_img_request *parent_request;
2082 struct rbd_device *rbd_dev;
2083
2084 rbd_assert(obj_request->img_request);
2085 rbd_dev = obj_request->img_request->rbd_dev;
2086
Josh Durgin4e752f02014-04-08 11:12:11 -07002087 parent_request = rbd_img_request_create(rbd_dev->parent, img_offset,
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002088 length, OBJ_OP_READ, NULL);
Alex Eldere93f3152013-05-08 22:50:04 -05002089 if (!parent_request)
2090 return NULL;
2091
2092 img_request_child_set(parent_request);
2093 rbd_obj_request_get(obj_request);
2094 parent_request->obj_request = obj_request;
2095
2096 return parent_request;
2097}
2098
2099static void rbd_parent_request_destroy(struct kref *kref)
2100{
2101 struct rbd_img_request *parent_request;
2102 struct rbd_obj_request *orig_request;
2103
2104 parent_request = container_of(kref, struct rbd_img_request, kref);
2105 orig_request = parent_request->obj_request;
2106
2107 parent_request->obj_request = NULL;
2108 rbd_obj_request_put(orig_request);
2109 img_request_child_clear(parent_request);
2110
2111 rbd_img_request_destroy(kref);
2112}
2113
Alex Elder12178572013-02-08 09:55:49 -06002114static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
2115{
Alex Elder6365d332013-02-11 12:33:24 -06002116 struct rbd_img_request *img_request;
Alex Elder12178572013-02-08 09:55:49 -06002117 unsigned int xferred;
2118 int result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002119 bool more;
Alex Elder12178572013-02-08 09:55:49 -06002120
Alex Elder6365d332013-02-11 12:33:24 -06002121 rbd_assert(obj_request_img_data_test(obj_request));
2122 img_request = obj_request->img_request;
2123
Alex Elder12178572013-02-08 09:55:49 -06002124 rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
2125 xferred = (unsigned int)obj_request->xferred;
2126 result = obj_request->result;
2127 if (result) {
2128 struct rbd_device *rbd_dev = img_request->rbd_dev;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002129 enum obj_operation_type op_type;
2130
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002131 if (img_request_discard_test(img_request))
2132 op_type = OBJ_OP_DISCARD;
2133 else if (img_request_write_test(img_request))
2134 op_type = OBJ_OP_WRITE;
2135 else
2136 op_type = OBJ_OP_READ;
Alex Elder12178572013-02-08 09:55:49 -06002137
Ilya Dryomov9584d502014-07-11 12:11:20 +04002138 rbd_warn(rbd_dev, "%s %llx at %llx (%llx)",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002139 obj_op_name(op_type), obj_request->length,
2140 obj_request->img_offset, obj_request->offset);
Ilya Dryomov9584d502014-07-11 12:11:20 +04002141 rbd_warn(rbd_dev, " result %d xferred %x",
Alex Elder12178572013-02-08 09:55:49 -06002142 result, xferred);
2143 if (!img_request->result)
2144 img_request->result = result;
Ilya Dryomov082a75d2015-04-25 15:56:15 +03002145 /*
2146 * Need to end I/O on the entire obj_request worth of
2147 * bytes in case of error.
2148 */
2149 xferred = obj_request->length;
Alex Elder12178572013-02-08 09:55:49 -06002150 }
2151
Alex Elder8b3e1a52013-01-24 16:13:36 -06002152 if (img_request_child_test(img_request)) {
2153 rbd_assert(img_request->obj_request != NULL);
2154 more = obj_request->which < img_request->obj_request_count - 1;
2155 } else {
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002156 blk_status_t status = errno_to_blk_status(result);
2157
Alex Elder8b3e1a52013-01-24 16:13:36 -06002158 rbd_assert(img_request->rq != NULL);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002159
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002160 more = blk_update_request(img_request->rq, status, xferred);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01002161 if (!more)
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002162 __blk_mq_end_request(img_request->rq, status);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002163 }
2164
2165 return more;
Alex Elder12178572013-02-08 09:55:49 -06002166}
2167
Alex Elder21692382013-04-05 01:27:12 -05002168static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
2169{
2170 struct rbd_img_request *img_request;
2171 u32 which = obj_request->which;
2172 bool more = true;
2173
Alex Elder6365d332013-02-11 12:33:24 -06002174 rbd_assert(obj_request_img_data_test(obj_request));
Alex Elder21692382013-04-05 01:27:12 -05002175 img_request = obj_request->img_request;
2176
2177 dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
2178 rbd_assert(img_request != NULL);
Alex Elder21692382013-04-05 01:27:12 -05002179 rbd_assert(img_request->obj_request_count > 0);
2180 rbd_assert(which != BAD_WHICH);
2181 rbd_assert(which < img_request->obj_request_count);
Alex Elder21692382013-04-05 01:27:12 -05002182
2183 spin_lock_irq(&img_request->completion_lock);
2184 if (which != img_request->next_completion)
2185 goto out;
2186
2187 for_each_obj_request_from(img_request, obj_request) {
Alex Elder21692382013-04-05 01:27:12 -05002188 rbd_assert(more);
2189 rbd_assert(which < img_request->obj_request_count);
2190
2191 if (!obj_request_done_test(obj_request))
2192 break;
Alex Elder12178572013-02-08 09:55:49 -06002193 more = rbd_img_obj_end_request(obj_request);
Alex Elder21692382013-04-05 01:27:12 -05002194 which++;
2195 }
2196
2197 rbd_assert(more ^ (which == img_request->obj_request_count));
2198 img_request->next_completion = which;
2199out:
2200 spin_unlock_irq(&img_request->completion_lock);
Alex Elder0f2d5be2014-04-26 14:21:44 +04002201 rbd_img_request_put(img_request);
Alex Elder21692382013-04-05 01:27:12 -05002202
2203 if (!more)
2204 rbd_img_request_complete(img_request);
2205}
2206
Alex Elderf1a47392013-04-19 15:34:50 -05002207/*
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002208 * Add individual osd ops to the given ceph_osd_request and prepare
2209 * them for submission. num_ops is the current number of
2210 * osd operations already to the object request.
2211 */
2212static void rbd_img_obj_request_fill(struct rbd_obj_request *obj_request,
2213 struct ceph_osd_request *osd_request,
2214 enum obj_operation_type op_type,
2215 unsigned int num_ops)
2216{
2217 struct rbd_img_request *img_request = obj_request->img_request;
2218 struct rbd_device *rbd_dev = img_request->rbd_dev;
2219 u64 object_size = rbd_obj_bytes(&rbd_dev->header);
2220 u64 offset = obj_request->offset;
2221 u64 length = obj_request->length;
2222 u64 img_end;
2223 u16 opcode;
2224
2225 if (op_type == OBJ_OP_DISCARD) {
Josh Durgind3246fb2014-04-07 16:49:21 -07002226 if (!offset && length == object_size &&
2227 (!img_request_layered_test(img_request) ||
2228 !obj_request_overlaps_parent(obj_request))) {
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002229 opcode = CEPH_OSD_OP_DELETE;
2230 } else if ((offset + length == object_size)) {
2231 opcode = CEPH_OSD_OP_TRUNCATE;
2232 } else {
2233 down_read(&rbd_dev->header_rwsem);
2234 img_end = rbd_dev->header.image_size;
2235 up_read(&rbd_dev->header_rwsem);
2236
2237 if (obj_request->img_offset + length == img_end)
2238 opcode = CEPH_OSD_OP_TRUNCATE;
2239 else
2240 opcode = CEPH_OSD_OP_ZERO;
2241 }
2242 } else if (op_type == OBJ_OP_WRITE) {
Ilya Dryomove30b7572015-10-07 17:27:17 +02002243 if (!offset && length == object_size)
2244 opcode = CEPH_OSD_OP_WRITEFULL;
2245 else
2246 opcode = CEPH_OSD_OP_WRITE;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002247 osd_req_op_alloc_hint_init(osd_request, num_ops,
2248 object_size, object_size);
2249 num_ops++;
2250 } else {
2251 opcode = CEPH_OSD_OP_READ;
2252 }
2253
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002254 if (opcode == CEPH_OSD_OP_DELETE)
Yan, Zheng144cba12015-04-27 11:09:54 +08002255 osd_req_op_init(osd_request, num_ops, opcode, 0);
Ilya Dryomov7e868b62014-11-21 22:16:43 +03002256 else
2257 osd_req_op_extent_init(osd_request, num_ops, opcode,
2258 offset, length, 0, 0);
2259
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002260 if (obj_request->type == OBJ_REQUEST_BIO)
2261 osd_req_op_extent_osd_data_bio(osd_request, num_ops,
Ilya Dryomov5359a172018-01-20 10:30:10 +01002262 &obj_request->bio_pos, length);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002263 else if (obj_request->type == OBJ_REQUEST_PAGES)
2264 osd_req_op_extent_osd_data_pages(osd_request, num_ops,
2265 obj_request->pages, length,
2266 offset & ~PAGE_MASK, false, false);
2267
2268 /* Discards are also writes */
2269 if (op_type == OBJ_OP_WRITE || op_type == OBJ_OP_DISCARD)
2270 rbd_osd_req_format_write(obj_request);
2271 else
2272 rbd_osd_req_format_read(obj_request);
2273}
2274
2275/*
Alex Elderf1a47392013-04-19 15:34:50 -05002276 * Split up an image request into one or more object requests, each
2277 * to a different object. The "type" parameter indicates whether
2278 * "data_desc" is the pointer to the head of a list of bio
2279 * structures, or the base of a page array. In either case this
2280 * function assumes data_desc describes memory sufficient to hold
2281 * all data described by the image request.
2282 */
2283static int rbd_img_request_fill(struct rbd_img_request *img_request,
2284 enum obj_request_type type,
2285 void *data_desc)
Alex Elderbf0d5f502012-11-22 00:00:08 -06002286{
2287 struct rbd_device *rbd_dev = img_request->rbd_dev;
2288 struct rbd_obj_request *obj_request = NULL;
2289 struct rbd_obj_request *next_obj_request;
Ilya Dryomov5359a172018-01-20 10:30:10 +01002290 struct ceph_bio_iter bio_it;
Jingoo Hana1580732013-08-09 13:04:35 +09002291 struct page **pages = NULL;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002292 enum obj_operation_type op_type;
Alex Elder7da22d22013-01-24 16:13:36 -06002293 u64 img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002294 u64 resid;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002295
Alex Elderf1a47392013-04-19 15:34:50 -05002296 dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
2297 (int)type, data_desc);
Alex Elder37206ee2013-02-20 17:32:08 -06002298
Alex Elder7da22d22013-01-24 16:13:36 -06002299 img_offset = img_request->offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002300 resid = img_request->length;
Alex Elder4dda41d2013-02-20 21:59:33 -06002301 rbd_assert(resid > 0);
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002302 op_type = rbd_img_request_op_type(img_request);
Alex Elderf1a47392013-04-19 15:34:50 -05002303
2304 if (type == OBJ_REQUEST_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +01002305 bio_it = *(struct ceph_bio_iter *)data_desc;
Kent Overstreet4f024f32013-10-11 15:44:27 -07002306 rbd_assert(img_offset ==
Ilya Dryomov5359a172018-01-20 10:30:10 +01002307 bio_it.iter.bi_sector << SECTOR_SHIFT);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002308 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002309 pages = data_desc;
2310 }
2311
Alex Elderbf0d5f502012-11-22 00:00:08 -06002312 while (resid) {
Alex Elder2fa12322013-04-05 01:27:12 -05002313 struct ceph_osd_request *osd_req;
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002314 u64 object_no = img_offset >> rbd_dev->header.obj_order;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002315 u64 offset = rbd_segment_offset(rbd_dev, img_offset);
2316 u64 length = rbd_segment_length(rbd_dev, img_offset, resid);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002317
Ilya Dryomov6c696d82017-01-25 18:16:23 +01002318 obj_request = rbd_obj_request_create(type);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002319 if (!obj_request)
2320 goto out_unwind;
Ilya Dryomov62054da2014-03-04 11:57:17 +02002321
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002322 obj_request->object_no = object_no;
Ilya Dryomov67e2b652017-01-25 18:16:22 +01002323 obj_request->offset = offset;
2324 obj_request->length = length;
2325
Josh Durgin03507db2013-08-27 14:45:46 -07002326 /*
2327 * set obj_request->img_request before creating the
2328 * osd_request so that it gets the right snapc
2329 */
2330 rbd_img_obj_request_add(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002331
Alex Elderf1a47392013-04-19 15:34:50 -05002332 if (type == OBJ_REQUEST_BIO) {
Ilya Dryomov5359a172018-01-20 10:30:10 +01002333 obj_request->bio_pos = bio_it;
2334 ceph_bio_iter_advance(&bio_it, length);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08002335 } else if (type == OBJ_REQUEST_PAGES) {
Alex Elderf1a47392013-04-19 15:34:50 -05002336 unsigned int page_count;
2337
2338 obj_request->pages = pages;
2339 page_count = (u32)calc_pages_for(offset, length);
2340 obj_request->page_count = page_count;
2341 if ((offset + length) & ~PAGE_MASK)
2342 page_count--; /* more on last page */
2343 pages += page_count;
2344 }
Alex Elderbf0d5f502012-11-22 00:00:08 -06002345
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08002346 osd_req = rbd_osd_req_create(rbd_dev, op_type,
2347 (op_type == OBJ_OP_WRITE) ? 2 : 1,
2348 obj_request);
Alex Elder2fa12322013-04-05 01:27:12 -05002349 if (!osd_req)
Ilya Dryomov62054da2014-03-04 11:57:17 +02002350 goto out_unwind;
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002351
Alex Elder2fa12322013-04-05 01:27:12 -05002352 obj_request->osd_req = osd_req;
Alex Elder21692382013-04-05 01:27:12 -05002353 obj_request->callback = rbd_img_obj_callback;
Alex Elder7da22d22013-01-24 16:13:36 -06002354 obj_request->img_offset = img_offset;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002355
Josh Durgin3b434a2a2014-04-04 17:32:15 -07002356 rbd_img_obj_request_fill(obj_request, osd_req, op_type, 0);
2357
Alex Elder7da22d22013-01-24 16:13:36 -06002358 img_offset += length;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002359 resid -= length;
2360 }
2361
2362 return 0;
2363
Alex Elderbf0d5f502012-11-22 00:00:08 -06002364out_unwind:
2365 for_each_obj_request_safe(img_request, obj_request, next_obj_request)
Ilya Dryomov42dd0372014-03-04 11:57:17 +02002366 rbd_img_obj_request_del(img_request, obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002367
2368 return -ENOMEM;
2369}
2370
Alex Elder3d7efd12013-04-19 15:34:50 -05002371static void
Ilya Dryomov27617132015-07-16 17:36:11 +03002372rbd_osd_copyup_callback(struct rbd_obj_request *obj_request)
Alex Elder0eefd472013-04-19 15:34:50 -05002373{
2374 struct rbd_img_request *img_request;
2375 struct rbd_device *rbd_dev;
Alex Elder0eefd472013-04-19 15:34:50 -05002376
Ilya Dryomov27617132015-07-16 17:36:11 +03002377 dout("%s: obj %p\n", __func__, obj_request);
2378
Josh Durgind3246fb2014-04-07 16:49:21 -07002379 rbd_assert(obj_request->type == OBJ_REQUEST_BIO ||
2380 obj_request->type == OBJ_REQUEST_NODATA);
Alex Elder0eefd472013-04-19 15:34:50 -05002381 rbd_assert(obj_request_img_data_test(obj_request));
2382 img_request = obj_request->img_request;
2383 rbd_assert(img_request);
2384
2385 rbd_dev = img_request->rbd_dev;
2386 rbd_assert(rbd_dev);
Alex Elder0eefd472013-04-19 15:34:50 -05002387
Alex Elder0eefd472013-04-19 15:34:50 -05002388 /*
2389 * We want the transfer count to reflect the size of the
2390 * original write request. There is no such thing as a
2391 * successful short write, so if the request was successful
2392 * we can just set it to the originally-requested length.
2393 */
2394 if (!obj_request->result)
2395 obj_request->xferred = obj_request->length;
2396
Ilya Dryomov27617132015-07-16 17:36:11 +03002397 obj_request_done_set(obj_request);
Alex Elder0eefd472013-04-19 15:34:50 -05002398}
2399
2400static void
Alex Elder3d7efd12013-04-19 15:34:50 -05002401rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
2402{
2403 struct rbd_obj_request *orig_request;
Alex Elder0eefd472013-04-19 15:34:50 -05002404 struct ceph_osd_request *osd_req;
Alex Elder0eefd472013-04-19 15:34:50 -05002405 struct rbd_device *rbd_dev;
Josh Durgind3246fb2014-04-07 16:49:21 -07002406 enum obj_operation_type op_type;
Alex Elderbbea1c12013-05-06 17:40:33 -05002407 int img_result;
Alex Elderebda6402013-05-10 16:29:22 -05002408 u64 parent_length;
Alex Elder3d7efd12013-04-19 15:34:50 -05002409
2410 rbd_assert(img_request_child_test(img_request));
2411
2412 /* First get what we need from the image request */
2413
Alex Elder3d7efd12013-04-19 15:34:50 -05002414 orig_request = img_request->obj_request;
2415 rbd_assert(orig_request != NULL);
Alex Elderb91f09f2013-05-10 16:29:22 -05002416 rbd_assert(obj_request_type_valid(orig_request->type));
Alex Elderbbea1c12013-05-06 17:40:33 -05002417 img_result = img_request->result;
Alex Elderebda6402013-05-10 16:29:22 -05002418 parent_length = img_request->length;
Ilya Dryomovfa355112016-09-16 15:20:42 +02002419 rbd_assert(img_result || parent_length == img_request->xferred);
Alex Elder3d7efd12013-04-19 15:34:50 -05002420 rbd_img_request_put(img_request);
2421
Alex Elder91c6feb2013-05-06 17:40:32 -05002422 rbd_assert(orig_request->img_request);
2423 rbd_dev = orig_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002424 rbd_assert(rbd_dev);
Alex Elder3d7efd12013-04-19 15:34:50 -05002425
Alex Elderbbea1c12013-05-06 17:40:33 -05002426 /*
2427 * If the overlap has become 0 (most likely because the
2428 * image has been flattened) we need to free the pages
2429 * and re-submit the original write request.
2430 */
2431 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002432 rbd_obj_request_submit(orig_request);
2433 return;
Alex Elderbbea1c12013-05-06 17:40:33 -05002434 }
2435
2436 if (img_result)
Alex Elder0eefd472013-04-19 15:34:50 -05002437 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002438
Alex Elder8785b1d2013-05-09 10:08:49 -05002439 /*
2440 * The original osd request is of no use to use any more.
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002441 * We need a new one that can hold the three ops in a copyup
Alex Elder8785b1d2013-05-09 10:08:49 -05002442 * request. Allocate the new copyup osd request for the
2443 * original request, and release the old one.
2444 */
Alex Elderbbea1c12013-05-06 17:40:33 -05002445 img_result = -ENOMEM;
Alex Elder0eefd472013-04-19 15:34:50 -05002446 osd_req = rbd_osd_req_create_copyup(orig_request);
2447 if (!osd_req)
2448 goto out_err;
Alex Elder8785b1d2013-05-09 10:08:49 -05002449 rbd_osd_req_destroy(orig_request->osd_req);
Alex Elder0eefd472013-04-19 15:34:50 -05002450 orig_request->osd_req = osd_req;
Alex Elder3d7efd12013-04-19 15:34:50 -05002451
Alex Elder0eefd472013-04-19 15:34:50 -05002452 /* Initialize the copyup op */
2453
2454 osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
Ilya Dryomovf9dcbc42018-01-20 10:30:11 +01002455 osd_req_op_cls_request_data_pages(osd_req, 0, orig_request->copyup_pages,
2456 parent_length, 0, false, false);
Alex Elder0eefd472013-04-19 15:34:50 -05002457
Josh Durgind3246fb2014-04-07 16:49:21 -07002458 /* Add the other op(s) */
Ilya Dryomov0ccd5922014-02-25 16:22:28 +02002459
Josh Durgind3246fb2014-04-07 16:49:21 -07002460 op_type = rbd_img_request_op_type(orig_request->img_request);
2461 rbd_img_obj_request_fill(orig_request, osd_req, op_type, 1);
Alex Elder0eefd472013-04-19 15:34:50 -05002462
2463 /* All set, send it off. */
2464
Ilya Dryomov980917f2016-09-12 18:59:42 +02002465 rbd_obj_request_submit(orig_request);
2466 return;
Alex Elder0eefd472013-04-19 15:34:50 -05002467
Alex Elder0eefd472013-04-19 15:34:50 -05002468out_err:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002469 rbd_obj_request_error(orig_request, img_result);
Alex Elder3d7efd12013-04-19 15:34:50 -05002470}
2471
2472/*
2473 * Read from the parent image the range of data that covers the
2474 * entire target of the given object request. This is used for
2475 * satisfying a layered image write request when the target of an
2476 * object request from the image request does not exist.
2477 *
2478 * A page array big enough to hold the returned data is allocated
2479 * and supplied to rbd_img_request_fill() as the "data descriptor."
2480 * When the read completes, this page array will be transferred to
2481 * the original object request for the copyup operation.
2482 *
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002483 * If an error occurs, it is recorded as the result of the original
2484 * object request in rbd_img_obj_exists_callback().
Alex Elder3d7efd12013-04-19 15:34:50 -05002485 */
2486static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
2487{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002488 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elder3d7efd12013-04-19 15:34:50 -05002489 struct rbd_img_request *parent_request = NULL;
Alex Elder3d7efd12013-04-19 15:34:50 -05002490 u64 img_offset;
2491 u64 length;
2492 struct page **pages = NULL;
2493 u32 page_count;
2494 int result;
2495
Alex Elder3d7efd12013-04-19 15:34:50 -05002496 rbd_assert(rbd_dev->parent != NULL);
2497
2498 /*
2499 * Determine the byte range covered by the object in the
2500 * child image to which the original request was to be sent.
2501 */
2502 img_offset = obj_request->img_offset - obj_request->offset;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01002503 length = rbd_obj_bytes(&rbd_dev->header);
Alex Elder3d7efd12013-04-19 15:34:50 -05002504
2505 /*
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002506 * There is no defined parent data beyond the parent
2507 * overlap, so limit what we read at that boundary if
2508 * necessary.
2509 */
2510 if (img_offset + length > rbd_dev->parent_overlap) {
2511 rbd_assert(img_offset < rbd_dev->parent_overlap);
2512 length = rbd_dev->parent_overlap - img_offset;
2513 }
2514
2515 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002516 * Allocate a page array big enough to receive the data read
2517 * from the parent.
2518 */
2519 page_count = (u32)calc_pages_for(0, length);
Ilya Dryomov1e37f2f2017-11-06 11:33:36 +01002520 pages = ceph_alloc_page_vector(page_count, GFP_NOIO);
Alex Elder3d7efd12013-04-19 15:34:50 -05002521 if (IS_ERR(pages)) {
2522 result = PTR_ERR(pages);
Alex Elder3d7efd12013-04-19 15:34:50 -05002523 goto out_err;
2524 }
2525
Ilya Dryomovf9dcbc42018-01-20 10:30:11 +01002526 rbd_assert(!obj_request->copyup_pages);
2527 obj_request->copyup_pages = pages;
2528 obj_request->copyup_page_count = page_count;
2529
Alex Elder3d7efd12013-04-19 15:34:50 -05002530 result = -ENOMEM;
Alex Eldere93f3152013-05-08 22:50:04 -05002531 parent_request = rbd_parent_request_create(obj_request,
2532 img_offset, length);
Alex Elder3d7efd12013-04-19 15:34:50 -05002533 if (!parent_request)
2534 goto out_err;
Alex Elder3d7efd12013-04-19 15:34:50 -05002535
2536 result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
2537 if (result)
2538 goto out_err;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002539
Alex Elder3d7efd12013-04-19 15:34:50 -05002540 parent_request->callback = rbd_img_obj_parent_read_full_callback;
Ilya Dryomov058aa992016-09-12 14:44:45 +02002541
Alex Elder3d7efd12013-04-19 15:34:50 -05002542 result = rbd_img_request_submit(parent_request);
2543 if (!result)
2544 return 0;
2545
Alex Elder3d7efd12013-04-19 15:34:50 -05002546out_err:
Alex Elder3d7efd12013-04-19 15:34:50 -05002547 if (parent_request)
2548 rbd_img_request_put(parent_request);
Alex Elder3d7efd12013-04-19 15:34:50 -05002549 return result;
2550}
2551
Alex Elderc5b5ef62013-02-11 12:33:24 -06002552static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
2553{
Alex Elderc5b5ef62013-02-11 12:33:24 -06002554 struct rbd_obj_request *orig_request;
Alex Elder638f5ab2013-05-06 17:40:33 -05002555 struct rbd_device *rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002556 int result;
2557
2558 rbd_assert(!obj_request_img_data_test(obj_request));
2559
2560 /*
2561 * All we need from the object request is the original
2562 * request and the result of the STAT op. Grab those, then
2563 * we're done with the request.
2564 */
2565 orig_request = obj_request->obj_request;
2566 obj_request->obj_request = NULL;
Alex Elder912c3172013-05-13 20:35:38 -05002567 rbd_obj_request_put(orig_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002568 rbd_assert(orig_request);
2569 rbd_assert(orig_request->img_request);
2570
2571 result = obj_request->result;
2572 obj_request->result = 0;
2573
2574 dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
2575 obj_request, orig_request, result,
2576 obj_request->xferred, obj_request->length);
2577 rbd_obj_request_put(obj_request);
2578
Alex Elder638f5ab2013-05-06 17:40:33 -05002579 /*
2580 * If the overlap has become 0 (most likely because the
Ilya Dryomov980917f2016-09-12 18:59:42 +02002581 * image has been flattened) we need to re-submit the
2582 * original request.
Alex Elder638f5ab2013-05-06 17:40:33 -05002583 */
2584 rbd_dev = orig_request->img_request->rbd_dev;
2585 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002586 rbd_obj_request_submit(orig_request);
2587 return;
Alex Elder638f5ab2013-05-06 17:40:33 -05002588 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002589
2590 /*
2591 * Our only purpose here is to determine whether the object
2592 * exists, and we don't want to treat the non-existence as
2593 * an error. If something else comes back, transfer the
2594 * error to the original request and complete it now.
2595 */
2596 if (!result) {
2597 obj_request_existence_set(orig_request, true);
2598 } else if (result == -ENOENT) {
2599 obj_request_existence_set(orig_request, false);
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002600 } else {
2601 goto fail_orig_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002602 }
2603
2604 /*
2605 * Resubmit the original request now that we have recorded
2606 * whether the target object exists.
2607 */
Ilya Dryomovc2e82412016-09-13 20:18:01 +02002608 result = rbd_img_obj_request_submit(orig_request);
2609 if (result)
2610 goto fail_orig_request;
2611
2612 return;
2613
2614fail_orig_request:
Ilya Dryomov0dcc6852016-09-26 15:43:52 +02002615 rbd_obj_request_error(orig_request, result);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002616}
2617
2618static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
2619{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002620 struct rbd_device *rbd_dev = obj_request->img_request->rbd_dev;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002621 struct rbd_obj_request *stat_request;
Ilya Dryomov710214e2016-09-15 17:53:32 +02002622 struct page **pages;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002623 int ret;
2624
Ilya Dryomov06fbb692018-01-20 10:30:10 +01002625 stat_request = rbd_obj_request_create(OBJ_REQUEST_NODATA);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002626 if (!stat_request)
2627 return -ENOMEM;
2628
Ilya Dryomova90bb0c2017-01-25 18:16:23 +01002629 stat_request->object_no = obj_request->object_no;
2630
Ilya Dryomov710214e2016-09-15 17:53:32 +02002631 stat_request->osd_req = rbd_osd_req_create(rbd_dev, OBJ_OP_READ, 1,
2632 stat_request);
2633 if (!stat_request->osd_req) {
2634 ret = -ENOMEM;
2635 goto fail_stat_request;
2636 }
2637
Alex Elderc5b5ef62013-02-11 12:33:24 -06002638 /*
2639 * The response data for a STAT call consists of:
2640 * le64 length;
2641 * struct {
2642 * le32 tv_sec;
2643 * le32 tv_nsec;
2644 * } mtime;
2645 */
Ilya Dryomov06fbb692018-01-20 10:30:10 +01002646 pages = ceph_alloc_page_vector(1, GFP_NOIO);
Ilya Dryomov710214e2016-09-15 17:53:32 +02002647 if (IS_ERR(pages)) {
2648 ret = PTR_ERR(pages);
2649 goto fail_stat_request;
2650 }
Alex Elderc5b5ef62013-02-11 12:33:24 -06002651
Ilya Dryomov710214e2016-09-15 17:53:32 +02002652 osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT, 0);
Ilya Dryomov06fbb692018-01-20 10:30:10 +01002653 osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages,
2654 8 + sizeof(struct ceph_timespec),
2655 0, false, true);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002656
2657 rbd_obj_request_get(obj_request);
2658 stat_request->obj_request = obj_request;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002659 stat_request->callback = rbd_img_obj_exists_callback;
2660
Ilya Dryomov980917f2016-09-12 18:59:42 +02002661 rbd_obj_request_submit(stat_request);
2662 return 0;
Alex Elderc5b5ef62013-02-11 12:33:24 -06002663
Ilya Dryomov710214e2016-09-15 17:53:32 +02002664fail_stat_request:
2665 rbd_obj_request_put(stat_request);
Alex Elderc5b5ef62013-02-11 12:33:24 -06002666 return ret;
2667}
2668
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002669static bool img_obj_request_simple(struct rbd_obj_request *obj_request)
Alex Elderb454e362013-04-19 15:34:50 -05002670{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002671 struct rbd_img_request *img_request = obj_request->img_request;
2672 struct rbd_device *rbd_dev = img_request->rbd_dev;
Alex Elderb454e362013-04-19 15:34:50 -05002673
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002674 /* Reads */
Josh Durgin1c220882014-04-04 17:49:12 -07002675 if (!img_request_write_test(img_request) &&
2676 !img_request_discard_test(img_request))
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002677 return true;
Alex Elderb454e362013-04-19 15:34:50 -05002678
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002679 /* Non-layered writes */
2680 if (!img_request_layered_test(img_request))
2681 return true;
2682
2683 /*
2684 * Layered writes outside of the parent overlap range don't
2685 * share any data with the parent.
2686 */
2687 if (!obj_request_overlaps_parent(obj_request))
2688 return true;
2689
2690 /*
Guangliang Zhaoc622d222014-04-01 22:22:15 +08002691 * Entire-object layered writes - we will overwrite whatever
2692 * parent data there is anyway.
2693 */
2694 if (!obj_request->offset &&
2695 obj_request->length == rbd_obj_bytes(&rbd_dev->header))
2696 return true;
2697
2698 /*
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002699 * If the object is known to already exist, its parent data has
2700 * already been copied.
2701 */
2702 if (obj_request_known_test(obj_request) &&
2703 obj_request_exists_test(obj_request))
2704 return true;
2705
2706 return false;
2707}
2708
2709static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
2710{
Ilya Dryomov058aa992016-09-12 14:44:45 +02002711 rbd_assert(obj_request_img_data_test(obj_request));
2712 rbd_assert(obj_request_type_valid(obj_request->type));
2713 rbd_assert(obj_request->img_request);
2714
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002715 if (img_obj_request_simple(obj_request)) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002716 rbd_obj_request_submit(obj_request);
2717 return 0;
Alex Elderb454e362013-04-19 15:34:50 -05002718 }
2719
2720 /*
Alex Elder3d7efd12013-04-19 15:34:50 -05002721 * It's a layered write. The target object might exist but
2722 * we may not know that yet. If we know it doesn't exist,
2723 * start by reading the data for the full target object from
2724 * the parent so we can use it for a copyup to the target.
Alex Elderb454e362013-04-19 15:34:50 -05002725 */
Ilya Dryomov70d045f2014-09-12 16:02:01 +04002726 if (obj_request_known_test(obj_request))
Alex Elder3d7efd12013-04-19 15:34:50 -05002727 return rbd_img_obj_parent_read_full(obj_request);
2728
2729 /* We don't know whether the target exists. Go find out. */
Alex Elderb454e362013-04-19 15:34:50 -05002730
2731 return rbd_img_obj_exists_submit(obj_request);
2732}
2733
Alex Elderbf0d5f502012-11-22 00:00:08 -06002734static int rbd_img_request_submit(struct rbd_img_request *img_request)
2735{
Alex Elderbf0d5f502012-11-22 00:00:08 -06002736 struct rbd_obj_request *obj_request;
Alex Elder46faeed2013-04-10 17:47:46 -05002737 struct rbd_obj_request *next_obj_request;
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002738 int ret = 0;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002739
Alex Elder37206ee2013-02-20 17:32:08 -06002740 dout("%s: img %p\n", __func__, img_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002741
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002742 rbd_img_request_get(img_request);
2743 for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
Alex Elderb454e362013-04-19 15:34:50 -05002744 ret = rbd_img_obj_request_submit(obj_request);
Alex Elderbf0d5f502012-11-22 00:00:08 -06002745 if (ret)
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002746 goto out_put_ireq;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002747 }
2748
Ilya Dryomov663ae2c2016-05-16 13:18:57 +02002749out_put_ireq:
2750 rbd_img_request_put(img_request);
2751 return ret;
Alex Elderbf0d5f502012-11-22 00:00:08 -06002752}
2753
Alex Elder8b3e1a52013-01-24 16:13:36 -06002754static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
2755{
2756 struct rbd_obj_request *obj_request;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002757 struct rbd_device *rbd_dev;
2758 u64 obj_end;
Alex Elder02c74fb2013-05-06 17:40:33 -05002759 u64 img_xferred;
2760 int img_result;
Alex Elder8b3e1a52013-01-24 16:13:36 -06002761
2762 rbd_assert(img_request_child_test(img_request));
2763
Alex Elder02c74fb2013-05-06 17:40:33 -05002764 /* First get what we need from the image request and release it */
2765
Alex Elder8b3e1a52013-01-24 16:13:36 -06002766 obj_request = img_request->obj_request;
Alex Elder02c74fb2013-05-06 17:40:33 -05002767 img_xferred = img_request->xferred;
2768 img_result = img_request->result;
2769 rbd_img_request_put(img_request);
2770
2771 /*
2772 * If the overlap has become 0 (most likely because the
2773 * image has been flattened) we need to re-submit the
2774 * original request.
2775 */
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002776 rbd_assert(obj_request);
2777 rbd_assert(obj_request->img_request);
Alex Elder02c74fb2013-05-06 17:40:33 -05002778 rbd_dev = obj_request->img_request->rbd_dev;
2779 if (!rbd_dev->parent_overlap) {
Ilya Dryomov980917f2016-09-12 18:59:42 +02002780 rbd_obj_request_submit(obj_request);
2781 return;
Alex Elder02c74fb2013-05-06 17:40:33 -05002782 }
2783
2784 obj_request->result = img_result;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002785 if (obj_request->result)
2786 goto out;
2787
2788 /*
2789 * We need to zero anything beyond the parent overlap
2790 * boundary. Since rbd_img_obj_request_read_callback()
2791 * will zero anything beyond the end of a short read, an
2792 * easy way to do this is to pretend the data from the
2793 * parent came up short--ending at the overlap boundary.
2794 */
2795 rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
2796 obj_end = obj_request->img_offset + obj_request->length;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002797 if (obj_end > rbd_dev->parent_overlap) {
2798 u64 xferred = 0;
2799
2800 if (obj_request->img_offset < rbd_dev->parent_overlap)
2801 xferred = rbd_dev->parent_overlap -
2802 obj_request->img_offset;
2803
Alex Elder02c74fb2013-05-06 17:40:33 -05002804 obj_request->xferred = min(img_xferred, xferred);
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002805 } else {
Alex Elder02c74fb2013-05-06 17:40:33 -05002806 obj_request->xferred = img_xferred;
Alex Eldera9e8ba2c2013-04-21 00:32:07 -05002807 }
2808out:
Alex Elder8b3e1a52013-01-24 16:13:36 -06002809 rbd_img_obj_request_read_callback(obj_request);
2810 rbd_obj_request_complete(obj_request);
2811}
2812
2813static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
2814{
Alex Elder8b3e1a52013-01-24 16:13:36 -06002815 struct rbd_img_request *img_request;
2816 int result;
2817
2818 rbd_assert(obj_request_img_data_test(obj_request));
2819 rbd_assert(obj_request->img_request != NULL);
2820 rbd_assert(obj_request->result == (s32) -ENOENT);
Alex Elder5b2ab722013-05-06 17:40:33 -05002821 rbd_assert(obj_request_type_valid(obj_request->type));
Alex Elder8b3e1a52013-01-24 16:13:36 -06002822
Alex Elder8b3e1a52013-01-24 16:13:36 -06002823 /* rbd_read_finish(obj_request, obj_request->length); */
Alex Eldere93f3152013-05-08 22:50:04 -05002824 img_request = rbd_parent_request_create(obj_request,
Alex Elder8b3e1a52013-01-24 16:13:36 -06002825 obj_request->img_offset,
Alex Eldere93f3152013-05-08 22:50:04 -05002826 obj_request->length);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002827 result = -ENOMEM;
2828 if (!img_request)
2829 goto out_err;
2830
Alex Elder5b2ab722013-05-06 17:40:33 -05002831 if (obj_request->type == OBJ_REQUEST_BIO)
2832 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
Ilya Dryomov5359a172018-01-20 10:30:10 +01002833 &obj_request->bio_pos);
Alex Elder5b2ab722013-05-06 17:40:33 -05002834 else
2835 result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
2836 obj_request->pages);
Alex Elder8b3e1a52013-01-24 16:13:36 -06002837 if (result)
2838 goto out_err;
2839
2840 img_request->callback = rbd_img_parent_read_callback;
2841 result = rbd_img_request_submit(img_request);
2842 if (result)
2843 goto out_err;
2844
2845 return;
2846out_err:
2847 if (img_request)
2848 rbd_img_request_put(img_request);
2849 obj_request->result = result;
2850 obj_request->xferred = 0;
2851 obj_request_done_set(obj_request);
2852}
2853
Ilya Dryomoved95b212016-08-12 16:40:02 +02002854static const struct rbd_client_id rbd_empty_cid;
2855
2856static bool rbd_cid_equal(const struct rbd_client_id *lhs,
2857 const struct rbd_client_id *rhs)
2858{
2859 return lhs->gid == rhs->gid && lhs->handle == rhs->handle;
2860}
2861
2862static struct rbd_client_id rbd_get_cid(struct rbd_device *rbd_dev)
2863{
2864 struct rbd_client_id cid;
2865
2866 mutex_lock(&rbd_dev->watch_mutex);
2867 cid.gid = ceph_client_gid(rbd_dev->rbd_client->client);
2868 cid.handle = rbd_dev->watch_cookie;
2869 mutex_unlock(&rbd_dev->watch_mutex);
2870 return cid;
2871}
2872
2873/*
2874 * lock_rwsem must be held for write
2875 */
2876static void rbd_set_owner_cid(struct rbd_device *rbd_dev,
2877 const struct rbd_client_id *cid)
2878{
2879 dout("%s rbd_dev %p %llu-%llu -> %llu-%llu\n", __func__, rbd_dev,
2880 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle,
2881 cid->gid, cid->handle);
2882 rbd_dev->owner_cid = *cid; /* struct */
2883}
2884
2885static void format_lock_cookie(struct rbd_device *rbd_dev, char *buf)
2886{
2887 mutex_lock(&rbd_dev->watch_mutex);
2888 sprintf(buf, "%s %llu", RBD_LOCK_COOKIE_PREFIX, rbd_dev->watch_cookie);
2889 mutex_unlock(&rbd_dev->watch_mutex);
2890}
2891
Florian Margaineedd8ca82017-12-13 16:43:59 +01002892static void __rbd_lock(struct rbd_device *rbd_dev, const char *cookie)
2893{
2894 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
2895
2896 strcpy(rbd_dev->lock_cookie, cookie);
2897 rbd_set_owner_cid(rbd_dev, &cid);
2898 queue_work(rbd_dev->task_wq, &rbd_dev->acquired_lock_work);
2899}
2900
Ilya Dryomoved95b212016-08-12 16:40:02 +02002901/*
2902 * lock_rwsem must be held for write
2903 */
2904static int rbd_lock(struct rbd_device *rbd_dev)
2905{
2906 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02002907 char cookie[32];
2908 int ret;
2909
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02002910 WARN_ON(__rbd_is_lock_owner(rbd_dev) ||
2911 rbd_dev->lock_cookie[0] != '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02002912
2913 format_lock_cookie(rbd_dev, cookie);
2914 ret = ceph_cls_lock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
2915 RBD_LOCK_NAME, CEPH_CLS_LOCK_EXCLUSIVE, cookie,
2916 RBD_LOCK_TAG, "", 0);
2917 if (ret)
2918 return ret;
2919
2920 rbd_dev->lock_state = RBD_LOCK_STATE_LOCKED;
Florian Margaineedd8ca82017-12-13 16:43:59 +01002921 __rbd_lock(rbd_dev, cookie);
Ilya Dryomoved95b212016-08-12 16:40:02 +02002922 return 0;
2923}
2924
2925/*
2926 * lock_rwsem must be held for write
2927 */
Ilya Dryomovbbead742017-04-13 12:17:38 +02002928static void rbd_unlock(struct rbd_device *rbd_dev)
Ilya Dryomoved95b212016-08-12 16:40:02 +02002929{
2930 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomoved95b212016-08-12 16:40:02 +02002931 int ret;
2932
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02002933 WARN_ON(!__rbd_is_lock_owner(rbd_dev) ||
2934 rbd_dev->lock_cookie[0] == '\0');
Ilya Dryomoved95b212016-08-12 16:40:02 +02002935
Ilya Dryomoved95b212016-08-12 16:40:02 +02002936 ret = ceph_cls_unlock(osdc, &rbd_dev->header_oid, &rbd_dev->header_oloc,
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02002937 RBD_LOCK_NAME, rbd_dev->lock_cookie);
Ilya Dryomovbbead742017-04-13 12:17:38 +02002938 if (ret && ret != -ENOENT)
2939 rbd_warn(rbd_dev, "failed to unlock: %d", ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02002940
Ilya Dryomovbbead742017-04-13 12:17:38 +02002941 /* treat errors as the image is unlocked */
2942 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
Ilya Dryomovcbbfb0f2017-04-13 12:17:38 +02002943 rbd_dev->lock_cookie[0] = '\0';
Ilya Dryomoved95b212016-08-12 16:40:02 +02002944 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
2945 queue_work(rbd_dev->task_wq, &rbd_dev->released_lock_work);
Ilya Dryomoved95b212016-08-12 16:40:02 +02002946}
2947
2948static int __rbd_notify_op_lock(struct rbd_device *rbd_dev,
2949 enum rbd_notify_op notify_op,
2950 struct page ***preply_pages,
2951 size_t *preply_len)
2952{
2953 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
2954 struct rbd_client_id cid = rbd_get_cid(rbd_dev);
2955 int buf_size = 4 + 8 + 8 + CEPH_ENCODING_START_BLK_LEN;
2956 char buf[buf_size];
2957 void *p = buf;
2958
2959 dout("%s rbd_dev %p notify_op %d\n", __func__, rbd_dev, notify_op);
2960
2961 /* encode *LockPayload NotifyMessage (op + ClientId) */
2962 ceph_start_encoding(&p, 2, 1, buf_size - CEPH_ENCODING_START_BLK_LEN);
2963 ceph_encode_32(&p, notify_op);
2964 ceph_encode_64(&p, cid.gid);
2965 ceph_encode_64(&p, cid.handle);
2966
2967 return ceph_osdc_notify(osdc, &rbd_dev->header_oid,
2968 &rbd_dev->header_oloc, buf, buf_size,
2969 RBD_NOTIFY_TIMEOUT, preply_pages, preply_len);
2970}
2971
2972static void rbd_notify_op_lock(struct rbd_device *rbd_dev,
2973 enum rbd_notify_op notify_op)
2974{
2975 struct page **reply_pages;
2976 size_t reply_len;
2977
2978 __rbd_notify_op_lock(rbd_dev, notify_op, &reply_pages, &reply_len);
2979 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
2980}
2981
2982static void rbd_notify_acquired_lock(struct work_struct *work)
2983{
2984 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
2985 acquired_lock_work);
2986
2987 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_ACQUIRED_LOCK);
2988}
2989
2990static void rbd_notify_released_lock(struct work_struct *work)
2991{
2992 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
2993 released_lock_work);
2994
2995 rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_RELEASED_LOCK);
2996}
2997
2998static int rbd_request_lock(struct rbd_device *rbd_dev)
2999{
3000 struct page **reply_pages;
3001 size_t reply_len;
3002 bool lock_owner_responded = false;
3003 int ret;
3004
3005 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3006
3007 ret = __rbd_notify_op_lock(rbd_dev, RBD_NOTIFY_OP_REQUEST_LOCK,
3008 &reply_pages, &reply_len);
3009 if (ret && ret != -ETIMEDOUT) {
3010 rbd_warn(rbd_dev, "failed to request lock: %d", ret);
3011 goto out;
3012 }
3013
3014 if (reply_len > 0 && reply_len <= PAGE_SIZE) {
3015 void *p = page_address(reply_pages[0]);
3016 void *const end = p + reply_len;
3017 u32 n;
3018
3019 ceph_decode_32_safe(&p, end, n, e_inval); /* num_acks */
3020 while (n--) {
3021 u8 struct_v;
3022 u32 len;
3023
3024 ceph_decode_need(&p, end, 8 + 8, e_inval);
3025 p += 8 + 8; /* skip gid and cookie */
3026
3027 ceph_decode_32_safe(&p, end, len, e_inval);
3028 if (!len)
3029 continue;
3030
3031 if (lock_owner_responded) {
3032 rbd_warn(rbd_dev,
3033 "duplicate lock owners detected");
3034 ret = -EIO;
3035 goto out;
3036 }
3037
3038 lock_owner_responded = true;
3039 ret = ceph_start_decoding(&p, end, 1, "ResponseMessage",
3040 &struct_v, &len);
3041 if (ret) {
3042 rbd_warn(rbd_dev,
3043 "failed to decode ResponseMessage: %d",
3044 ret);
3045 goto e_inval;
3046 }
3047
3048 ret = ceph_decode_32(&p);
3049 }
3050 }
3051
3052 if (!lock_owner_responded) {
3053 rbd_warn(rbd_dev, "no lock owners detected");
3054 ret = -ETIMEDOUT;
3055 }
3056
3057out:
3058 ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len));
3059 return ret;
3060
3061e_inval:
3062 ret = -EINVAL;
3063 goto out;
3064}
3065
3066static void wake_requests(struct rbd_device *rbd_dev, bool wake_all)
3067{
3068 dout("%s rbd_dev %p wake_all %d\n", __func__, rbd_dev, wake_all);
3069
3070 cancel_delayed_work(&rbd_dev->lock_dwork);
3071 if (wake_all)
3072 wake_up_all(&rbd_dev->lock_waitq);
3073 else
3074 wake_up(&rbd_dev->lock_waitq);
3075}
3076
3077static int get_lock_owner_info(struct rbd_device *rbd_dev,
3078 struct ceph_locker **lockers, u32 *num_lockers)
3079{
3080 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3081 u8 lock_type;
3082 char *lock_tag;
3083 int ret;
3084
3085 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3086
3087 ret = ceph_cls_lock_info(osdc, &rbd_dev->header_oid,
3088 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3089 &lock_type, &lock_tag, lockers, num_lockers);
3090 if (ret)
3091 return ret;
3092
3093 if (*num_lockers == 0) {
3094 dout("%s rbd_dev %p no lockers detected\n", __func__, rbd_dev);
3095 goto out;
3096 }
3097
3098 if (strcmp(lock_tag, RBD_LOCK_TAG)) {
3099 rbd_warn(rbd_dev, "locked by external mechanism, tag %s",
3100 lock_tag);
3101 ret = -EBUSY;
3102 goto out;
3103 }
3104
3105 if (lock_type == CEPH_CLS_LOCK_SHARED) {
3106 rbd_warn(rbd_dev, "shared lock type detected");
3107 ret = -EBUSY;
3108 goto out;
3109 }
3110
3111 if (strncmp((*lockers)[0].id.cookie, RBD_LOCK_COOKIE_PREFIX,
3112 strlen(RBD_LOCK_COOKIE_PREFIX))) {
3113 rbd_warn(rbd_dev, "locked by external mechanism, cookie %s",
3114 (*lockers)[0].id.cookie);
3115 ret = -EBUSY;
3116 goto out;
3117 }
3118
3119out:
3120 kfree(lock_tag);
3121 return ret;
3122}
3123
3124static int find_watcher(struct rbd_device *rbd_dev,
3125 const struct ceph_locker *locker)
3126{
3127 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3128 struct ceph_watch_item *watchers;
3129 u32 num_watchers;
3130 u64 cookie;
3131 int i;
3132 int ret;
3133
3134 ret = ceph_osdc_list_watchers(osdc, &rbd_dev->header_oid,
3135 &rbd_dev->header_oloc, &watchers,
3136 &num_watchers);
3137 if (ret)
3138 return ret;
3139
3140 sscanf(locker->id.cookie, RBD_LOCK_COOKIE_PREFIX " %llu", &cookie);
3141 for (i = 0; i < num_watchers; i++) {
3142 if (!memcmp(&watchers[i].addr, &locker->info.addr,
3143 sizeof(locker->info.addr)) &&
3144 watchers[i].cookie == cookie) {
3145 struct rbd_client_id cid = {
3146 .gid = le64_to_cpu(watchers[i].name.num),
3147 .handle = cookie,
3148 };
3149
3150 dout("%s rbd_dev %p found cid %llu-%llu\n", __func__,
3151 rbd_dev, cid.gid, cid.handle);
3152 rbd_set_owner_cid(rbd_dev, &cid);
3153 ret = 1;
3154 goto out;
3155 }
3156 }
3157
3158 dout("%s rbd_dev %p no watchers\n", __func__, rbd_dev);
3159 ret = 0;
3160out:
3161 kfree(watchers);
3162 return ret;
3163}
3164
3165/*
3166 * lock_rwsem must be held for write
3167 */
3168static int rbd_try_lock(struct rbd_device *rbd_dev)
3169{
3170 struct ceph_client *client = rbd_dev->rbd_client->client;
3171 struct ceph_locker *lockers;
3172 u32 num_lockers;
3173 int ret;
3174
3175 for (;;) {
3176 ret = rbd_lock(rbd_dev);
3177 if (ret != -EBUSY)
3178 return ret;
3179
3180 /* determine if the current lock holder is still alive */
3181 ret = get_lock_owner_info(rbd_dev, &lockers, &num_lockers);
3182 if (ret)
3183 return ret;
3184
3185 if (num_lockers == 0)
3186 goto again;
3187
3188 ret = find_watcher(rbd_dev, lockers);
3189 if (ret) {
3190 if (ret > 0)
3191 ret = 0; /* have to request lock */
3192 goto out;
3193 }
3194
3195 rbd_warn(rbd_dev, "%s%llu seems dead, breaking lock",
3196 ENTITY_NAME(lockers[0].id.name));
3197
3198 ret = ceph_monc_blacklist_add(&client->monc,
3199 &lockers[0].info.addr);
3200 if (ret) {
3201 rbd_warn(rbd_dev, "blacklist of %s%llu failed: %d",
3202 ENTITY_NAME(lockers[0].id.name), ret);
3203 goto out;
3204 }
3205
3206 ret = ceph_cls_break_lock(&client->osdc, &rbd_dev->header_oid,
3207 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3208 lockers[0].id.cookie,
3209 &lockers[0].id.name);
3210 if (ret && ret != -ENOENT)
3211 goto out;
3212
3213again:
3214 ceph_free_lockers(lockers, num_lockers);
3215 }
3216
3217out:
3218 ceph_free_lockers(lockers, num_lockers);
3219 return ret;
3220}
3221
3222/*
3223 * ret is set only if lock_state is RBD_LOCK_STATE_UNLOCKED
3224 */
3225static enum rbd_lock_state rbd_try_acquire_lock(struct rbd_device *rbd_dev,
3226 int *pret)
3227{
3228 enum rbd_lock_state lock_state;
3229
3230 down_read(&rbd_dev->lock_rwsem);
3231 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3232 rbd_dev->lock_state);
3233 if (__rbd_is_lock_owner(rbd_dev)) {
3234 lock_state = rbd_dev->lock_state;
3235 up_read(&rbd_dev->lock_rwsem);
3236 return lock_state;
3237 }
3238
3239 up_read(&rbd_dev->lock_rwsem);
3240 down_write(&rbd_dev->lock_rwsem);
3241 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3242 rbd_dev->lock_state);
3243 if (!__rbd_is_lock_owner(rbd_dev)) {
3244 *pret = rbd_try_lock(rbd_dev);
3245 if (*pret)
3246 rbd_warn(rbd_dev, "failed to acquire lock: %d", *pret);
3247 }
3248
3249 lock_state = rbd_dev->lock_state;
3250 up_write(&rbd_dev->lock_rwsem);
3251 return lock_state;
3252}
3253
3254static void rbd_acquire_lock(struct work_struct *work)
3255{
3256 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3257 struct rbd_device, lock_dwork);
3258 enum rbd_lock_state lock_state;
Kefeng Wang37f13252017-07-13 15:46:35 +08003259 int ret = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003260
3261 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3262again:
3263 lock_state = rbd_try_acquire_lock(rbd_dev, &ret);
3264 if (lock_state != RBD_LOCK_STATE_UNLOCKED || ret == -EBLACKLISTED) {
3265 if (lock_state == RBD_LOCK_STATE_LOCKED)
3266 wake_requests(rbd_dev, true);
3267 dout("%s rbd_dev %p lock_state %d ret %d - done\n", __func__,
3268 rbd_dev, lock_state, ret);
3269 return;
3270 }
3271
3272 ret = rbd_request_lock(rbd_dev);
3273 if (ret == -ETIMEDOUT) {
3274 goto again; /* treat this as a dead client */
Ilya Dryomove010dd02017-04-13 12:17:39 +02003275 } else if (ret == -EROFS) {
3276 rbd_warn(rbd_dev, "peer will not release lock");
3277 /*
3278 * If this is rbd_add_acquire_lock(), we want to fail
3279 * immediately -- reuse BLACKLISTED flag. Otherwise we
3280 * want to block.
3281 */
3282 if (!(rbd_dev->disk->flags & GENHD_FL_UP)) {
3283 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
3284 /* wake "rbd map --exclusive" process */
3285 wake_requests(rbd_dev, false);
3286 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003287 } else if (ret < 0) {
3288 rbd_warn(rbd_dev, "error requesting lock: %d", ret);
3289 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3290 RBD_RETRY_DELAY);
3291 } else {
3292 /*
3293 * lock owner acked, but resend if we don't see them
3294 * release the lock
3295 */
3296 dout("%s rbd_dev %p requeueing lock_dwork\n", __func__,
3297 rbd_dev);
3298 mod_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork,
3299 msecs_to_jiffies(2 * RBD_NOTIFY_TIMEOUT * MSEC_PER_SEC));
3300 }
3301}
3302
3303/*
3304 * lock_rwsem must be held for write
3305 */
3306static bool rbd_release_lock(struct rbd_device *rbd_dev)
3307{
3308 dout("%s rbd_dev %p read lock_state %d\n", __func__, rbd_dev,
3309 rbd_dev->lock_state);
3310 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED)
3311 return false;
3312
3313 rbd_dev->lock_state = RBD_LOCK_STATE_RELEASING;
3314 downgrade_write(&rbd_dev->lock_rwsem);
3315 /*
3316 * Ensure that all in-flight IO is flushed.
3317 *
3318 * FIXME: ceph_osdc_sync() flushes the entire OSD client, which
3319 * may be shared with other devices.
3320 */
3321 ceph_osdc_sync(&rbd_dev->rbd_client->client->osdc);
3322 up_read(&rbd_dev->lock_rwsem);
3323
3324 down_write(&rbd_dev->lock_rwsem);
3325 dout("%s rbd_dev %p write lock_state %d\n", __func__, rbd_dev,
3326 rbd_dev->lock_state);
3327 if (rbd_dev->lock_state != RBD_LOCK_STATE_RELEASING)
3328 return false;
3329
Ilya Dryomovbbead742017-04-13 12:17:38 +02003330 rbd_unlock(rbd_dev);
3331 /*
3332 * Give others a chance to grab the lock - we would re-acquire
3333 * almost immediately if we got new IO during ceph_osdc_sync()
3334 * otherwise. We need to ack our own notifications, so this
3335 * lock_dwork will be requeued from rbd_wait_state_locked()
3336 * after wake_requests() in rbd_handle_released_lock().
3337 */
3338 cancel_delayed_work(&rbd_dev->lock_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003339 return true;
3340}
3341
3342static void rbd_release_lock_work(struct work_struct *work)
3343{
3344 struct rbd_device *rbd_dev = container_of(work, struct rbd_device,
3345 unlock_work);
3346
3347 down_write(&rbd_dev->lock_rwsem);
3348 rbd_release_lock(rbd_dev);
3349 up_write(&rbd_dev->lock_rwsem);
3350}
3351
3352static void rbd_handle_acquired_lock(struct rbd_device *rbd_dev, u8 struct_v,
3353 void **p)
3354{
3355 struct rbd_client_id cid = { 0 };
3356
3357 if (struct_v >= 2) {
3358 cid.gid = ceph_decode_64(p);
3359 cid.handle = ceph_decode_64(p);
3360 }
3361
3362 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3363 cid.handle);
3364 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3365 down_write(&rbd_dev->lock_rwsem);
3366 if (rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3367 /*
3368 * we already know that the remote client is
3369 * the owner
3370 */
3371 up_write(&rbd_dev->lock_rwsem);
3372 return;
3373 }
3374
3375 rbd_set_owner_cid(rbd_dev, &cid);
3376 downgrade_write(&rbd_dev->lock_rwsem);
3377 } else {
3378 down_read(&rbd_dev->lock_rwsem);
3379 }
3380
3381 if (!__rbd_is_lock_owner(rbd_dev))
3382 wake_requests(rbd_dev, false);
3383 up_read(&rbd_dev->lock_rwsem);
3384}
3385
3386static void rbd_handle_released_lock(struct rbd_device *rbd_dev, u8 struct_v,
3387 void **p)
3388{
3389 struct rbd_client_id cid = { 0 };
3390
3391 if (struct_v >= 2) {
3392 cid.gid = ceph_decode_64(p);
3393 cid.handle = ceph_decode_64(p);
3394 }
3395
3396 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3397 cid.handle);
3398 if (!rbd_cid_equal(&cid, &rbd_empty_cid)) {
3399 down_write(&rbd_dev->lock_rwsem);
3400 if (!rbd_cid_equal(&cid, &rbd_dev->owner_cid)) {
3401 dout("%s rbd_dev %p unexpected owner, cid %llu-%llu != owner_cid %llu-%llu\n",
3402 __func__, rbd_dev, cid.gid, cid.handle,
3403 rbd_dev->owner_cid.gid, rbd_dev->owner_cid.handle);
3404 up_write(&rbd_dev->lock_rwsem);
3405 return;
3406 }
3407
3408 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3409 downgrade_write(&rbd_dev->lock_rwsem);
3410 } else {
3411 down_read(&rbd_dev->lock_rwsem);
3412 }
3413
3414 if (!__rbd_is_lock_owner(rbd_dev))
3415 wake_requests(rbd_dev, false);
3416 up_read(&rbd_dev->lock_rwsem);
3417}
3418
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003419/*
3420 * Returns result for ResponseMessage to be encoded (<= 0), or 1 if no
3421 * ResponseMessage is needed.
3422 */
3423static int rbd_handle_request_lock(struct rbd_device *rbd_dev, u8 struct_v,
3424 void **p)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003425{
3426 struct rbd_client_id my_cid = rbd_get_cid(rbd_dev);
3427 struct rbd_client_id cid = { 0 };
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003428 int result = 1;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003429
3430 if (struct_v >= 2) {
3431 cid.gid = ceph_decode_64(p);
3432 cid.handle = ceph_decode_64(p);
3433 }
3434
3435 dout("%s rbd_dev %p cid %llu-%llu\n", __func__, rbd_dev, cid.gid,
3436 cid.handle);
3437 if (rbd_cid_equal(&cid, &my_cid))
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003438 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003439
3440 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003441 if (__rbd_is_lock_owner(rbd_dev)) {
3442 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED &&
3443 rbd_cid_equal(&rbd_dev->owner_cid, &rbd_empty_cid))
3444 goto out_unlock;
3445
3446 /*
3447 * encode ResponseMessage(0) so the peer can detect
3448 * a missing owner
3449 */
3450 result = 0;
3451
3452 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED) {
Ilya Dryomove010dd02017-04-13 12:17:39 +02003453 if (!rbd_dev->opts->exclusive) {
3454 dout("%s rbd_dev %p queueing unlock_work\n",
3455 __func__, rbd_dev);
3456 queue_work(rbd_dev->task_wq,
3457 &rbd_dev->unlock_work);
3458 } else {
3459 /* refuse to release the lock */
3460 result = -EROFS;
3461 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003462 }
3463 }
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003464
3465out_unlock:
Ilya Dryomoved95b212016-08-12 16:40:02 +02003466 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003467 return result;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003468}
3469
3470static void __rbd_acknowledge_notify(struct rbd_device *rbd_dev,
3471 u64 notify_id, u64 cookie, s32 *result)
3472{
3473 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3474 int buf_size = 4 + CEPH_ENCODING_START_BLK_LEN;
3475 char buf[buf_size];
3476 int ret;
3477
3478 if (result) {
3479 void *p = buf;
3480
3481 /* encode ResponseMessage */
3482 ceph_start_encoding(&p, 1, 1,
3483 buf_size - CEPH_ENCODING_START_BLK_LEN);
3484 ceph_encode_32(&p, *result);
3485 } else {
3486 buf_size = 0;
3487 }
3488
3489 ret = ceph_osdc_notify_ack(osdc, &rbd_dev->header_oid,
3490 &rbd_dev->header_oloc, notify_id, cookie,
3491 buf, buf_size);
3492 if (ret)
3493 rbd_warn(rbd_dev, "acknowledge_notify failed: %d", ret);
3494}
3495
3496static void rbd_acknowledge_notify(struct rbd_device *rbd_dev, u64 notify_id,
3497 u64 cookie)
3498{
3499 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3500 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, NULL);
3501}
3502
3503static void rbd_acknowledge_notify_result(struct rbd_device *rbd_dev,
3504 u64 notify_id, u64 cookie, s32 result)
3505{
3506 dout("%s rbd_dev %p result %d\n", __func__, rbd_dev, result);
3507 __rbd_acknowledge_notify(rbd_dev, notify_id, cookie, &result);
3508}
Ilya Dryomov922dab62016-05-26 01:15:02 +02003509
3510static void rbd_watch_cb(void *arg, u64 notify_id, u64 cookie,
3511 u64 notifier_id, void *data, size_t data_len)
Alex Elderb8d70032012-11-30 17:53:04 -06003512{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003513 struct rbd_device *rbd_dev = arg;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003514 void *p = data;
3515 void *const end = p + data_len;
Ilya Dryomovd4c22692016-09-06 11:15:48 +02003516 u8 struct_v = 0;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003517 u32 len;
3518 u32 notify_op;
Alex Elderb8d70032012-11-30 17:53:04 -06003519 int ret;
3520
Ilya Dryomoved95b212016-08-12 16:40:02 +02003521 dout("%s rbd_dev %p cookie %llu notify_id %llu data_len %zu\n",
3522 __func__, rbd_dev, cookie, notify_id, data_len);
3523 if (data_len) {
3524 ret = ceph_start_decoding(&p, end, 1, "NotifyMessage",
3525 &struct_v, &len);
3526 if (ret) {
3527 rbd_warn(rbd_dev, "failed to decode NotifyMessage: %d",
3528 ret);
3529 return;
3530 }
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04003531
Ilya Dryomoved95b212016-08-12 16:40:02 +02003532 notify_op = ceph_decode_32(&p);
3533 } else {
3534 /* legacy notification for header updates */
3535 notify_op = RBD_NOTIFY_OP_HEADER_UPDATE;
3536 len = 0;
3537 }
Alex Elderb8d70032012-11-30 17:53:04 -06003538
Ilya Dryomoved95b212016-08-12 16:40:02 +02003539 dout("%s rbd_dev %p notify_op %u\n", __func__, rbd_dev, notify_op);
3540 switch (notify_op) {
3541 case RBD_NOTIFY_OP_ACQUIRED_LOCK:
3542 rbd_handle_acquired_lock(rbd_dev, struct_v, &p);
3543 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3544 break;
3545 case RBD_NOTIFY_OP_RELEASED_LOCK:
3546 rbd_handle_released_lock(rbd_dev, struct_v, &p);
3547 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3548 break;
3549 case RBD_NOTIFY_OP_REQUEST_LOCK:
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003550 ret = rbd_handle_request_lock(rbd_dev, struct_v, &p);
3551 if (ret <= 0)
Ilya Dryomoved95b212016-08-12 16:40:02 +02003552 rbd_acknowledge_notify_result(rbd_dev, notify_id,
Ilya Dryomov3b77faa2017-04-13 12:17:39 +02003553 cookie, ret);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003554 else
3555 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3556 break;
3557 case RBD_NOTIFY_OP_HEADER_UPDATE:
3558 ret = rbd_dev_refresh(rbd_dev);
3559 if (ret)
3560 rbd_warn(rbd_dev, "refresh failed: %d", ret);
3561
3562 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3563 break;
3564 default:
3565 if (rbd_is_lock_owner(rbd_dev))
3566 rbd_acknowledge_notify_result(rbd_dev, notify_id,
3567 cookie, -EOPNOTSUPP);
3568 else
3569 rbd_acknowledge_notify(rbd_dev, notify_id, cookie);
3570 break;
3571 }
Alex Elderb8d70032012-11-30 17:53:04 -06003572}
3573
Ilya Dryomov99d16942016-08-12 16:11:41 +02003574static void __rbd_unregister_watch(struct rbd_device *rbd_dev);
3575
Ilya Dryomov922dab62016-05-26 01:15:02 +02003576static void rbd_watch_errcb(void *arg, u64 cookie, int err)
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003577{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003578 struct rbd_device *rbd_dev = arg;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003579
Ilya Dryomov922dab62016-05-26 01:15:02 +02003580 rbd_warn(rbd_dev, "encountered watch error: %d", err);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003581
Ilya Dryomoved95b212016-08-12 16:40:02 +02003582 down_write(&rbd_dev->lock_rwsem);
3583 rbd_set_owner_cid(rbd_dev, &rbd_empty_cid);
3584 up_write(&rbd_dev->lock_rwsem);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003585
Ilya Dryomov99d16942016-08-12 16:11:41 +02003586 mutex_lock(&rbd_dev->watch_mutex);
3587 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED) {
3588 __rbd_unregister_watch(rbd_dev);
3589 rbd_dev->watch_state = RBD_WATCH_STATE_ERROR;
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003590
Ilya Dryomov99d16942016-08-12 16:11:41 +02003591 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->watch_dwork, 0);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003592 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003593 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomovbb040aa2014-06-19 11:38:14 +04003594}
3595
3596/*
Ilya Dryomov99d16942016-08-12 16:11:41 +02003597 * watch_mutex must be locked
Alex Elder9969ebc2013-01-18 12:31:10 -06003598 */
Ilya Dryomov99d16942016-08-12 16:11:41 +02003599static int __rbd_register_watch(struct rbd_device *rbd_dev)
Alex Elder9969ebc2013-01-18 12:31:10 -06003600{
3601 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
Ilya Dryomov922dab62016-05-26 01:15:02 +02003602 struct ceph_osd_linger_request *handle;
Alex Elder9969ebc2013-01-18 12:31:10 -06003603
Ilya Dryomov922dab62016-05-26 01:15:02 +02003604 rbd_assert(!rbd_dev->watch_handle);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003605 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Alex Elder9969ebc2013-01-18 12:31:10 -06003606
Ilya Dryomov922dab62016-05-26 01:15:02 +02003607 handle = ceph_osdc_watch(osdc, &rbd_dev->header_oid,
3608 &rbd_dev->header_oloc, rbd_watch_cb,
3609 rbd_watch_errcb, rbd_dev);
3610 if (IS_ERR(handle))
3611 return PTR_ERR(handle);
Alex Elder9969ebc2013-01-18 12:31:10 -06003612
Ilya Dryomov922dab62016-05-26 01:15:02 +02003613 rbd_dev->watch_handle = handle;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003614 return 0;
Alex Elder9969ebc2013-01-18 12:31:10 -06003615}
3616
Ilya Dryomov99d16942016-08-12 16:11:41 +02003617/*
3618 * watch_mutex must be locked
3619 */
3620static void __rbd_unregister_watch(struct rbd_device *rbd_dev)
Ilya Dryomovfca27062013-12-16 18:02:40 +02003621{
Ilya Dryomov922dab62016-05-26 01:15:02 +02003622 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3623 int ret;
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003624
Ilya Dryomov99d16942016-08-12 16:11:41 +02003625 rbd_assert(rbd_dev->watch_handle);
3626 dout("%s rbd_dev %p\n", __func__, rbd_dev);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003627
Ilya Dryomov922dab62016-05-26 01:15:02 +02003628 ret = ceph_osdc_unwatch(osdc, rbd_dev->watch_handle);
3629 if (ret)
3630 rbd_warn(rbd_dev, "failed to unwatch: %d", ret);
Ilya Dryomovb30a01f2014-05-22 19:28:52 +04003631
Ilya Dryomov922dab62016-05-26 01:15:02 +02003632 rbd_dev->watch_handle = NULL;
Ilya Dryomovc525f032016-04-28 16:07:26 +02003633}
3634
Ilya Dryomov99d16942016-08-12 16:11:41 +02003635static int rbd_register_watch(struct rbd_device *rbd_dev)
Ilya Dryomovc525f032016-04-28 16:07:26 +02003636{
Ilya Dryomov99d16942016-08-12 16:11:41 +02003637 int ret;
Ilya Dryomov811c6682016-04-15 16:22:16 +02003638
Ilya Dryomov99d16942016-08-12 16:11:41 +02003639 mutex_lock(&rbd_dev->watch_mutex);
3640 rbd_assert(rbd_dev->watch_state == RBD_WATCH_STATE_UNREGISTERED);
3641 ret = __rbd_register_watch(rbd_dev);
3642 if (ret)
3643 goto out;
3644
3645 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3646 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3647
3648out:
3649 mutex_unlock(&rbd_dev->watch_mutex);
3650 return ret;
3651}
3652
3653static void cancel_tasks_sync(struct rbd_device *rbd_dev)
3654{
3655 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3656
3657 cancel_delayed_work_sync(&rbd_dev->watch_dwork);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003658 cancel_work_sync(&rbd_dev->acquired_lock_work);
3659 cancel_work_sync(&rbd_dev->released_lock_work);
3660 cancel_delayed_work_sync(&rbd_dev->lock_dwork);
3661 cancel_work_sync(&rbd_dev->unlock_work);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003662}
3663
3664static void rbd_unregister_watch(struct rbd_device *rbd_dev)
3665{
Ilya Dryomoved95b212016-08-12 16:40:02 +02003666 WARN_ON(waitqueue_active(&rbd_dev->lock_waitq));
Ilya Dryomov99d16942016-08-12 16:11:41 +02003667 cancel_tasks_sync(rbd_dev);
3668
3669 mutex_lock(&rbd_dev->watch_mutex);
3670 if (rbd_dev->watch_state == RBD_WATCH_STATE_REGISTERED)
3671 __rbd_unregister_watch(rbd_dev);
3672 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
3673 mutex_unlock(&rbd_dev->watch_mutex);
3674
Ilya Dryomov811c6682016-04-15 16:22:16 +02003675 ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
Ilya Dryomovfca27062013-12-16 18:02:40 +02003676}
3677
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003678/*
3679 * lock_rwsem must be held for write
3680 */
3681static void rbd_reacquire_lock(struct rbd_device *rbd_dev)
3682{
3683 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3684 char cookie[32];
3685 int ret;
3686
3687 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED);
3688
3689 format_lock_cookie(rbd_dev, cookie);
3690 ret = ceph_cls_set_cookie(osdc, &rbd_dev->header_oid,
3691 &rbd_dev->header_oloc, RBD_LOCK_NAME,
3692 CEPH_CLS_LOCK_EXCLUSIVE, rbd_dev->lock_cookie,
3693 RBD_LOCK_TAG, cookie);
3694 if (ret) {
3695 if (ret != -EOPNOTSUPP)
3696 rbd_warn(rbd_dev, "failed to update lock cookie: %d",
3697 ret);
3698
3699 /*
3700 * Lock cookie cannot be updated on older OSDs, so do
3701 * a manual release and queue an acquire.
3702 */
3703 if (rbd_release_lock(rbd_dev))
3704 queue_delayed_work(rbd_dev->task_wq,
3705 &rbd_dev->lock_dwork, 0);
3706 } else {
Florian Margaineedd8ca82017-12-13 16:43:59 +01003707 __rbd_lock(rbd_dev, cookie);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003708 }
3709}
3710
Ilya Dryomov99d16942016-08-12 16:11:41 +02003711static void rbd_reregister_watch(struct work_struct *work)
3712{
3713 struct rbd_device *rbd_dev = container_of(to_delayed_work(work),
3714 struct rbd_device, watch_dwork);
3715 int ret;
3716
3717 dout("%s rbd_dev %p\n", __func__, rbd_dev);
3718
3719 mutex_lock(&rbd_dev->watch_mutex);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003720 if (rbd_dev->watch_state != RBD_WATCH_STATE_ERROR) {
3721 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003722 return;
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003723 }
Ilya Dryomov99d16942016-08-12 16:11:41 +02003724
3725 ret = __rbd_register_watch(rbd_dev);
3726 if (ret) {
3727 rbd_warn(rbd_dev, "failed to reregister watch: %d", ret);
Ilya Dryomov4d736442016-09-29 14:23:12 +02003728 if (ret == -EBLACKLISTED || ret == -ENOENT) {
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003729 set_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003730 wake_requests(rbd_dev, true);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003731 } else {
Ilya Dryomov99d16942016-08-12 16:11:41 +02003732 queue_delayed_work(rbd_dev->task_wq,
3733 &rbd_dev->watch_dwork,
3734 RBD_RETRY_DELAY);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003735 }
3736 mutex_unlock(&rbd_dev->watch_mutex);
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003737 return;
Ilya Dryomov99d16942016-08-12 16:11:41 +02003738 }
3739
3740 rbd_dev->watch_state = RBD_WATCH_STATE_REGISTERED;
3741 rbd_dev->watch_cookie = rbd_dev->watch_handle->linger_id;
3742 mutex_unlock(&rbd_dev->watch_mutex);
3743
Ilya Dryomov14bb2112017-04-13 12:17:38 +02003744 down_write(&rbd_dev->lock_rwsem);
3745 if (rbd_dev->lock_state == RBD_LOCK_STATE_LOCKED)
3746 rbd_reacquire_lock(rbd_dev);
3747 up_write(&rbd_dev->lock_rwsem);
3748
Ilya Dryomov99d16942016-08-12 16:11:41 +02003749 ret = rbd_dev_refresh(rbd_dev);
3750 if (ret)
3751 rbd_warn(rbd_dev, "reregisteration refresh failed: %d", ret);
Ilya Dryomov99d16942016-08-12 16:11:41 +02003752}
3753
Alex Elder36be9a72013-01-19 00:30:28 -06003754/*
Alex Elderf40eb342013-04-25 15:09:42 -05003755 * Synchronous osd object method call. Returns the number of bytes
3756 * returned in the outbound buffer, or a negative error code.
Alex Elder36be9a72013-01-19 00:30:28 -06003757 */
3758static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003759 struct ceph_object_id *oid,
3760 struct ceph_object_locator *oloc,
Alex Elder36be9a72013-01-19 00:30:28 -06003761 const char *method_name,
Alex Elder41579762013-04-21 12:14:45 -05003762 const void *outbound,
Alex Elder36be9a72013-01-19 00:30:28 -06003763 size_t outbound_size,
Alex Elder41579762013-04-21 12:14:45 -05003764 void *inbound,
Alex Eldere2a58ee2013-04-30 00:44:33 -05003765 size_t inbound_size)
Alex Elder36be9a72013-01-19 00:30:28 -06003766{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003767 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
3768 struct page *req_page = NULL;
3769 struct page *reply_page;
Alex Elder36be9a72013-01-19 00:30:28 -06003770 int ret;
3771
3772 /*
Alex Elder6010a452013-04-05 01:27:11 -05003773 * Method calls are ultimately read operations. The result
3774 * should placed into the inbound buffer provided. They
3775 * also supply outbound data--parameters for the object
3776 * method. Currently if this is present it will be a
3777 * snapshot id.
Alex Elder36be9a72013-01-19 00:30:28 -06003778 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003779 if (outbound) {
3780 if (outbound_size > PAGE_SIZE)
3781 return -E2BIG;
Alex Elder36be9a72013-01-19 00:30:28 -06003782
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003783 req_page = alloc_page(GFP_KERNEL);
3784 if (!req_page)
3785 return -ENOMEM;
Alex Elder36be9a72013-01-19 00:30:28 -06003786
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003787 memcpy(page_address(req_page), outbound, outbound_size);
Alex Elder04017e22013-04-05 14:46:02 -05003788 }
Alex Elder430c28c2013-04-03 21:32:51 -05003789
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003790 reply_page = alloc_page(GFP_KERNEL);
3791 if (!reply_page) {
3792 if (req_page)
3793 __free_page(req_page);
3794 return -ENOMEM;
3795 }
Alex Elder36be9a72013-01-19 00:30:28 -06003796
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003797 ret = ceph_osdc_call(osdc, oid, oloc, RBD_DRV_NAME, method_name,
3798 CEPH_OSD_FLAG_READ, req_page, outbound_size,
3799 reply_page, &inbound_size);
3800 if (!ret) {
3801 memcpy(inbound, page_address(reply_page), inbound_size);
3802 ret = inbound_size;
3803 }
Alex Elder57385b52013-04-21 12:14:45 -05003804
Ilya Dryomovecd4a682017-01-25 18:16:21 +01003805 if (req_page)
3806 __free_page(req_page);
3807 __free_page(reply_page);
Alex Elder36be9a72013-01-19 00:30:28 -06003808 return ret;
3809}
3810
Ilya Dryomoved95b212016-08-12 16:40:02 +02003811/*
3812 * lock_rwsem must be held for read
3813 */
3814static void rbd_wait_state_locked(struct rbd_device *rbd_dev)
3815{
3816 DEFINE_WAIT(wait);
3817
3818 do {
3819 /*
3820 * Note the use of mod_delayed_work() in rbd_acquire_lock()
3821 * and cancel_delayed_work() in wake_requests().
3822 */
3823 dout("%s rbd_dev %p queueing lock_dwork\n", __func__, rbd_dev);
3824 queue_delayed_work(rbd_dev->task_wq, &rbd_dev->lock_dwork, 0);
3825 prepare_to_wait_exclusive(&rbd_dev->lock_waitq, &wait,
3826 TASK_UNINTERRUPTIBLE);
3827 up_read(&rbd_dev->lock_rwsem);
3828 schedule();
3829 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003830 } while (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
3831 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags));
3832
Ilya Dryomoved95b212016-08-12 16:40:02 +02003833 finish_wait(&rbd_dev->lock_waitq, &wait);
3834}
3835
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003836static void rbd_queue_workfn(struct work_struct *work)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003837{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003838 struct request *rq = blk_mq_rq_from_pdu(work);
3839 struct rbd_device *rbd_dev = rq->q->queuedata;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003840 struct rbd_img_request *img_request;
Josh Durgin4e752f02014-04-08 11:12:11 -07003841 struct ceph_snap_context *snapc = NULL;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003842 u64 offset = (u64)blk_rq_pos(rq) << SECTOR_SHIFT;
3843 u64 length = blk_rq_bytes(rq);
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003844 enum obj_operation_type op_type;
Josh Durgin4e752f02014-04-08 11:12:11 -07003845 u64 mapping_size;
Ilya Dryomov80de1912016-09-20 14:23:17 +02003846 bool must_be_locked;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003847 int result;
3848
Christoph Hellwigaebf5262017-01-31 16:57:31 +01003849 switch (req_op(rq)) {
3850 case REQ_OP_DISCARD:
Ilya Dryomov6ac56952017-05-22 19:59:24 +02003851 case REQ_OP_WRITE_ZEROES:
Christoph Hellwigaebf5262017-01-31 16:57:31 +01003852 op_type = OBJ_OP_DISCARD;
3853 break;
3854 case REQ_OP_WRITE:
3855 op_type = OBJ_OP_WRITE;
3856 break;
3857 case REQ_OP_READ:
3858 op_type = OBJ_OP_READ;
3859 break;
3860 default:
3861 dout("%s: non-fs request type %d\n", __func__, req_op(rq));
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003862 result = -EIO;
3863 goto err;
3864 }
3865
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003866 /* Ignore/skip any zero-length requests */
3867
3868 if (!length) {
3869 dout("%s: zero-length request\n", __func__);
3870 result = 0;
3871 goto err_rq;
3872 }
3873
Ilya Dryomov9568c932017-10-12 12:35:19 +02003874 rbd_assert(op_type == OBJ_OP_READ ||
3875 rbd_dev->spec->snap_id == CEPH_NOSNAP);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003876
3877 /*
3878 * Quit early if the mapped snapshot no longer exists. It's
3879 * still possible the snapshot will have disappeared by the
3880 * time our request arrives at the osd, but there's no sense in
3881 * sending it if we already know.
3882 */
3883 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
3884 dout("request for non-existent snapshot");
3885 rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
3886 result = -ENXIO;
3887 goto err_rq;
3888 }
3889
3890 if (offset && length > U64_MAX - offset + 1) {
3891 rbd_warn(rbd_dev, "bad request range (%llu~%llu)", offset,
3892 length);
3893 result = -EINVAL;
3894 goto err_rq; /* Shouldn't happen */
3895 }
3896
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003897 blk_mq_start_request(rq);
3898
Josh Durgin4e752f02014-04-08 11:12:11 -07003899 down_read(&rbd_dev->header_rwsem);
3900 mapping_size = rbd_dev->mapping.size;
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003901 if (op_type != OBJ_OP_READ) {
Josh Durgin4e752f02014-04-08 11:12:11 -07003902 snapc = rbd_dev->header.snapc;
3903 ceph_get_snap_context(snapc);
3904 }
3905 up_read(&rbd_dev->header_rwsem);
3906
3907 if (offset + length > mapping_size) {
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003908 rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)", offset,
Josh Durgin4e752f02014-04-08 11:12:11 -07003909 length, mapping_size);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003910 result = -EIO;
3911 goto err_rq;
3912 }
3913
Ilya Dryomovf9bebd52017-04-13 12:17:39 +02003914 must_be_locked =
3915 (rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK) &&
3916 (op_type != OBJ_OP_READ || rbd_dev->opts->lock_on_read);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003917 if (must_be_locked) {
3918 down_read(&rbd_dev->lock_rwsem);
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003919 if (rbd_dev->lock_state != RBD_LOCK_STATE_LOCKED &&
Ilya Dryomove010dd02017-04-13 12:17:39 +02003920 !test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
3921 if (rbd_dev->opts->exclusive) {
3922 rbd_warn(rbd_dev, "exclusive lock required");
3923 result = -EROFS;
3924 goto err_unlock;
3925 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003926 rbd_wait_state_locked(rbd_dev);
Ilya Dryomove010dd02017-04-13 12:17:39 +02003927 }
Ilya Dryomov87c0fde2016-09-29 13:41:05 +02003928 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
3929 result = -EBLACKLISTED;
3930 goto err_unlock;
3931 }
Ilya Dryomoved95b212016-08-12 16:40:02 +02003932 }
3933
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003934 img_request = rbd_img_request_create(rbd_dev, offset, length, op_type,
Josh Durgin4e752f02014-04-08 11:12:11 -07003935 snapc);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003936 if (!img_request) {
3937 result = -ENOMEM;
Ilya Dryomoved95b212016-08-12 16:40:02 +02003938 goto err_unlock;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003939 }
3940 img_request->rq = rq;
Ilya Dryomov70b16db2015-11-27 19:23:24 +01003941 snapc = NULL; /* img_request consumes a ref */
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003942
Guangliang Zhao90e98c52014-04-01 22:22:16 +08003943 if (op_type == OBJ_OP_DISCARD)
3944 result = rbd_img_request_fill(img_request, OBJ_REQUEST_NODATA,
3945 NULL);
Ilya Dryomov5359a172018-01-20 10:30:10 +01003946 else {
3947 struct ceph_bio_iter bio_it = { .bio = rq->bio,
3948 .iter = rq->bio->bi_iter };
3949
Guangliang Zhao90e98c52014-04-01 22:22:16 +08003950 result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
Ilya Dryomov5359a172018-01-20 10:30:10 +01003951 &bio_it);
3952 }
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003953 if (result)
3954 goto err_img_request;
3955
3956 result = rbd_img_request_submit(img_request);
3957 if (result)
3958 goto err_img_request;
3959
Ilya Dryomoved95b212016-08-12 16:40:02 +02003960 if (must_be_locked)
3961 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003962 return;
3963
3964err_img_request:
3965 rbd_img_request_put(img_request);
Ilya Dryomoved95b212016-08-12 16:40:02 +02003966err_unlock:
3967 if (must_be_locked)
3968 up_read(&rbd_dev->lock_rwsem);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003969err_rq:
3970 if (result)
3971 rbd_warn(rbd_dev, "%s %llx at %llx result %d",
Guangliang Zhao6d2940c2014-03-13 11:21:35 +08003972 obj_op_name(op_type), length, offset, result);
SF Markus Elfringe96a6502014-11-02 15:20:59 +01003973 ceph_put_snap_context(snapc);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003974err:
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003975 blk_mq_end_request(rq, errno_to_blk_status(result));
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003976}
3977
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003978static blk_status_t rbd_queue_rq(struct blk_mq_hw_ctx *hctx,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003979 const struct blk_mq_queue_data *bd)
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003980{
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003981 struct request *rq = bd->rq;
3982 struct work_struct *work = blk_mq_rq_to_pdu(rq);
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04003983
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01003984 queue_work(rbd_wq, work);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003985 return BLK_STS_OK;
Alex Elderbf0d5f502012-11-22 00:00:08 -06003986}
3987
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003988static void rbd_free_disk(struct rbd_device *rbd_dev)
3989{
Ilya Dryomov5769ed02017-04-13 12:17:38 +02003990 blk_cleanup_queue(rbd_dev->disk->queue);
3991 blk_mq_free_tag_set(&rbd_dev->tag_set);
3992 put_disk(rbd_dev->disk);
Alex Eldera0cab922013-04-25 23:15:08 -05003993 rbd_dev->disk = NULL;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07003994}
3995
Alex Elder788e2df2013-01-17 12:25:27 -06003996static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01003997 struct ceph_object_id *oid,
3998 struct ceph_object_locator *oloc,
3999 void *buf, int buf_len)
Alex Elder788e2df2013-01-17 12:25:27 -06004000
4001{
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004002 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
4003 struct ceph_osd_request *req;
4004 struct page **pages;
4005 int num_pages = calc_pages_for(0, buf_len);
Alex Elder788e2df2013-01-17 12:25:27 -06004006 int ret;
4007
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004008 req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_KERNEL);
4009 if (!req)
4010 return -ENOMEM;
Alex Elder788e2df2013-01-17 12:25:27 -06004011
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004012 ceph_oid_copy(&req->r_base_oid, oid);
4013 ceph_oloc_copy(&req->r_base_oloc, oloc);
4014 req->r_flags = CEPH_OSD_FLAG_READ;
Alex Elder788e2df2013-01-17 12:25:27 -06004015
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004016 ret = ceph_osdc_alloc_messages(req, GFP_KERNEL);
Alex Elder788e2df2013-01-17 12:25:27 -06004017 if (ret)
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004018 goto out_req;
Alex Elder788e2df2013-01-17 12:25:27 -06004019
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004020 pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
4021 if (IS_ERR(pages)) {
4022 ret = PTR_ERR(pages);
4023 goto out_req;
4024 }
Alex Elder1ceae7e2013-02-06 13:11:38 -06004025
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004026 osd_req_op_extent_init(req, 0, CEPH_OSD_OP_READ, 0, buf_len, 0, 0);
4027 osd_req_op_extent_osd_data_pages(req, 0, pages, buf_len, 0, false,
4028 true);
Alex Elder788e2df2013-01-17 12:25:27 -06004029
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004030 ceph_osdc_start_request(osdc, req, false);
4031 ret = ceph_osdc_wait_request(osdc, req);
4032 if (ret >= 0)
4033 ceph_copy_from_page_vector(pages, buf, 0, ret);
4034
4035out_req:
4036 ceph_osdc_put_request(req);
Alex Elder788e2df2013-01-17 12:25:27 -06004037 return ret;
4038}
4039
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004040/*
Alex Elder662518b2013-05-06 09:51:29 -05004041 * Read the complete header for the given rbd device. On successful
4042 * return, the rbd_dev->header field will contain up-to-date
4043 * information about the image.
Alex Elder4156d992012-08-02 11:29:46 -05004044 */
Alex Elder99a41eb2013-05-06 09:51:30 -05004045static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
Alex Elder4156d992012-08-02 11:29:46 -05004046{
4047 struct rbd_image_header_ondisk *ondisk = NULL;
4048 u32 snap_count = 0;
4049 u64 names_size = 0;
4050 u32 want_count;
4051 int ret;
4052
4053 /*
4054 * The complete header will include an array of its 64-bit
4055 * snapshot ids, followed by the names of those snapshots as
4056 * a contiguous block of NUL-terminated strings. Note that
4057 * the number of snapshots could change by the time we read
4058 * it in, in which case we re-read it.
4059 */
4060 do {
4061 size_t size;
4062
4063 kfree(ondisk);
4064
4065 size = sizeof (*ondisk);
4066 size += snap_count * sizeof (struct rbd_image_snap_ondisk);
4067 size += names_size;
4068 ondisk = kmalloc(size, GFP_KERNEL);
4069 if (!ondisk)
Alex Elder662518b2013-05-06 09:51:29 -05004070 return -ENOMEM;
Alex Elder4156d992012-08-02 11:29:46 -05004071
Ilya Dryomovfe5478e2017-01-25 18:16:21 +01004072 ret = rbd_obj_read_sync(rbd_dev, &rbd_dev->header_oid,
4073 &rbd_dev->header_oloc, ondisk, size);
Alex Elder4156d992012-08-02 11:29:46 -05004074 if (ret < 0)
Alex Elder662518b2013-05-06 09:51:29 -05004075 goto out;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004076 if ((size_t)ret < size) {
Alex Elder4156d992012-08-02 11:29:46 -05004077 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004078 rbd_warn(rbd_dev, "short header read (want %zd got %d)",
4079 size, ret);
Alex Elder662518b2013-05-06 09:51:29 -05004080 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004081 }
4082 if (!rbd_dev_ondisk_valid(ondisk)) {
4083 ret = -ENXIO;
Alex Elder06ecc6c2012-11-01 10:17:15 -05004084 rbd_warn(rbd_dev, "invalid header");
Alex Elder662518b2013-05-06 09:51:29 -05004085 goto out;
Alex Elder4156d992012-08-02 11:29:46 -05004086 }
4087
4088 names_size = le64_to_cpu(ondisk->snap_names_len);
4089 want_count = snap_count;
4090 snap_count = le32_to_cpu(ondisk->snap_count);
4091 } while (snap_count != want_count);
4092
Alex Elder662518b2013-05-06 09:51:29 -05004093 ret = rbd_header_from_disk(rbd_dev, ondisk);
4094out:
Alex Elder4156d992012-08-02 11:29:46 -05004095 kfree(ondisk);
4096
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004097 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004098}
4099
Alex Elder15228ed2013-05-01 12:43:03 -05004100/*
4101 * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
4102 * has disappeared from the (just updated) snapshot context.
4103 */
4104static void rbd_exists_validate(struct rbd_device *rbd_dev)
4105{
4106 u64 snap_id;
4107
4108 if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
4109 return;
4110
4111 snap_id = rbd_dev->spec->snap_id;
4112 if (snap_id == CEPH_NOSNAP)
4113 return;
4114
4115 if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
4116 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
4117}
4118
Josh Durgin98752012013-08-29 17:26:31 -07004119static void rbd_dev_update_size(struct rbd_device *rbd_dev)
4120{
4121 sector_t size;
Josh Durgin98752012013-08-29 17:26:31 -07004122
4123 /*
Ilya Dryomov811c6682016-04-15 16:22:16 +02004124 * If EXISTS is not set, rbd_dev->disk may be NULL, so don't
4125 * try to update its size. If REMOVING is set, updating size
4126 * is just useless work since the device can't be opened.
Josh Durgin98752012013-08-29 17:26:31 -07004127 */
Ilya Dryomov811c6682016-04-15 16:22:16 +02004128 if (test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags) &&
4129 !test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags)) {
Josh Durgin98752012013-08-29 17:26:31 -07004130 size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
4131 dout("setting size to %llu sectors", (unsigned long long)size);
4132 set_capacity(rbd_dev->disk, size);
4133 revalidate_disk(rbd_dev->disk);
4134 }
4135}
4136
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004137static int rbd_dev_refresh(struct rbd_device *rbd_dev)
Alex Elder1fe5e992012-07-25 09:32:41 -05004138{
Alex Eldere627db02013-05-06 07:40:30 -05004139 u64 mapping_size;
Alex Elder1fe5e992012-07-25 09:32:41 -05004140 int ret;
4141
Alex Eldercfbf6372013-05-31 17:40:45 -05004142 down_write(&rbd_dev->header_rwsem);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004143 mapping_size = rbd_dev->mapping.size;
Ilya Dryomova720ae02014-07-23 17:11:19 +04004144
4145 ret = rbd_dev_header_info(rbd_dev);
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004146 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004147 goto out;
Alex Elder15228ed2013-05-01 12:43:03 -05004148
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004149 /*
4150 * If there is a parent, see if it has disappeared due to the
4151 * mapped image getting flattened.
4152 */
4153 if (rbd_dev->parent) {
4154 ret = rbd_dev_v2_parent_info(rbd_dev);
4155 if (ret)
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004156 goto out;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04004157 }
4158
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004159 if (rbd_dev->spec->snap_id == CEPH_NOSNAP) {
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004160 rbd_dev->mapping.size = rbd_dev->header.image_size;
Ilya Dryomov5ff11082014-07-23 17:11:21 +04004161 } else {
4162 /* validate mapped snapshot's EXISTS flag */
4163 rbd_exists_validate(rbd_dev);
4164 }
Alex Elder15228ed2013-05-01 12:43:03 -05004165
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004166out:
Alex Eldercfbf6372013-05-31 17:40:45 -05004167 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004168 if (!ret && mapping_size != rbd_dev->mapping.size)
Josh Durgin98752012013-08-29 17:26:31 -07004169 rbd_dev_update_size(rbd_dev);
Alex Elder1fe5e992012-07-25 09:32:41 -05004170
Ilya Dryomov73e39e42015-01-08 20:18:22 +03004171 return ret;
Alex Elder1fe5e992012-07-25 09:32:41 -05004172}
4173
Christoph Hellwigd6296d392017-05-01 10:19:08 -06004174static int rbd_init_request(struct blk_mq_tag_set *set, struct request *rq,
4175 unsigned int hctx_idx, unsigned int numa_node)
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004176{
4177 struct work_struct *work = blk_mq_rq_to_pdu(rq);
4178
4179 INIT_WORK(work, rbd_queue_workfn);
4180 return 0;
4181}
4182
Eric Biggersf363b082017-03-30 13:39:16 -07004183static const struct blk_mq_ops rbd_mq_ops = {
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004184 .queue_rq = rbd_queue_rq,
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004185 .init_request = rbd_init_request,
4186};
4187
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004188static int rbd_init_disk(struct rbd_device *rbd_dev)
4189{
4190 struct gendisk *disk;
4191 struct request_queue *q;
Alex Elder593a9e72012-02-07 12:03:37 -06004192 u64 segment_size;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004193 int err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004194
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004195 /* create gendisk info */
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004196 disk = alloc_disk(single_major ?
4197 (1 << RBD_SINGLE_MAJOR_PART_SHIFT) :
4198 RBD_MINORS_PER_MAJOR);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004199 if (!disk)
Alex Elder1fcdb8a2012-08-29 17:11:06 -05004200 return -ENOMEM;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004201
Alex Elderf0f8cef2012-01-29 13:57:44 -06004202 snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
Alex Elderde71a292012-07-03 16:01:19 -05004203 rbd_dev->dev_id);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004204 disk->major = rbd_dev->major;
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004205 disk->first_minor = rbd_dev->minor;
Ilya Dryomov7e513d42013-12-16 19:26:32 +02004206 if (single_major)
4207 disk->flags |= GENHD_FL_EXT_DEVT;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004208 disk->fops = &rbd_bd_ops;
4209 disk->private_data = rbd_dev;
4210
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004211 memset(&rbd_dev->tag_set, 0, sizeof(rbd_dev->tag_set));
4212 rbd_dev->tag_set.ops = &rbd_mq_ops;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004213 rbd_dev->tag_set.queue_depth = rbd_dev->opts->queue_depth;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004214 rbd_dev->tag_set.numa_node = NUMA_NO_NODE;
Ilya Dryomovb5584182015-06-23 16:21:19 +03004215 rbd_dev->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004216 rbd_dev->tag_set.nr_hw_queues = 1;
4217 rbd_dev->tag_set.cmd_size = sizeof(struct work_struct);
4218
4219 err = blk_mq_alloc_tag_set(&rbd_dev->tag_set);
4220 if (err)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004221 goto out_disk;
Josh Durgin029bcbd2011-07-22 11:35:23 -07004222
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004223 q = blk_mq_init_queue(&rbd_dev->tag_set);
4224 if (IS_ERR(q)) {
4225 err = PTR_ERR(q);
4226 goto out_tag_set;
4227 }
4228
Ilya Dryomovd8a2c892015-03-24 16:15:17 +03004229 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
4230 /* QUEUE_FLAG_ADD_RANDOM is off by default for blk-mq */
Alex Elder593a9e72012-02-07 12:03:37 -06004231
Josh Durgin029bcbd2011-07-22 11:35:23 -07004232 /* set io sizes to object size */
Alex Elder593a9e72012-02-07 12:03:37 -06004233 segment_size = rbd_obj_bytes(&rbd_dev->header);
4234 blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov0d9fde42015-10-07 16:09:35 +02004235 q->limits.max_sectors = queue_max_hw_sectors(q);
Ilya Dryomov21acdf42017-12-21 15:35:11 +01004236 blk_queue_max_segments(q, USHRT_MAX);
Ilya Dryomov24f1df62018-01-12 17:22:10 +01004237 blk_queue_max_segment_size(q, UINT_MAX);
Alex Elder593a9e72012-02-07 12:03:37 -06004238 blk_queue_io_min(q, segment_size);
4239 blk_queue_io_opt(q, segment_size);
Josh Durgin029bcbd2011-07-22 11:35:23 -07004240
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004241 /* enable the discard support */
4242 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
4243 q->limits.discard_granularity = segment_size;
Jens Axboe2bb4cd52015-07-14 08:15:12 -06004244 blk_queue_max_discard_sectors(q, segment_size / SECTOR_SIZE);
Ilya Dryomov6ac56952017-05-22 19:59:24 +02004245 blk_queue_max_write_zeroes_sectors(q, segment_size / SECTOR_SIZE);
Guangliang Zhao90e98c52014-04-01 22:22:16 +08004246
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004247 if (!ceph_test_opt(rbd_dev->rbd_client->client, NOCRC))
Jan Karadc3b17c2017-02-02 15:56:50 +01004248 q->backing_dev_info->capabilities |= BDI_CAP_STABLE_WRITES;
Ronny Hegewaldbae818e2015-10-15 18:50:46 +00004249
Ilya Dryomov5769ed02017-04-13 12:17:38 +02004250 /*
4251 * disk_release() expects a queue ref from add_disk() and will
4252 * put it. Hold an extra ref until add_disk() is called.
4253 */
4254 WARN_ON(!blk_get_queue(q));
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004255 disk->queue = q;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004256 q->queuedata = rbd_dev;
4257
4258 rbd_dev->disk = disk;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004259
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004260 return 0;
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004261out_tag_set:
4262 blk_mq_free_tag_set(&rbd_dev->tag_set);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004263out_disk:
4264 put_disk(disk);
Christoph Hellwig7ad18af2015-01-13 17:20:04 +01004265 return err;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004266}
4267
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004268/*
4269 sysfs
4270*/
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004271
Alex Elder593a9e72012-02-07 12:03:37 -06004272static struct rbd_device *dev_to_rbd_dev(struct device *dev)
4273{
4274 return container_of(dev, struct rbd_device, dev);
4275}
4276
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004277static ssize_t rbd_size_show(struct device *dev,
4278 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004279{
Alex Elder593a9e72012-02-07 12:03:37 -06004280 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004281
Alex Elderfc71d832013-04-26 15:44:36 -05004282 return sprintf(buf, "%llu\n",
4283 (unsigned long long)rbd_dev->mapping.size);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004284}
4285
Alex Elder34b13182012-07-13 20:35:12 -05004286/*
4287 * Note this shows the features for whatever's mapped, which is not
4288 * necessarily the base image.
4289 */
4290static ssize_t rbd_features_show(struct device *dev,
4291 struct device_attribute *attr, char *buf)
4292{
4293 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4294
4295 return sprintf(buf, "0x%016llx\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004296 (unsigned long long)rbd_dev->mapping.features);
Alex Elder34b13182012-07-13 20:35:12 -05004297}
4298
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004299static ssize_t rbd_major_show(struct device *dev,
4300 struct device_attribute *attr, char *buf)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004301{
Alex Elder593a9e72012-02-07 12:03:37 -06004302 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004303
Alex Elderfc71d832013-04-26 15:44:36 -05004304 if (rbd_dev->major)
4305 return sprintf(buf, "%d\n", rbd_dev->major);
4306
4307 return sprintf(buf, "(none)\n");
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004308}
Alex Elderfc71d832013-04-26 15:44:36 -05004309
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004310static ssize_t rbd_minor_show(struct device *dev,
4311 struct device_attribute *attr, char *buf)
4312{
4313 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4314
4315 return sprintf(buf, "%d\n", rbd_dev->minor);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004316}
4317
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004318static ssize_t rbd_client_addr_show(struct device *dev,
4319 struct device_attribute *attr, char *buf)
4320{
4321 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4322 struct ceph_entity_addr *client_addr =
4323 ceph_client_addr(rbd_dev->rbd_client->client);
4324
4325 return sprintf(buf, "%pISpc/%u\n", &client_addr->in_addr,
4326 le32_to_cpu(client_addr->nonce));
4327}
4328
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004329static ssize_t rbd_client_id_show(struct device *dev,
4330 struct device_attribute *attr, char *buf)
4331{
Alex Elder593a9e72012-02-07 12:03:37 -06004332 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004333
Alex Elder1dbb4392012-01-24 10:08:37 -06004334 return sprintf(buf, "client%lld\n",
Ilya Dryomov033268a2016-08-12 14:59:58 +02004335 ceph_client_gid(rbd_dev->rbd_client->client));
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004336}
4337
Mike Christie267fb902016-08-18 18:38:43 +02004338static ssize_t rbd_cluster_fsid_show(struct device *dev,
4339 struct device_attribute *attr, char *buf)
4340{
4341 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4342
4343 return sprintf(buf, "%pU\n", &rbd_dev->rbd_client->client->fsid);
4344}
4345
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004346static ssize_t rbd_config_info_show(struct device *dev,
4347 struct device_attribute *attr, char *buf)
4348{
4349 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4350
4351 return sprintf(buf, "%s\n", rbd_dev->config_info);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004352}
4353
4354static ssize_t rbd_pool_show(struct device *dev,
4355 struct device_attribute *attr, char *buf)
4356{
Alex Elder593a9e72012-02-07 12:03:37 -06004357 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004358
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004359 return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004360}
4361
Alex Elder9bb2f332012-07-12 10:46:35 -05004362static ssize_t rbd_pool_id_show(struct device *dev,
4363 struct device_attribute *attr, char *buf)
4364{
4365 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4366
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004367 return sprintf(buf, "%llu\n",
Alex Elderfc71d832013-04-26 15:44:36 -05004368 (unsigned long long) rbd_dev->spec->pool_id);
Alex Elder9bb2f332012-07-12 10:46:35 -05004369}
4370
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004371static ssize_t rbd_name_show(struct device *dev,
4372 struct device_attribute *attr, char *buf)
4373{
Alex Elder593a9e72012-02-07 12:03:37 -06004374 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004375
Alex Eldera92ffdf2012-10-30 19:40:33 -05004376 if (rbd_dev->spec->image_name)
4377 return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
4378
4379 return sprintf(buf, "(unknown)\n");
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004380}
4381
Alex Elder589d30e2012-07-10 20:30:11 -05004382static ssize_t rbd_image_id_show(struct device *dev,
4383 struct device_attribute *attr, char *buf)
4384{
4385 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4386
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004387 return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05004388}
4389
Alex Elder34b13182012-07-13 20:35:12 -05004390/*
4391 * Shows the name of the currently-mapped snapshot (or
4392 * RBD_SNAP_HEAD_NAME for the base image).
4393 */
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004394static ssize_t rbd_snap_show(struct device *dev,
4395 struct device_attribute *attr,
4396 char *buf)
4397{
Alex Elder593a9e72012-02-07 12:03:37 -06004398 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004399
Alex Elder0d7dbfc2012-10-25 23:34:41 -05004400 return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004401}
4402
Mike Christie92a58672016-08-18 18:38:44 +02004403static ssize_t rbd_snap_id_show(struct device *dev,
4404 struct device_attribute *attr, char *buf)
4405{
4406 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4407
4408 return sprintf(buf, "%llu\n", rbd_dev->spec->snap_id);
4409}
4410
Alex Elder86b00e02012-10-25 23:34:42 -05004411/*
Ilya Dryomovff961282014-07-22 21:53:07 +04004412 * For a v2 image, shows the chain of parent images, separated by empty
4413 * lines. For v1 images or if there is no parent, shows "(no parent
4414 * image)".
Alex Elder86b00e02012-10-25 23:34:42 -05004415 */
4416static ssize_t rbd_parent_show(struct device *dev,
Ilya Dryomovff961282014-07-22 21:53:07 +04004417 struct device_attribute *attr,
4418 char *buf)
Alex Elder86b00e02012-10-25 23:34:42 -05004419{
4420 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Ilya Dryomovff961282014-07-22 21:53:07 +04004421 ssize_t count = 0;
Alex Elder86b00e02012-10-25 23:34:42 -05004422
Ilya Dryomovff961282014-07-22 21:53:07 +04004423 if (!rbd_dev->parent)
Alex Elder86b00e02012-10-25 23:34:42 -05004424 return sprintf(buf, "(no parent image)\n");
4425
Ilya Dryomovff961282014-07-22 21:53:07 +04004426 for ( ; rbd_dev->parent; rbd_dev = rbd_dev->parent) {
4427 struct rbd_spec *spec = rbd_dev->parent_spec;
Alex Elder86b00e02012-10-25 23:34:42 -05004428
Ilya Dryomovff961282014-07-22 21:53:07 +04004429 count += sprintf(&buf[count], "%s"
4430 "pool_id %llu\npool_name %s\n"
4431 "image_id %s\nimage_name %s\n"
4432 "snap_id %llu\nsnap_name %s\n"
4433 "overlap %llu\n",
4434 !count ? "" : "\n", /* first? */
4435 spec->pool_id, spec->pool_name,
4436 spec->image_id, spec->image_name ?: "(unknown)",
4437 spec->snap_id, spec->snap_name,
4438 rbd_dev->parent_overlap);
4439 }
Alex Elder86b00e02012-10-25 23:34:42 -05004440
Ilya Dryomovff961282014-07-22 21:53:07 +04004441 return count;
Alex Elder86b00e02012-10-25 23:34:42 -05004442}
4443
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004444static ssize_t rbd_image_refresh(struct device *dev,
4445 struct device_attribute *attr,
4446 const char *buf,
4447 size_t size)
4448{
Alex Elder593a9e72012-02-07 12:03:37 -06004449 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
Alex Elderb8136232012-07-25 09:32:41 -05004450 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004451
Alex Eldercc4a38bd2013-04-30 00:44:33 -05004452 ret = rbd_dev_refresh(rbd_dev);
Alex Eldere627db02013-05-06 07:40:30 -05004453 if (ret)
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004454 return ret;
Alex Elderb8136232012-07-25 09:32:41 -05004455
Ilya Dryomov52bb1f92014-07-23 17:11:20 +04004456 return size;
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004457}
Yehuda Sadeh602adf42010-08-12 16:11:25 -07004458
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004459static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
Alex Elder34b13182012-07-13 20:35:12 -05004460static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004461static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004462static DEVICE_ATTR(minor, S_IRUGO, rbd_minor_show, NULL);
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004463static DEVICE_ATTR(client_addr, S_IRUGO, rbd_client_addr_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004464static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
Mike Christie267fb902016-08-18 18:38:43 +02004465static DEVICE_ATTR(cluster_fsid, S_IRUGO, rbd_cluster_fsid_show, NULL);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004466static DEVICE_ATTR(config_info, S_IRUSR, rbd_config_info_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004467static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
Alex Elder9bb2f332012-07-12 10:46:35 -05004468static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004469static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
Alex Elder589d30e2012-07-10 20:30:11 -05004470static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004471static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
4472static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
Mike Christie92a58672016-08-18 18:38:44 +02004473static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
Alex Elder86b00e02012-10-25 23:34:42 -05004474static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004475
4476static struct attribute *rbd_attrs[] = {
4477 &dev_attr_size.attr,
Alex Elder34b13182012-07-13 20:35:12 -05004478 &dev_attr_features.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004479 &dev_attr_major.attr,
Ilya Dryomovdd82fff2013-12-13 15:28:57 +02004480 &dev_attr_minor.attr,
Ilya Dryomov005a07bf2016-08-18 18:38:43 +02004481 &dev_attr_client_addr.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004482 &dev_attr_client_id.attr,
Mike Christie267fb902016-08-18 18:38:43 +02004483 &dev_attr_cluster_fsid.attr,
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004484 &dev_attr_config_info.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004485 &dev_attr_pool.attr,
Alex Elder9bb2f332012-07-12 10:46:35 -05004486 &dev_attr_pool_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004487 &dev_attr_name.attr,
Alex Elder589d30e2012-07-10 20:30:11 -05004488 &dev_attr_image_id.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004489 &dev_attr_current_snap.attr,
Mike Christie92a58672016-08-18 18:38:44 +02004490 &dev_attr_snap_id.attr,
Alex Elder86b00e02012-10-25 23:34:42 -05004491 &dev_attr_parent.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004492 &dev_attr_refresh.attr,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004493 NULL
4494};
4495
4496static struct attribute_group rbd_attr_group = {
4497 .attrs = rbd_attrs,
4498};
4499
4500static const struct attribute_group *rbd_attr_groups[] = {
4501 &rbd_attr_group,
4502 NULL
4503};
4504
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004505static void rbd_dev_release(struct device *dev);
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004506
Bhumika Goyalb9942bc2017-02-11 12:14:38 +05304507static const struct device_type rbd_device_type = {
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004508 .name = "rbd",
4509 .groups = rbd_attr_groups,
Ilya Dryomov6cac4692015-10-16 20:11:25 +02004510 .release = rbd_dev_release,
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004511};
4512
Alex Elder8b8fb992012-10-26 17:25:24 -05004513static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
4514{
4515 kref_get(&spec->kref);
4516
4517 return spec;
4518}
4519
4520static void rbd_spec_free(struct kref *kref);
4521static void rbd_spec_put(struct rbd_spec *spec)
4522{
4523 if (spec)
4524 kref_put(&spec->kref, rbd_spec_free);
4525}
4526
4527static struct rbd_spec *rbd_spec_alloc(void)
4528{
4529 struct rbd_spec *spec;
4530
4531 spec = kzalloc(sizeof (*spec), GFP_KERNEL);
4532 if (!spec)
4533 return NULL;
Ilya Dryomov04077592014-07-23 17:11:20 +04004534
4535 spec->pool_id = CEPH_NOPOOL;
4536 spec->snap_id = CEPH_NOSNAP;
Alex Elder8b8fb992012-10-26 17:25:24 -05004537 kref_init(&spec->kref);
4538
Alex Elder8b8fb992012-10-26 17:25:24 -05004539 return spec;
4540}
4541
4542static void rbd_spec_free(struct kref *kref)
4543{
4544 struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
4545
4546 kfree(spec->pool_name);
4547 kfree(spec->image_id);
4548 kfree(spec->image_name);
4549 kfree(spec->snap_name);
4550 kfree(spec);
4551}
4552
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004553static void rbd_dev_free(struct rbd_device *rbd_dev)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004554{
Ilya Dryomov99d16942016-08-12 16:11:41 +02004555 WARN_ON(rbd_dev->watch_state != RBD_WATCH_STATE_UNREGISTERED);
Ilya Dryomoved95b212016-08-12 16:40:02 +02004556 WARN_ON(rbd_dev->lock_state != RBD_LOCK_STATE_UNLOCKED);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004557
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004558 ceph_oid_destroy(&rbd_dev->header_oid);
Ilya Dryomov6b6dddb2016-08-05 16:15:38 +02004559 ceph_oloc_destroy(&rbd_dev->header_oloc);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02004560 kfree(rbd_dev->config_info);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004561
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004562 rbd_put_client(rbd_dev->rbd_client);
4563 rbd_spec_put(rbd_dev->spec);
4564 kfree(rbd_dev->opts);
4565 kfree(rbd_dev);
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004566}
4567
4568static void rbd_dev_release(struct device *dev)
4569{
4570 struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
4571 bool need_put = !!rbd_dev->opts;
4572
4573 if (need_put) {
4574 destroy_workqueue(rbd_dev->task_wq);
4575 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4576 }
4577
4578 rbd_dev_free(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004579
4580 /*
4581 * This is racy, but way better than putting module outside of
4582 * the release callback. The race window is pretty small, so
4583 * doing something similar to dm (dm-builtin.c) is overkill.
4584 */
4585 if (need_put)
4586 module_put(THIS_MODULE);
4587}
4588
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004589static struct rbd_device *__rbd_dev_create(struct rbd_client *rbdc,
4590 struct rbd_spec *spec)
Alex Elderc53d5892012-10-25 23:34:42 -05004591{
4592 struct rbd_device *rbd_dev;
4593
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004594 rbd_dev = kzalloc(sizeof(*rbd_dev), GFP_KERNEL);
Alex Elderc53d5892012-10-25 23:34:42 -05004595 if (!rbd_dev)
4596 return NULL;
4597
4598 spin_lock_init(&rbd_dev->lock);
4599 INIT_LIST_HEAD(&rbd_dev->node);
Alex Elderc53d5892012-10-25 23:34:42 -05004600 init_rwsem(&rbd_dev->header_rwsem);
4601
Ilya Dryomov7e973322017-01-25 18:16:22 +01004602 rbd_dev->header.data_pool_id = CEPH_NOPOOL;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004603 ceph_oid_init(&rbd_dev->header_oid);
Ilya Dryomov431a02c2017-01-25 18:16:21 +01004604 rbd_dev->header_oloc.pool = spec->pool_id;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02004605
Ilya Dryomov99d16942016-08-12 16:11:41 +02004606 mutex_init(&rbd_dev->watch_mutex);
4607 rbd_dev->watch_state = RBD_WATCH_STATE_UNREGISTERED;
4608 INIT_DELAYED_WORK(&rbd_dev->watch_dwork, rbd_reregister_watch);
4609
Ilya Dryomoved95b212016-08-12 16:40:02 +02004610 init_rwsem(&rbd_dev->lock_rwsem);
4611 rbd_dev->lock_state = RBD_LOCK_STATE_UNLOCKED;
4612 INIT_WORK(&rbd_dev->acquired_lock_work, rbd_notify_acquired_lock);
4613 INIT_WORK(&rbd_dev->released_lock_work, rbd_notify_released_lock);
4614 INIT_DELAYED_WORK(&rbd_dev->lock_dwork, rbd_acquire_lock);
4615 INIT_WORK(&rbd_dev->unlock_work, rbd_release_lock_work);
4616 init_waitqueue_head(&rbd_dev->lock_waitq);
4617
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004618 rbd_dev->dev.bus = &rbd_bus_type;
4619 rbd_dev->dev.type = &rbd_device_type;
4620 rbd_dev->dev.parent = &rbd_root_dev;
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004621 device_initialize(&rbd_dev->dev);
4622
Alex Elderc53d5892012-10-25 23:34:42 -05004623 rbd_dev->rbd_client = rbdc;
Ilya Dryomovd1475432015-06-22 13:24:48 +03004624 rbd_dev->spec = spec;
Alex Elder0903e872012-11-14 12:25:19 -06004625
Alex Elderc53d5892012-10-25 23:34:42 -05004626 return rbd_dev;
4627}
4628
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02004629/*
4630 * Create a mapping rbd_dev.
4631 */
4632static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
4633 struct rbd_spec *spec,
4634 struct rbd_options *opts)
4635{
4636 struct rbd_device *rbd_dev;
4637
4638 rbd_dev = __rbd_dev_create(rbdc, spec);
4639 if (!rbd_dev)
4640 return NULL;
4641
4642 rbd_dev->opts = opts;
4643
4644 /* get an id and fill in device name */
4645 rbd_dev->dev_id = ida_simple_get(&rbd_dev_id_ida, 0,
4646 minor_to_rbd_dev_id(1 << MINORBITS),
4647 GFP_KERNEL);
4648 if (rbd_dev->dev_id < 0)
4649 goto fail_rbd_dev;
4650
4651 sprintf(rbd_dev->name, RBD_DRV_NAME "%d", rbd_dev->dev_id);
4652 rbd_dev->task_wq = alloc_ordered_workqueue("%s-tasks", WQ_MEM_RECLAIM,
4653 rbd_dev->name);
4654 if (!rbd_dev->task_wq)
4655 goto fail_dev_id;
4656
4657 /* we have a ref from do_rbd_add() */
4658 __module_get(THIS_MODULE);
4659
4660 dout("%s rbd_dev %p dev_id %d\n", __func__, rbd_dev, rbd_dev->dev_id);
4661 return rbd_dev;
4662
4663fail_dev_id:
4664 ida_simple_remove(&rbd_dev_id_ida, rbd_dev->dev_id);
4665fail_rbd_dev:
4666 rbd_dev_free(rbd_dev);
4667 return NULL;
4668}
4669
Alex Elderc53d5892012-10-25 23:34:42 -05004670static void rbd_dev_destroy(struct rbd_device *rbd_dev)
4671{
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02004672 if (rbd_dev)
4673 put_device(&rbd_dev->dev);
Alex Elderc53d5892012-10-25 23:34:42 -05004674}
4675
Yehuda Sadehdfc56062010-11-19 14:51:04 -08004676/*
Alex Elder9d475de2012-07-03 16:01:19 -05004677 * Get the size and object order for an image snapshot, or if
4678 * snap_id is CEPH_NOSNAP, gets this information for the base
4679 * image.
4680 */
4681static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
4682 u8 *order, u64 *snap_size)
4683{
4684 __le64 snapid = cpu_to_le64(snap_id);
4685 int ret;
4686 struct {
4687 u8 order;
4688 __le64 size;
4689 } __attribute__ ((packed)) size_buf = { 0 };
4690
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004691 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4692 &rbd_dev->header_oloc, "get_size",
4693 &snapid, sizeof(snapid),
4694 &size_buf, sizeof(size_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004695 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder9d475de2012-07-03 16:01:19 -05004696 if (ret < 0)
4697 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004698 if (ret < sizeof (size_buf))
4699 return -ERANGE;
Alex Elder9d475de2012-07-03 16:01:19 -05004700
Josh Durginc3545572013-08-28 17:08:10 -07004701 if (order) {
Alex Elderc86f86e2013-04-25 15:09:41 -05004702 *order = size_buf.order;
Josh Durginc3545572013-08-28 17:08:10 -07004703 dout(" order %u", (unsigned int)*order);
4704 }
Alex Elder9d475de2012-07-03 16:01:19 -05004705 *snap_size = le64_to_cpu(size_buf.size);
4706
Josh Durginc3545572013-08-28 17:08:10 -07004707 dout(" snap_id 0x%016llx snap_size = %llu\n",
4708 (unsigned long long)snap_id,
Alex Elder57385b52013-04-21 12:14:45 -05004709 (unsigned long long)*snap_size);
Alex Elder9d475de2012-07-03 16:01:19 -05004710
4711 return 0;
4712}
4713
4714static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
4715{
4716 return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
4717 &rbd_dev->header.obj_order,
4718 &rbd_dev->header.image_size);
4719}
4720
Alex Elder1e130192012-07-03 16:01:19 -05004721static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
4722{
4723 void *reply_buf;
4724 int ret;
4725 void *p;
4726
4727 reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
4728 if (!reply_buf)
4729 return -ENOMEM;
4730
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004731 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4732 &rbd_dev->header_oloc, "get_object_prefix",
4733 NULL, 0, reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06004734 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder1e130192012-07-03 16:01:19 -05004735 if (ret < 0)
4736 goto out;
4737
4738 p = reply_buf;
4739 rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
Alex Elder57385b52013-04-21 12:14:45 -05004740 p + ret, NULL, GFP_NOIO);
4741 ret = 0;
Alex Elder1e130192012-07-03 16:01:19 -05004742
4743 if (IS_ERR(rbd_dev->header.object_prefix)) {
4744 ret = PTR_ERR(rbd_dev->header.object_prefix);
4745 rbd_dev->header.object_prefix = NULL;
4746 } else {
4747 dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
4748 }
Alex Elder1e130192012-07-03 16:01:19 -05004749out:
4750 kfree(reply_buf);
4751
4752 return ret;
4753}
4754
Alex Elderb1b54022012-07-03 16:01:19 -05004755static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
4756 u64 *snap_features)
4757{
4758 __le64 snapid = cpu_to_le64(snap_id);
4759 struct {
4760 __le64 features;
4761 __le64 incompat;
Alex Elder41579762013-04-21 12:14:45 -05004762 } __attribute__ ((packed)) features_buf = { 0 };
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004763 u64 unsup;
Alex Elderb1b54022012-07-03 16:01:19 -05004764 int ret;
4765
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004766 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4767 &rbd_dev->header_oloc, "get_features",
4768 &snapid, sizeof(snapid),
4769 &features_buf, sizeof(features_buf));
Alex Elder36be9a72013-01-19 00:30:28 -06004770 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderb1b54022012-07-03 16:01:19 -05004771 if (ret < 0)
4772 return ret;
Alex Elder57385b52013-04-21 12:14:45 -05004773 if (ret < sizeof (features_buf))
4774 return -ERANGE;
Alex Elderd8891402012-10-09 13:50:17 -07004775
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004776 unsup = le64_to_cpu(features_buf.incompat) & ~RBD_FEATURES_SUPPORTED;
4777 if (unsup) {
4778 rbd_warn(rbd_dev, "image uses unsupported features: 0x%llx",
4779 unsup);
Alex Elderb8f5c6e2012-11-01 08:39:26 -05004780 return -ENXIO;
Ilya Dryomovd3767f02016-04-13 14:15:50 +02004781 }
Alex Elderd8891402012-10-09 13:50:17 -07004782
Alex Elderb1b54022012-07-03 16:01:19 -05004783 *snap_features = le64_to_cpu(features_buf.features);
4784
4785 dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
Alex Elder57385b52013-04-21 12:14:45 -05004786 (unsigned long long)snap_id,
4787 (unsigned long long)*snap_features,
4788 (unsigned long long)le64_to_cpu(features_buf.incompat));
Alex Elderb1b54022012-07-03 16:01:19 -05004789
4790 return 0;
4791}
4792
4793static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
4794{
4795 return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
4796 &rbd_dev->header.features);
4797}
4798
Alex Elder86b00e02012-10-25 23:34:42 -05004799static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
4800{
4801 struct rbd_spec *parent_spec;
4802 size_t size;
4803 void *reply_buf = NULL;
4804 __le64 snapid;
4805 void *p;
4806 void *end;
Alex Elder642a2532013-05-06 17:40:33 -05004807 u64 pool_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004808 char *image_id;
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004809 u64 snap_id;
Alex Elder86b00e02012-10-25 23:34:42 -05004810 u64 overlap;
Alex Elder86b00e02012-10-25 23:34:42 -05004811 int ret;
4812
4813 parent_spec = rbd_spec_alloc();
4814 if (!parent_spec)
4815 return -ENOMEM;
4816
4817 size = sizeof (__le64) + /* pool_id */
4818 sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
4819 sizeof (__le64) + /* snap_id */
4820 sizeof (__le64); /* overlap */
4821 reply_buf = kmalloc(size, GFP_KERNEL);
4822 if (!reply_buf) {
4823 ret = -ENOMEM;
4824 goto out_err;
4825 }
4826
Ilya Dryomov4d9b67c2014-07-24 10:42:13 +04004827 snapid = cpu_to_le64(rbd_dev->spec->snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004828 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4829 &rbd_dev->header_oloc, "get_parent",
4830 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06004831 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder86b00e02012-10-25 23:34:42 -05004832 if (ret < 0)
4833 goto out_err;
4834
Alex Elder86b00e02012-10-25 23:34:42 -05004835 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05004836 end = reply_buf + ret;
4837 ret = -ERANGE;
Alex Elder642a2532013-05-06 17:40:33 -05004838 ceph_decode_64_safe(&p, end, pool_id, out_err);
Alex Elder392a9da2013-05-06 17:40:33 -05004839 if (pool_id == CEPH_NOPOOL) {
4840 /*
4841 * Either the parent never existed, or we have
4842 * record of it but the image got flattened so it no
4843 * longer has a parent. When the parent of a
4844 * layered image disappears we immediately set the
4845 * overlap to 0. The effect of this is that all new
4846 * requests will be treated as if the image had no
4847 * parent.
4848 */
4849 if (rbd_dev->parent_overlap) {
4850 rbd_dev->parent_overlap = 0;
Alex Elder392a9da2013-05-06 17:40:33 -05004851 rbd_dev_parent_put(rbd_dev);
4852 pr_info("%s: clone image has been flattened\n",
4853 rbd_dev->disk->disk_name);
4854 }
4855
Alex Elder86b00e02012-10-25 23:34:42 -05004856 goto out; /* No parent? No problem. */
Alex Elder392a9da2013-05-06 17:40:33 -05004857 }
Alex Elder86b00e02012-10-25 23:34:42 -05004858
Alex Elder0903e872012-11-14 12:25:19 -06004859 /* The ceph file layout needs to fit pool id in 32 bits */
4860
4861 ret = -EIO;
Alex Elder642a2532013-05-06 17:40:33 -05004862 if (pool_id > (u64)U32_MAX) {
Ilya Dryomov9584d502014-07-11 12:11:20 +04004863 rbd_warn(NULL, "parent pool id too large (%llu > %u)",
Alex Elder642a2532013-05-06 17:40:33 -05004864 (unsigned long long)pool_id, U32_MAX);
Alex Elder57385b52013-04-21 12:14:45 -05004865 goto out_err;
Alex Elderc0cd10db2013-04-26 09:43:47 -05004866 }
Alex Elder0903e872012-11-14 12:25:19 -06004867
Alex Elder979ed482012-11-01 08:39:26 -05004868 image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elder86b00e02012-10-25 23:34:42 -05004869 if (IS_ERR(image_id)) {
4870 ret = PTR_ERR(image_id);
4871 goto out_err;
4872 }
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004873 ceph_decode_64_safe(&p, end, snap_id, out_err);
Alex Elder86b00e02012-10-25 23:34:42 -05004874 ceph_decode_64_safe(&p, end, overlap, out_err);
4875
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004876 /*
4877 * The parent won't change (except when the clone is
4878 * flattened, already handled that). So we only need to
4879 * record the parent spec we have not already done so.
4880 */
4881 if (!rbd_dev->parent_spec) {
4882 parent_spec->pool_id = pool_id;
4883 parent_spec->image_id = image_id;
4884 parent_spec->snap_id = snap_id;
Alex Elder70cf49c2013-05-06 17:40:33 -05004885 rbd_dev->parent_spec = parent_spec;
4886 parent_spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovfbba11b2014-06-27 21:46:33 +04004887 } else {
4888 kfree(image_id);
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004889 }
4890
4891 /*
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03004892 * We always update the parent overlap. If it's zero we issue
4893 * a warning, as we will proceed as if there was no parent.
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004894 */
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004895 if (!overlap) {
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004896 if (parent_spec) {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03004897 /* refresh, careful to warn just once */
4898 if (rbd_dev->parent_overlap)
4899 rbd_warn(rbd_dev,
4900 "clone now standalone (overlap became 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004901 } else {
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03004902 /* initial probe */
4903 rbd_warn(rbd_dev, "clone is standalone (overlap 0)");
Alex Elder3b5cf2a2013-05-29 11:18:59 -05004904 }
Alex Elder70cf49c2013-05-06 17:40:33 -05004905 }
Ilya Dryomovcf32bd92015-01-19 22:57:39 +03004906 rbd_dev->parent_overlap = overlap;
4907
Alex Elder86b00e02012-10-25 23:34:42 -05004908out:
4909 ret = 0;
4910out_err:
4911 kfree(reply_buf);
4912 rbd_spec_put(parent_spec);
4913
4914 return ret;
4915}
4916
Alex Eldercc070d52013-04-21 12:14:45 -05004917static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
4918{
4919 struct {
4920 __le64 stripe_unit;
4921 __le64 stripe_count;
4922 } __attribute__ ((packed)) striping_info_buf = { 0 };
4923 size_t size = sizeof (striping_info_buf);
4924 void *p;
4925 u64 obj_size;
4926 u64 stripe_unit;
4927 u64 stripe_count;
4928 int ret;
4929
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004930 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4931 &rbd_dev->header_oloc, "get_stripe_unit_count",
4932 NULL, 0, &striping_info_buf, size);
Alex Eldercc070d52013-04-21 12:14:45 -05004933 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
4934 if (ret < 0)
4935 return ret;
4936 if (ret < size)
4937 return -ERANGE;
4938
4939 /*
4940 * We don't actually support the "fancy striping" feature
4941 * (STRIPINGV2) yet, but if the striping sizes are the
4942 * defaults the behavior is the same as before. So find
4943 * out, and only fail if the image has non-default values.
4944 */
4945 ret = -EINVAL;
Ilya Dryomov5bc3fb12017-01-25 18:16:22 +01004946 obj_size = rbd_obj_bytes(&rbd_dev->header);
Alex Eldercc070d52013-04-21 12:14:45 -05004947 p = &striping_info_buf;
4948 stripe_unit = ceph_decode_64(&p);
4949 if (stripe_unit != obj_size) {
4950 rbd_warn(rbd_dev, "unsupported stripe unit "
4951 "(got %llu want %llu)",
4952 stripe_unit, obj_size);
4953 return -EINVAL;
4954 }
4955 stripe_count = ceph_decode_64(&p);
4956 if (stripe_count != 1) {
4957 rbd_warn(rbd_dev, "unsupported stripe count "
4958 "(got %llu want 1)", stripe_count);
4959 return -EINVAL;
4960 }
Alex Elder500d0c02013-04-26 09:43:47 -05004961 rbd_dev->header.stripe_unit = stripe_unit;
4962 rbd_dev->header.stripe_count = stripe_count;
Alex Eldercc070d52013-04-21 12:14:45 -05004963
4964 return 0;
4965}
4966
Ilya Dryomov7e973322017-01-25 18:16:22 +01004967static int rbd_dev_v2_data_pool(struct rbd_device *rbd_dev)
4968{
4969 __le64 data_pool_id;
4970 int ret;
4971
4972 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
4973 &rbd_dev->header_oloc, "get_data_pool",
4974 NULL, 0, &data_pool_id, sizeof(data_pool_id));
4975 if (ret < 0)
4976 return ret;
4977 if (ret < sizeof(data_pool_id))
4978 return -EBADMSG;
4979
4980 rbd_dev->header.data_pool_id = le64_to_cpu(data_pool_id);
4981 WARN_ON(rbd_dev->header.data_pool_id == CEPH_NOPOOL);
4982 return 0;
4983}
4984
Alex Elder9e15b772012-10-30 19:40:33 -05004985static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
4986{
Ilya Dryomovecd4a682017-01-25 18:16:21 +01004987 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder9e15b772012-10-30 19:40:33 -05004988 size_t image_id_size;
4989 char *image_id;
4990 void *p;
4991 void *end;
4992 size_t size;
4993 void *reply_buf = NULL;
4994 size_t len = 0;
4995 char *image_name = NULL;
4996 int ret;
4997
4998 rbd_assert(!rbd_dev->spec->image_name);
4999
Alex Elder69e7a022012-11-01 08:39:26 -05005000 len = strlen(rbd_dev->spec->image_id);
5001 image_id_size = sizeof (__le32) + len;
Alex Elder9e15b772012-10-30 19:40:33 -05005002 image_id = kmalloc(image_id_size, GFP_KERNEL);
5003 if (!image_id)
5004 return NULL;
5005
5006 p = image_id;
Alex Elder41579762013-04-21 12:14:45 -05005007 end = image_id + image_id_size;
Alex Elder57385b52013-04-21 12:14:45 -05005008 ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
Alex Elder9e15b772012-10-30 19:40:33 -05005009
5010 size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
5011 reply_buf = kmalloc(size, GFP_KERNEL);
5012 if (!reply_buf)
5013 goto out;
5014
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005015 ceph_oid_printf(&oid, "%s", RBD_DIRECTORY);
5016 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5017 "dir_get_name", image_id, image_id_size,
5018 reply_buf, size);
Alex Elder9e15b772012-10-30 19:40:33 -05005019 if (ret < 0)
5020 goto out;
5021 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005022 end = reply_buf + ret;
5023
Alex Elder9e15b772012-10-30 19:40:33 -05005024 image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
5025 if (IS_ERR(image_name))
5026 image_name = NULL;
5027 else
5028 dout("%s: name is %s len is %zd\n", __func__, image_name, len);
5029out:
5030 kfree(reply_buf);
5031 kfree(image_id);
5032
5033 return image_name;
5034}
5035
Alex Elder2ad3d712013-04-30 00:44:33 -05005036static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5037{
5038 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5039 const char *snap_name;
5040 u32 which = 0;
5041
5042 /* Skip over names until we find the one we are looking for */
5043
5044 snap_name = rbd_dev->header.snap_names;
5045 while (which < snapc->num_snaps) {
5046 if (!strcmp(name, snap_name))
5047 return snapc->snaps[which];
5048 snap_name += strlen(snap_name) + 1;
5049 which++;
5050 }
5051 return CEPH_NOSNAP;
5052}
5053
5054static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5055{
5056 struct ceph_snap_context *snapc = rbd_dev->header.snapc;
5057 u32 which;
5058 bool found = false;
5059 u64 snap_id;
5060
5061 for (which = 0; !found && which < snapc->num_snaps; which++) {
5062 const char *snap_name;
5063
5064 snap_id = snapc->snaps[which];
5065 snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
Josh Durginefadc982013-08-29 19:16:42 -07005066 if (IS_ERR(snap_name)) {
5067 /* ignore no-longer existing snapshots */
5068 if (PTR_ERR(snap_name) == -ENOENT)
5069 continue;
5070 else
5071 break;
5072 }
Alex Elder2ad3d712013-04-30 00:44:33 -05005073 found = !strcmp(name, snap_name);
5074 kfree(snap_name);
5075 }
5076 return found ? snap_id : CEPH_NOSNAP;
5077}
5078
5079/*
5080 * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
5081 * no snapshot by that name is found, or if an error occurs.
5082 */
5083static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
5084{
5085 if (rbd_dev->image_format == 1)
5086 return rbd_v1_snap_id_by_name(rbd_dev, name);
5087
5088 return rbd_v2_snap_id_by_name(rbd_dev, name);
5089}
5090
Alex Elder9e15b772012-10-30 19:40:33 -05005091/*
Ilya Dryomov04077592014-07-23 17:11:20 +04005092 * An image being mapped will have everything but the snap id.
Alex Elder9e15b772012-10-30 19:40:33 -05005093 */
Ilya Dryomov04077592014-07-23 17:11:20 +04005094static int rbd_spec_fill_snap_id(struct rbd_device *rbd_dev)
5095{
5096 struct rbd_spec *spec = rbd_dev->spec;
5097
5098 rbd_assert(spec->pool_id != CEPH_NOPOOL && spec->pool_name);
5099 rbd_assert(spec->image_id && spec->image_name);
5100 rbd_assert(spec->snap_name);
5101
5102 if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
5103 u64 snap_id;
5104
5105 snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
5106 if (snap_id == CEPH_NOSNAP)
5107 return -ENOENT;
5108
5109 spec->snap_id = snap_id;
5110 } else {
5111 spec->snap_id = CEPH_NOSNAP;
5112 }
5113
5114 return 0;
5115}
5116
5117/*
5118 * A parent image will have all ids but none of the names.
5119 *
5120 * All names in an rbd spec are dynamically allocated. It's OK if we
5121 * can't figure out the name for an image id.
5122 */
5123static int rbd_spec_fill_names(struct rbd_device *rbd_dev)
Alex Elder9e15b772012-10-30 19:40:33 -05005124{
Alex Elder2e9f7f12013-04-26 09:43:48 -05005125 struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
5126 struct rbd_spec *spec = rbd_dev->spec;
5127 const char *pool_name;
5128 const char *image_name;
5129 const char *snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005130 int ret;
5131
Ilya Dryomov04077592014-07-23 17:11:20 +04005132 rbd_assert(spec->pool_id != CEPH_NOPOOL);
5133 rbd_assert(spec->image_id);
5134 rbd_assert(spec->snap_id != CEPH_NOSNAP);
Alex Elder9e15b772012-10-30 19:40:33 -05005135
Alex Elder2e9f7f12013-04-26 09:43:48 -05005136 /* Get the pool name; we have to make our own copy of this */
Alex Elder9e15b772012-10-30 19:40:33 -05005137
Alex Elder2e9f7f12013-04-26 09:43:48 -05005138 pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
5139 if (!pool_name) {
5140 rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
Alex Elder935dc892012-11-01 10:17:15 -05005141 return -EIO;
5142 }
Alex Elder2e9f7f12013-04-26 09:43:48 -05005143 pool_name = kstrdup(pool_name, GFP_KERNEL);
5144 if (!pool_name)
Alex Elder9e15b772012-10-30 19:40:33 -05005145 return -ENOMEM;
5146
5147 /* Fetch the image name; tolerate failure here */
5148
Alex Elder2e9f7f12013-04-26 09:43:48 -05005149 image_name = rbd_dev_image_name(rbd_dev);
5150 if (!image_name)
Alex Elder06ecc6c2012-11-01 10:17:15 -05005151 rbd_warn(rbd_dev, "unable to get image name");
Alex Elder9e15b772012-10-30 19:40:33 -05005152
Ilya Dryomov04077592014-07-23 17:11:20 +04005153 /* Fetch the snapshot name */
Alex Elder9e15b772012-10-30 19:40:33 -05005154
Alex Elder2e9f7f12013-04-26 09:43:48 -05005155 snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
Josh Durginda6a6b62013-09-04 17:57:31 -07005156 if (IS_ERR(snap_name)) {
5157 ret = PTR_ERR(snap_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005158 goto out_err;
Alex Elder2e9f7f12013-04-26 09:43:48 -05005159 }
5160
5161 spec->pool_name = pool_name;
5162 spec->image_name = image_name;
5163 spec->snap_name = snap_name;
Alex Elder9e15b772012-10-30 19:40:33 -05005164
5165 return 0;
Ilya Dryomov04077592014-07-23 17:11:20 +04005166
Alex Elder9e15b772012-10-30 19:40:33 -05005167out_err:
Alex Elder2e9f7f12013-04-26 09:43:48 -05005168 kfree(image_name);
5169 kfree(pool_name);
Alex Elder9e15b772012-10-30 19:40:33 -05005170 return ret;
5171}
5172
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005173static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
Alex Elder35d489f2012-07-03 16:01:19 -05005174{
5175 size_t size;
5176 int ret;
5177 void *reply_buf;
5178 void *p;
5179 void *end;
5180 u64 seq;
5181 u32 snap_count;
5182 struct ceph_snap_context *snapc;
5183 u32 i;
5184
5185 /*
5186 * We'll need room for the seq value (maximum snapshot id),
5187 * snapshot count, and array of that many snapshot ids.
5188 * For now we have a fixed upper limit on the number we're
5189 * prepared to receive.
5190 */
5191 size = sizeof (__le64) + sizeof (__le32) +
5192 RBD_MAX_SNAP_COUNT * sizeof (__le64);
5193 reply_buf = kzalloc(size, GFP_KERNEL);
5194 if (!reply_buf)
5195 return -ENOMEM;
5196
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005197 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5198 &rbd_dev->header_oloc, "get_snapcontext",
5199 NULL, 0, reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005200 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elder35d489f2012-07-03 16:01:19 -05005201 if (ret < 0)
5202 goto out;
5203
Alex Elder35d489f2012-07-03 16:01:19 -05005204 p = reply_buf;
Alex Elder57385b52013-04-21 12:14:45 -05005205 end = reply_buf + ret;
5206 ret = -ERANGE;
Alex Elder35d489f2012-07-03 16:01:19 -05005207 ceph_decode_64_safe(&p, end, seq, out);
5208 ceph_decode_32_safe(&p, end, snap_count, out);
5209
5210 /*
5211 * Make sure the reported number of snapshot ids wouldn't go
5212 * beyond the end of our buffer. But before checking that,
5213 * make sure the computed size of the snapshot context we
5214 * allocate is representable in a size_t.
5215 */
5216 if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
5217 / sizeof (u64)) {
5218 ret = -EINVAL;
5219 goto out;
5220 }
5221 if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
5222 goto out;
Alex Elder468521c2013-04-26 09:43:47 -05005223 ret = 0;
Alex Elder35d489f2012-07-03 16:01:19 -05005224
Alex Elder812164f82013-04-30 00:44:32 -05005225 snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
Alex Elder35d489f2012-07-03 16:01:19 -05005226 if (!snapc) {
5227 ret = -ENOMEM;
5228 goto out;
5229 }
Alex Elder35d489f2012-07-03 16:01:19 -05005230 snapc->seq = seq;
Alex Elder35d489f2012-07-03 16:01:19 -05005231 for (i = 0; i < snap_count; i++)
5232 snapc->snaps[i] = ceph_decode_64(&p);
5233
Alex Elder49ece552013-05-06 08:37:00 -05005234 ceph_put_snap_context(rbd_dev->header.snapc);
Alex Elder35d489f2012-07-03 16:01:19 -05005235 rbd_dev->header.snapc = snapc;
5236
5237 dout(" snap context seq = %llu, snap_count = %u\n",
Alex Elder57385b52013-04-21 12:14:45 -05005238 (unsigned long long)seq, (unsigned int)snap_count);
Alex Elder35d489f2012-07-03 16:01:19 -05005239out:
5240 kfree(reply_buf);
5241
Alex Elder57385b52013-04-21 12:14:45 -05005242 return ret;
Alex Elder35d489f2012-07-03 16:01:19 -05005243}
5244
Alex Elder54cac612013-04-30 00:44:33 -05005245static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
5246 u64 snap_id)
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005247{
5248 size_t size;
5249 void *reply_buf;
Alex Elder54cac612013-04-30 00:44:33 -05005250 __le64 snapid;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005251 int ret;
5252 void *p;
5253 void *end;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005254 char *snap_name;
5255
5256 size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
5257 reply_buf = kmalloc(size, GFP_KERNEL);
5258 if (!reply_buf)
5259 return ERR_PTR(-ENOMEM);
5260
Alex Elder54cac612013-04-30 00:44:33 -05005261 snapid = cpu_to_le64(snap_id);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005262 ret = rbd_obj_method_sync(rbd_dev, &rbd_dev->header_oid,
5263 &rbd_dev->header_oloc, "get_snapshot_name",
5264 &snapid, sizeof(snapid), reply_buf, size);
Alex Elder36be9a72013-01-19 00:30:28 -06005265 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderf40eb342013-04-25 15:09:42 -05005266 if (ret < 0) {
5267 snap_name = ERR_PTR(ret);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005268 goto out;
Alex Elderf40eb342013-04-25 15:09:42 -05005269 }
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005270
5271 p = reply_buf;
Alex Elderf40eb342013-04-25 15:09:42 -05005272 end = reply_buf + ret;
Alex Eldere5c35532012-10-25 23:34:41 -05005273 snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
Alex Elderf40eb342013-04-25 15:09:42 -05005274 if (IS_ERR(snap_name))
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005275 goto out;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005276
Alex Elderf40eb342013-04-25 15:09:42 -05005277 dout(" snap_id 0x%016llx snap_name = %s\n",
Alex Elder54cac612013-04-30 00:44:33 -05005278 (unsigned long long)snap_id, snap_name);
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005279out:
5280 kfree(reply_buf);
5281
Alex Elderf40eb342013-04-25 15:09:42 -05005282 return snap_name;
Alex Elderb8b1e2d2012-07-03 16:01:19 -05005283}
5284
Alex Elder2df3fac2013-05-06 09:51:30 -05005285static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
Alex Elder117973f2012-08-31 17:29:55 -05005286{
Alex Elder2df3fac2013-05-06 09:51:30 -05005287 bool first_time = rbd_dev->header.object_prefix == NULL;
Alex Elder117973f2012-08-31 17:29:55 -05005288 int ret;
Alex Elder117973f2012-08-31 17:29:55 -05005289
Josh Durgin1617e402013-06-12 14:43:10 -07005290 ret = rbd_dev_v2_image_size(rbd_dev);
5291 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005292 return ret;
Josh Durgin1617e402013-06-12 14:43:10 -07005293
Alex Elder2df3fac2013-05-06 09:51:30 -05005294 if (first_time) {
5295 ret = rbd_dev_v2_header_onetime(rbd_dev);
5296 if (ret)
Alex Eldercfbf6372013-05-31 17:40:45 -05005297 return ret;
Alex Elder2df3fac2013-05-06 09:51:30 -05005298 }
5299
Alex Eldercc4a38bd2013-04-30 00:44:33 -05005300 ret = rbd_dev_v2_snap_context(rbd_dev);
Ilya Dryomovd194cd12015-08-31 18:22:10 +03005301 if (ret && first_time) {
5302 kfree(rbd_dev->header.object_prefix);
5303 rbd_dev->header.object_prefix = NULL;
5304 }
Alex Elder117973f2012-08-31 17:29:55 -05005305
5306 return ret;
5307}
5308
Ilya Dryomova720ae02014-07-23 17:11:19 +04005309static int rbd_dev_header_info(struct rbd_device *rbd_dev)
5310{
5311 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
5312
5313 if (rbd_dev->image_format == 1)
5314 return rbd_dev_v1_header_info(rbd_dev);
5315
5316 return rbd_dev_v2_header_info(rbd_dev);
5317}
5318
Alex Elder1ddbe942012-01-29 13:57:44 -06005319/*
Alex Eldere28fff262012-02-02 08:13:30 -06005320 * Skips over white space at *buf, and updates *buf to point to the
5321 * first found non-space character (if any). Returns the length of
Alex Elder593a9e72012-02-07 12:03:37 -06005322 * the token (string of non-white space characters) found. Note
5323 * that *buf must be terminated with '\0'.
Alex Eldere28fff262012-02-02 08:13:30 -06005324 */
5325static inline size_t next_token(const char **buf)
5326{
5327 /*
5328 * These are the characters that produce nonzero for
5329 * isspace() in the "C" and "POSIX" locales.
5330 */
5331 const char *spaces = " \f\n\r\t\v";
5332
5333 *buf += strspn(*buf, spaces); /* Find start of token */
5334
5335 return strcspn(*buf, spaces); /* Return token length */
5336}
5337
5338/*
Alex Elderea3352f2012-07-09 21:04:23 -05005339 * Finds the next token in *buf, dynamically allocates a buffer big
5340 * enough to hold a copy of it, and copies the token into the new
5341 * buffer. The copy is guaranteed to be terminated with '\0'. Note
5342 * that a duplicate buffer is created even for a zero-length token.
5343 *
5344 * Returns a pointer to the newly-allocated duplicate, or a null
5345 * pointer if memory for the duplicate was not available. If
5346 * the lenp argument is a non-null pointer, the length of the token
5347 * (not including the '\0') is returned in *lenp.
5348 *
5349 * If successful, the *buf pointer will be updated to point beyond
5350 * the end of the found token.
5351 *
5352 * Note: uses GFP_KERNEL for allocation.
5353 */
5354static inline char *dup_token(const char **buf, size_t *lenp)
5355{
5356 char *dup;
5357 size_t len;
5358
5359 len = next_token(buf);
Alex Elder4caf35f2012-11-01 08:39:27 -05005360 dup = kmemdup(*buf, len + 1, GFP_KERNEL);
Alex Elderea3352f2012-07-09 21:04:23 -05005361 if (!dup)
5362 return NULL;
Alex Elderea3352f2012-07-09 21:04:23 -05005363 *(dup + len) = '\0';
5364 *buf += len;
5365
5366 if (lenp)
5367 *lenp = len;
5368
5369 return dup;
5370}
5371
5372/*
Alex Elder859c31d2012-10-25 23:34:42 -05005373 * Parse the options provided for an "rbd add" (i.e., rbd image
5374 * mapping) request. These arrive via a write to /sys/bus/rbd/add,
5375 * and the data written is passed here via a NUL-terminated buffer.
5376 * Returns 0 if successful or an error code otherwise.
Alex Elderd22f76e2012-07-12 10:46:35 -05005377 *
Alex Elder859c31d2012-10-25 23:34:42 -05005378 * The information extracted from these options is recorded in
5379 * the other parameters which return dynamically-allocated
5380 * structures:
5381 * ceph_opts
5382 * The address of a pointer that will refer to a ceph options
5383 * structure. Caller must release the returned pointer using
5384 * ceph_destroy_options() when it is no longer needed.
5385 * rbd_opts
5386 * Address of an rbd options pointer. Fully initialized by
5387 * this function; caller must release with kfree().
5388 * spec
5389 * Address of an rbd image specification pointer. Fully
5390 * initialized by this function based on parsed options.
5391 * Caller must release with rbd_spec_put().
5392 *
5393 * The options passed take this form:
5394 * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
5395 * where:
5396 * <mon_addrs>
5397 * A comma-separated list of one or more monitor addresses.
5398 * A monitor address is an ip address, optionally followed
5399 * by a port number (separated by a colon).
5400 * I.e.: ip1[:port1][,ip2[:port2]...]
5401 * <options>
5402 * A comma-separated list of ceph and/or rbd options.
5403 * <pool_name>
5404 * The name of the rados pool containing the rbd image.
5405 * <image_name>
5406 * The name of the image in that pool to map.
5407 * <snap_id>
5408 * An optional snapshot id. If provided, the mapping will
5409 * present data from the image at the time that snapshot was
5410 * created. The image head is used if no snapshot id is
5411 * provided. Snapshot mappings are always read-only.
Alex Eldera725f65e2012-02-02 08:13:30 -06005412 */
Alex Elder859c31d2012-10-25 23:34:42 -05005413static int rbd_add_parse_args(const char *buf,
Alex Elderdc79b112012-10-25 23:34:41 -05005414 struct ceph_options **ceph_opts,
Alex Elder859c31d2012-10-25 23:34:42 -05005415 struct rbd_options **opts,
5416 struct rbd_spec **rbd_spec)
Alex Eldera725f65e2012-02-02 08:13:30 -06005417{
Alex Elderd22f76e2012-07-12 10:46:35 -05005418 size_t len;
Alex Elder859c31d2012-10-25 23:34:42 -05005419 char *options;
Alex Elder0ddebc02012-10-25 23:34:41 -05005420 const char *mon_addrs;
Alex Elderecb4dc22013-04-26 09:43:47 -05005421 char *snap_name;
Alex Elder0ddebc02012-10-25 23:34:41 -05005422 size_t mon_addrs_size;
Alex Elder859c31d2012-10-25 23:34:42 -05005423 struct rbd_spec *spec = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005424 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005425 struct ceph_options *copts;
Alex Elderdc79b112012-10-25 23:34:41 -05005426 int ret;
Alex Eldere28fff262012-02-02 08:13:30 -06005427
5428 /* The first four tokens are required */
5429
Alex Elder7ef32142012-02-02 08:13:30 -06005430 len = next_token(&buf);
Alex Elder4fb5d6712012-11-01 10:17:15 -05005431 if (!len) {
5432 rbd_warn(NULL, "no monitor address(es) provided");
5433 return -EINVAL;
5434 }
Alex Elder0ddebc02012-10-25 23:34:41 -05005435 mon_addrs = buf;
Alex Elderf28e5652012-10-25 23:34:41 -05005436 mon_addrs_size = len + 1;
Alex Elder7ef32142012-02-02 08:13:30 -06005437 buf += len;
Alex Eldera725f65e2012-02-02 08:13:30 -06005438
Alex Elderdc79b112012-10-25 23:34:41 -05005439 ret = -EINVAL;
Alex Elderf28e5652012-10-25 23:34:41 -05005440 options = dup_token(&buf, NULL);
5441 if (!options)
Alex Elderdc79b112012-10-25 23:34:41 -05005442 return -ENOMEM;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005443 if (!*options) {
5444 rbd_warn(NULL, "no options provided");
5445 goto out_err;
5446 }
Alex Eldera725f65e2012-02-02 08:13:30 -06005447
Alex Elder859c31d2012-10-25 23:34:42 -05005448 spec = rbd_spec_alloc();
5449 if (!spec)
Alex Elderf28e5652012-10-25 23:34:41 -05005450 goto out_mem;
Alex Elder859c31d2012-10-25 23:34:42 -05005451
5452 spec->pool_name = dup_token(&buf, NULL);
5453 if (!spec->pool_name)
5454 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005455 if (!*spec->pool_name) {
5456 rbd_warn(NULL, "no pool name provided");
5457 goto out_err;
5458 }
Alex Eldere28fff262012-02-02 08:13:30 -06005459
Alex Elder69e7a022012-11-01 08:39:26 -05005460 spec->image_name = dup_token(&buf, NULL);
Alex Elder859c31d2012-10-25 23:34:42 -05005461 if (!spec->image_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005462 goto out_mem;
Alex Elder4fb5d6712012-11-01 10:17:15 -05005463 if (!*spec->image_name) {
5464 rbd_warn(NULL, "no image name provided");
5465 goto out_err;
5466 }
Alex Eldere28fff262012-02-02 08:13:30 -06005467
Alex Elderf28e5652012-10-25 23:34:41 -05005468 /*
5469 * Snapshot name is optional; default is to use "-"
5470 * (indicating the head/no snapshot).
5471 */
Alex Elder3feeb8942012-08-31 17:29:52 -05005472 len = next_token(&buf);
Alex Elder820a5f32012-07-09 21:04:24 -05005473 if (!len) {
Alex Elder3feeb8942012-08-31 17:29:52 -05005474 buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
5475 len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
Alex Elderf28e5652012-10-25 23:34:41 -05005476 } else if (len > RBD_MAX_SNAP_NAME_LEN) {
Alex Elderdc79b112012-10-25 23:34:41 -05005477 ret = -ENAMETOOLONG;
Alex Elderf28e5652012-10-25 23:34:41 -05005478 goto out_err;
Alex Elder849b4262012-07-09 21:04:24 -05005479 }
Alex Elderecb4dc22013-04-26 09:43:47 -05005480 snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
5481 if (!snap_name)
Alex Elderf28e5652012-10-25 23:34:41 -05005482 goto out_mem;
Alex Elderecb4dc22013-04-26 09:43:47 -05005483 *(snap_name + len) = '\0';
5484 spec->snap_name = snap_name;
Alex Eldere5c35532012-10-25 23:34:41 -05005485
Alex Elder0ddebc02012-10-25 23:34:41 -05005486 /* Initialize all rbd options to the defaults */
Alex Eldere28fff262012-02-02 08:13:30 -06005487
Alex Elder4e9afeb2012-10-25 23:34:41 -05005488 rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
5489 if (!rbd_opts)
5490 goto out_mem;
5491
5492 rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
Ilya Dryomovb5584182015-06-23 16:21:19 +03005493 rbd_opts->queue_depth = RBD_QUEUE_DEPTH_DEFAULT;
Ilya Dryomov80de1912016-09-20 14:23:17 +02005494 rbd_opts->lock_on_read = RBD_LOCK_ON_READ_DEFAULT;
Ilya Dryomove010dd02017-04-13 12:17:39 +02005495 rbd_opts->exclusive = RBD_EXCLUSIVE_DEFAULT;
Alex Elderd22f76e2012-07-12 10:46:35 -05005496
Alex Elder859c31d2012-10-25 23:34:42 -05005497 copts = ceph_parse_options(options, mon_addrs,
Alex Elder0ddebc02012-10-25 23:34:41 -05005498 mon_addrs + mon_addrs_size - 1,
Alex Elder4e9afeb2012-10-25 23:34:41 -05005499 parse_rbd_opts_token, rbd_opts);
Alex Elder859c31d2012-10-25 23:34:42 -05005500 if (IS_ERR(copts)) {
5501 ret = PTR_ERR(copts);
Alex Elderdc79b112012-10-25 23:34:41 -05005502 goto out_err;
5503 }
Alex Elder859c31d2012-10-25 23:34:42 -05005504 kfree(options);
5505
5506 *ceph_opts = copts;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005507 *opts = rbd_opts;
Alex Elder859c31d2012-10-25 23:34:42 -05005508 *rbd_spec = spec;
Alex Elder0ddebc02012-10-25 23:34:41 -05005509
Alex Elderdc79b112012-10-25 23:34:41 -05005510 return 0;
Alex Elderf28e5652012-10-25 23:34:41 -05005511out_mem:
Alex Elderdc79b112012-10-25 23:34:41 -05005512 ret = -ENOMEM;
Alex Elderd22f76e2012-07-12 10:46:35 -05005513out_err:
Alex Elder859c31d2012-10-25 23:34:42 -05005514 kfree(rbd_opts);
5515 rbd_spec_put(spec);
Alex Elderf28e5652012-10-25 23:34:41 -05005516 kfree(options);
Alex Elderd22f76e2012-07-12 10:46:35 -05005517
Alex Elderdc79b112012-10-25 23:34:41 -05005518 return ret;
Alex Eldera725f65e2012-02-02 08:13:30 -06005519}
5520
Alex Elder589d30e2012-07-10 20:30:11 -05005521/*
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005522 * Return pool id (>= 0) or a negative error code.
5523 */
5524static int rbd_add_get_pool_id(struct rbd_client *rbdc, const char *pool_name)
5525{
Ilya Dryomova319bf52015-05-15 12:02:17 +03005526 struct ceph_options *opts = rbdc->client->options;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005527 u64 newest_epoch;
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005528 int tries = 0;
5529 int ret;
5530
5531again:
5532 ret = ceph_pg_poolid_by_name(rbdc->client->osdc.osdmap, pool_name);
5533 if (ret == -ENOENT && tries++ < 1) {
Ilya Dryomovd0b19702016-04-28 16:07:27 +02005534 ret = ceph_monc_get_version(&rbdc->client->monc, "osdmap",
5535 &newest_epoch);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005536 if (ret < 0)
5537 return ret;
5538
5539 if (rbdc->client->osdc.osdmap->epoch < newest_epoch) {
Ilya Dryomov7cca78c2016-04-28 16:07:28 +02005540 ceph_osdc_maybe_request_map(&rbdc->client->osdc);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005541 (void) ceph_monc_wait_osdmap(&rbdc->client->monc,
Ilya Dryomova319bf52015-05-15 12:02:17 +03005542 newest_epoch,
5543 opts->mount_timeout);
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005544 goto again;
5545 } else {
5546 /* the osdmap we have is new enough */
5547 return -ENOENT;
5548 }
5549 }
5550
5551 return ret;
5552}
5553
Ilya Dryomove010dd02017-04-13 12:17:39 +02005554static void rbd_dev_image_unlock(struct rbd_device *rbd_dev)
5555{
5556 down_write(&rbd_dev->lock_rwsem);
5557 if (__rbd_is_lock_owner(rbd_dev))
5558 rbd_unlock(rbd_dev);
5559 up_write(&rbd_dev->lock_rwsem);
5560}
5561
5562static int rbd_add_acquire_lock(struct rbd_device *rbd_dev)
5563{
5564 if (!(rbd_dev->header.features & RBD_FEATURE_EXCLUSIVE_LOCK)) {
5565 rbd_warn(rbd_dev, "exclusive-lock feature is not enabled");
5566 return -EINVAL;
5567 }
5568
5569 /* FIXME: "rbd map --exclusive" should be in interruptible */
5570 down_read(&rbd_dev->lock_rwsem);
5571 rbd_wait_state_locked(rbd_dev);
5572 up_read(&rbd_dev->lock_rwsem);
5573 if (test_bit(RBD_DEV_FLAG_BLACKLISTED, &rbd_dev->flags)) {
5574 rbd_warn(rbd_dev, "failed to acquire exclusive lock");
5575 return -EROFS;
5576 }
5577
5578 return 0;
5579}
5580
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005581/*
Alex Elder589d30e2012-07-10 20:30:11 -05005582 * An rbd format 2 image has a unique identifier, distinct from the
5583 * name given to it by the user. Internally, that identifier is
5584 * what's used to specify the names of objects related to the image.
5585 *
5586 * A special "rbd id" object is used to map an rbd image name to its
5587 * id. If that object doesn't exist, then there is no v2 rbd image
5588 * with the supplied name.
5589 *
5590 * This function will record the given rbd_dev's image_id field if
5591 * it can be determined, and in that case will return 0. If any
5592 * errors occur a negative errno will be returned and the rbd_dev's
5593 * image_id field will be unchanged (and should be NULL).
5594 */
5595static int rbd_dev_image_id(struct rbd_device *rbd_dev)
5596{
5597 int ret;
5598 size_t size;
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005599 CEPH_DEFINE_OID_ONSTACK(oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005600 void *response;
Alex Elderc0fba362013-04-25 23:15:08 -05005601 char *image_id;
Alex Elder2f82ee52012-10-30 19:40:33 -05005602
Alex Elder589d30e2012-07-10 20:30:11 -05005603 /*
Alex Elder2c0d0a12012-10-30 19:40:33 -05005604 * When probing a parent image, the image id is already
5605 * known (and the image name likely is not). There's no
Alex Elderc0fba362013-04-25 23:15:08 -05005606 * need to fetch the image id again in this case. We
5607 * do still need to set the image format though.
Alex Elder2c0d0a12012-10-30 19:40:33 -05005608 */
Alex Elderc0fba362013-04-25 23:15:08 -05005609 if (rbd_dev->spec->image_id) {
5610 rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
5611
Alex Elder2c0d0a12012-10-30 19:40:33 -05005612 return 0;
Alex Elderc0fba362013-04-25 23:15:08 -05005613 }
Alex Elder2c0d0a12012-10-30 19:40:33 -05005614
5615 /*
Alex Elder589d30e2012-07-10 20:30:11 -05005616 * First, see if the format 2 image id file exists, and if
5617 * so, get the image's persistent id from it.
5618 */
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005619 ret = ceph_oid_aprintf(&oid, GFP_KERNEL, "%s%s", RBD_ID_PREFIX,
5620 rbd_dev->spec->image_name);
5621 if (ret)
5622 return ret;
5623
5624 dout("rbd id object name is %s\n", oid.name);
Alex Elder589d30e2012-07-10 20:30:11 -05005625
5626 /* Response will be an encoded string, which includes a length */
5627
5628 size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
5629 response = kzalloc(size, GFP_NOIO);
5630 if (!response) {
5631 ret = -ENOMEM;
5632 goto out;
5633 }
5634
Alex Elderc0fba362013-04-25 23:15:08 -05005635 /* If it doesn't exist we'll assume it's a format 1 image */
5636
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005637 ret = rbd_obj_method_sync(rbd_dev, &oid, &rbd_dev->header_oloc,
5638 "get_id", NULL, 0,
5639 response, RBD_IMAGE_ID_LEN_MAX);
Alex Elder36be9a72013-01-19 00:30:28 -06005640 dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
Alex Elderc0fba362013-04-25 23:15:08 -05005641 if (ret == -ENOENT) {
5642 image_id = kstrdup("", GFP_KERNEL);
5643 ret = image_id ? 0 : -ENOMEM;
5644 if (!ret)
5645 rbd_dev->image_format = 1;
Ilya Dryomov7dd440c2014-09-11 18:49:18 +04005646 } else if (ret >= 0) {
Alex Elderc0fba362013-04-25 23:15:08 -05005647 void *p = response;
Alex Elder589d30e2012-07-10 20:30:11 -05005648
Alex Elderc0fba362013-04-25 23:15:08 -05005649 image_id = ceph_extract_encoded_string(&p, p + ret,
Alex Elder979ed482012-11-01 08:39:26 -05005650 NULL, GFP_NOIO);
Duan Jiong461f7582014-04-11 16:38:12 +08005651 ret = PTR_ERR_OR_ZERO(image_id);
Alex Elderc0fba362013-04-25 23:15:08 -05005652 if (!ret)
5653 rbd_dev->image_format = 2;
Alex Elderc0fba362013-04-25 23:15:08 -05005654 }
5655
5656 if (!ret) {
5657 rbd_dev->spec->image_id = image_id;
5658 dout("image_id is %s\n", image_id);
Alex Elder589d30e2012-07-10 20:30:11 -05005659 }
5660out:
5661 kfree(response);
Ilya Dryomovecd4a682017-01-25 18:16:21 +01005662 ceph_oid_destroy(&oid);
Alex Elder589d30e2012-07-10 20:30:11 -05005663 return ret;
5664}
5665
Alex Elder3abef3b2013-05-13 20:35:37 -05005666/*
5667 * Undo whatever state changes are made by v1 or v2 header info
5668 * call.
5669 */
Alex Elder6fd48b32013-04-28 23:32:34 -05005670static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
5671{
5672 struct rbd_image_header *header;
5673
Ilya Dryomove69b8d42015-01-19 12:06:14 +03005674 rbd_dev_parent_put(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005675
5676 /* Free dynamic fields from the header, then zero it out */
5677
5678 header = &rbd_dev->header;
Alex Elder812164f82013-04-30 00:44:32 -05005679 ceph_put_snap_context(header->snapc);
Alex Elder6fd48b32013-04-28 23:32:34 -05005680 kfree(header->snap_sizes);
5681 kfree(header->snap_names);
5682 kfree(header->object_prefix);
5683 memset(header, 0, sizeof (*header));
5684}
5685
Alex Elder2df3fac2013-05-06 09:51:30 -05005686static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
Alex Eldera30b71b2012-07-10 20:30:11 -05005687{
5688 int ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005689
Alex Elder1e130192012-07-03 16:01:19 -05005690 ret = rbd_dev_v2_object_prefix(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005691 if (ret)
Alex Elder1e130192012-07-03 16:01:19 -05005692 goto out_err;
Alex Elderb1b54022012-07-03 16:01:19 -05005693
Alex Elder2df3fac2013-05-06 09:51:30 -05005694 /*
5695 * Get the and check features for the image. Currently the
5696 * features are assumed to never change.
5697 */
Alex Elderb1b54022012-07-03 16:01:19 -05005698 ret = rbd_dev_v2_features(rbd_dev);
Alex Elder57385b52013-04-21 12:14:45 -05005699 if (ret)
Alex Elderb1b54022012-07-03 16:01:19 -05005700 goto out_err;
Alex Elder35d489f2012-07-03 16:01:19 -05005701
Alex Eldercc070d52013-04-21 12:14:45 -05005702 /* If the image supports fancy striping, get its parameters */
5703
5704 if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
5705 ret = rbd_dev_v2_striping_info(rbd_dev);
5706 if (ret < 0)
5707 goto out_err;
5708 }
Alex Eldera30b71b2012-07-10 20:30:11 -05005709
Ilya Dryomov7e973322017-01-25 18:16:22 +01005710 if (rbd_dev->header.features & RBD_FEATURE_DATA_POOL) {
5711 ret = rbd_dev_v2_data_pool(rbd_dev);
5712 if (ret)
5713 goto out_err;
5714 }
5715
Ilya Dryomov263423f2017-01-25 18:16:22 +01005716 rbd_init_layout(rbd_dev);
Alex Elder35152972012-08-31 17:29:55 -05005717 return 0;
Ilya Dryomov263423f2017-01-25 18:16:22 +01005718
Alex Elder9d475de2012-07-03 16:01:19 -05005719out_err:
Alex Elder642a2532013-05-06 17:40:33 -05005720 rbd_dev->header.features = 0;
Alex Elder1e130192012-07-03 16:01:19 -05005721 kfree(rbd_dev->header.object_prefix);
5722 rbd_dev->header.object_prefix = NULL;
Alex Elder9d475de2012-07-03 16:01:19 -05005723 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005724}
5725
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005726/*
5727 * @depth is rbd_dev_image_probe() -> rbd_dev_probe_parent() ->
5728 * rbd_dev_image_probe() recursion depth, which means it's also the
5729 * length of the already discovered part of the parent chain.
5730 */
5731static int rbd_dev_probe_parent(struct rbd_device *rbd_dev, int depth)
Alex Elder83a06262012-10-30 15:47:17 -05005732{
Alex Elder2f82ee52012-10-30 19:40:33 -05005733 struct rbd_device *parent = NULL;
Alex Elder124afba2013-04-26 15:44:36 -05005734 int ret;
5735
5736 if (!rbd_dev->parent_spec)
5737 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005738
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005739 if (++depth > RBD_MAX_PARENT_CHAIN_LEN) {
5740 pr_info("parent chain is too long (%d)\n", depth);
5741 ret = -EINVAL;
5742 goto out_err;
5743 }
5744
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005745 parent = __rbd_dev_create(rbd_dev->rbd_client, rbd_dev->parent_spec);
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005746 if (!parent) {
5747 ret = -ENOMEM;
Alex Elder124afba2013-04-26 15:44:36 -05005748 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005749 }
5750
5751 /*
5752 * Images related by parent/child relationships always share
5753 * rbd_client and spec/parent_spec, so bump their refcounts.
5754 */
5755 __rbd_get_client(rbd_dev->rbd_client);
5756 rbd_spec_get(rbd_dev->parent_spec);
Alex Elder124afba2013-04-26 15:44:36 -05005757
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005758 ret = rbd_dev_image_probe(parent, depth);
Alex Elder124afba2013-04-26 15:44:36 -05005759 if (ret < 0)
5760 goto out_err;
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005761
Alex Elder124afba2013-04-26 15:44:36 -05005762 rbd_dev->parent = parent;
Alex Eldera2acd002013-05-08 22:50:04 -05005763 atomic_set(&rbd_dev->parent_ref, 1);
Alex Elder124afba2013-04-26 15:44:36 -05005764 return 0;
Alex Elder124afba2013-04-26 15:44:36 -05005765
Ilya Dryomov1f2c6652015-10-11 19:38:00 +02005766out_err:
5767 rbd_dev_unparent(rbd_dev);
Markus Elfring1761b222015-11-23 20:16:45 +01005768 rbd_dev_destroy(parent);
Alex Elder124afba2013-04-26 15:44:36 -05005769 return ret;
5770}
5771
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005772static void rbd_dev_device_release(struct rbd_device *rbd_dev)
5773{
5774 clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
5775 rbd_dev_mapping_clear(rbd_dev);
5776 rbd_free_disk(rbd_dev);
5777 if (!single_major)
5778 unregister_blkdev(rbd_dev->major, rbd_dev->name);
5779}
5780
Ilya Dryomov811c6682016-04-15 16:22:16 +02005781/*
5782 * rbd_dev->header_rwsem must be locked for write and will be unlocked
5783 * upon return.
5784 */
Alex Elder200a6a82013-04-28 23:32:34 -05005785static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
Alex Elder124afba2013-04-26 15:44:36 -05005786{
Alex Elder83a06262012-10-30 15:47:17 -05005787 int ret;
Alex Elder83a06262012-10-30 15:47:17 -05005788
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005789 /* Record our major and minor device numbers. */
Alex Elder83a06262012-10-30 15:47:17 -05005790
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005791 if (!single_major) {
5792 ret = register_blkdev(0, rbd_dev->name);
5793 if (ret < 0)
Ilya Dryomov1643dfa2016-08-12 15:45:52 +02005794 goto err_out_unlock;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005795
5796 rbd_dev->major = ret;
5797 rbd_dev->minor = 0;
5798 } else {
5799 rbd_dev->major = rbd_major;
5800 rbd_dev->minor = rbd_dev_id_to_minor(rbd_dev->dev_id);
5801 }
Alex Elder83a06262012-10-30 15:47:17 -05005802
5803 /* Set up the blkdev mapping. */
5804
5805 ret = rbd_init_disk(rbd_dev);
5806 if (ret)
5807 goto err_out_blkdev;
5808
Alex Elderf35a4de2013-05-06 09:51:29 -05005809 ret = rbd_dev_mapping_set(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005810 if (ret)
5811 goto err_out_disk;
Ilya Dryomovbc1ecc62014-08-04 18:04:39 +04005812
Alex Elderf35a4de2013-05-06 09:51:29 -05005813 set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
Ilya Dryomov9568c932017-10-12 12:35:19 +02005814 set_disk_ro(rbd_dev->disk, rbd_dev->opts->read_only);
Alex Elderf35a4de2013-05-06 09:51:29 -05005815
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005816 ret = dev_set_name(&rbd_dev->dev, "%d", rbd_dev->dev_id);
Alex Elderf35a4de2013-05-06 09:51:29 -05005817 if (ret)
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04005818 goto err_out_mapping;
Alex Elder83a06262012-10-30 15:47:17 -05005819
Alex Elder129b79d2013-04-26 15:44:36 -05005820 set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005821 up_write(&rbd_dev->header_rwsem);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02005822 return 0;
Alex Elder2f82ee52012-10-30 19:40:33 -05005823
Alex Elderf35a4de2013-05-06 09:51:29 -05005824err_out_mapping:
5825 rbd_dev_mapping_clear(rbd_dev);
Alex Elder83a06262012-10-30 15:47:17 -05005826err_out_disk:
5827 rbd_free_disk(rbd_dev);
5828err_out_blkdev:
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005829 if (!single_major)
5830 unregister_blkdev(rbd_dev->major, rbd_dev->name);
Ilya Dryomov811c6682016-04-15 16:22:16 +02005831err_out_unlock:
5832 up_write(&rbd_dev->header_rwsem);
Alex Elder83a06262012-10-30 15:47:17 -05005833 return ret;
5834}
5835
Alex Elder332bb122013-04-27 09:59:30 -05005836static int rbd_dev_header_name(struct rbd_device *rbd_dev)
5837{
5838 struct rbd_spec *spec = rbd_dev->spec;
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005839 int ret;
Alex Elder332bb122013-04-27 09:59:30 -05005840
5841 /* Record the header object name for this rbd image. */
5842
5843 rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
Alex Elder332bb122013-04-27 09:59:30 -05005844 if (rbd_dev->image_format == 1)
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005845 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5846 spec->image_name, RBD_SUFFIX);
Alex Elder332bb122013-04-27 09:59:30 -05005847 else
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005848 ret = ceph_oid_aprintf(&rbd_dev->header_oid, GFP_KERNEL, "%s%s",
5849 RBD_HEADER_PREFIX, spec->image_id);
Alex Elder332bb122013-04-27 09:59:30 -05005850
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005851 return ret;
Alex Elder332bb122013-04-27 09:59:30 -05005852}
5853
Alex Elder200a6a82013-04-28 23:32:34 -05005854static void rbd_dev_image_release(struct rbd_device *rbd_dev)
5855{
Alex Elder6fd48b32013-04-28 23:32:34 -05005856 rbd_dev_unprobe(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02005857 if (rbd_dev->opts)
5858 rbd_unregister_watch(rbd_dev);
Alex Elder6fd48b32013-04-28 23:32:34 -05005859 rbd_dev->image_format = 0;
5860 kfree(rbd_dev->spec->image_id);
5861 rbd_dev->spec->image_id = NULL;
Alex Elder200a6a82013-04-28 23:32:34 -05005862}
5863
Alex Eldera30b71b2012-07-10 20:30:11 -05005864/*
5865 * Probe for the existence of the header object for the given rbd
Alex Elder1f3ef782013-05-06 17:40:33 -05005866 * device. If this image is the one being mapped (i.e., not a
5867 * parent), initiate a watch on its header object before using that
5868 * object to get detailed information about the rbd image.
Alex Eldera30b71b2012-07-10 20:30:11 -05005869 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005870static int rbd_dev_image_probe(struct rbd_device *rbd_dev, int depth)
Alex Eldera30b71b2012-07-10 20:30:11 -05005871{
5872 int ret;
5873
5874 /*
Alex Elder3abef3b2013-05-13 20:35:37 -05005875 * Get the id from the image id object. Unless there's an
5876 * error, rbd_dev->spec->image_id will be filled in with
5877 * a dynamically-allocated string, and rbd_dev->image_format
5878 * will be set to either 1 or 2.
Alex Eldera30b71b2012-07-10 20:30:11 -05005879 */
5880 ret = rbd_dev_image_id(rbd_dev);
5881 if (ret)
Alex Elderc0fba362013-04-25 23:15:08 -05005882 return ret;
Alex Elderc0fba362013-04-25 23:15:08 -05005883
Alex Elder332bb122013-04-27 09:59:30 -05005884 ret = rbd_dev_header_name(rbd_dev);
5885 if (ret)
5886 goto err_out_format;
5887
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005888 if (!depth) {
Ilya Dryomov99d16942016-08-12 16:11:41 +02005889 ret = rbd_register_watch(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005890 if (ret) {
5891 if (ret == -ENOENT)
5892 pr_info("image %s/%s does not exist\n",
5893 rbd_dev->spec->pool_name,
5894 rbd_dev->spec->image_name);
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005895 goto err_out_format;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005896 }
Alex Elder1f3ef782013-05-06 17:40:33 -05005897 }
Alex Elderb644de22013-04-27 09:59:31 -05005898
Ilya Dryomova720ae02014-07-23 17:11:19 +04005899 ret = rbd_dev_header_info(rbd_dev);
Alex Elder5655c4d2013-04-25 23:15:08 -05005900 if (ret)
Alex Elderb644de22013-04-27 09:59:31 -05005901 goto err_out_watch;
Alex Elder83a06262012-10-30 15:47:17 -05005902
Ilya Dryomov04077592014-07-23 17:11:20 +04005903 /*
5904 * If this image is the one being mapped, we have pool name and
5905 * id, image name and id, and snap name - need to fill snap id.
5906 * Otherwise this is a parent image, identified by pool, image
5907 * and snap ids - need to fill in names for those ids.
5908 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005909 if (!depth)
Ilya Dryomov04077592014-07-23 17:11:20 +04005910 ret = rbd_spec_fill_snap_id(rbd_dev);
5911 else
5912 ret = rbd_spec_fill_names(rbd_dev);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005913 if (ret) {
5914 if (ret == -ENOENT)
5915 pr_info("snap %s/%s@%s does not exist\n",
5916 rbd_dev->spec->pool_name,
5917 rbd_dev->spec->image_name,
5918 rbd_dev->spec->snap_name);
Alex Elder33dca392013-04-30 00:44:33 -05005919 goto err_out_probe;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005920 }
Alex Elder9bb81c92013-04-27 09:59:30 -05005921
Ilya Dryomove8f59b52014-07-24 10:42:13 +04005922 if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
5923 ret = rbd_dev_v2_parent_info(rbd_dev);
5924 if (ret)
5925 goto err_out_probe;
5926
5927 /*
5928 * Need to warn users if this image is the one being
5929 * mapped and has a parent.
5930 */
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005931 if (!depth && rbd_dev->parent_spec)
Ilya Dryomove8f59b52014-07-24 10:42:13 +04005932 rbd_warn(rbd_dev,
5933 "WARNING: kernel layering is EXPERIMENTAL!");
5934 }
5935
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005936 ret = rbd_dev_probe_parent(rbd_dev, depth);
Alex Elder30d60ba2013-05-06 09:51:30 -05005937 if (ret)
5938 goto err_out_probe;
Alex Elder83a06262012-10-30 15:47:17 -05005939
Alex Elder30d60ba2013-05-06 09:51:30 -05005940 dout("discovered format %u image, header name is %s\n",
Ilya Dryomovc41d13a2016-04-29 20:01:25 +02005941 rbd_dev->image_format, rbd_dev->header_oid.name);
Alex Elder30d60ba2013-05-06 09:51:30 -05005942 return 0;
Ilya Dryomove8f59b52014-07-24 10:42:13 +04005943
Alex Elder6fd48b32013-04-28 23:32:34 -05005944err_out_probe:
5945 rbd_dev_unprobe(rbd_dev);
Alex Elderb644de22013-04-27 09:59:31 -05005946err_out_watch:
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02005947 if (!depth)
Ilya Dryomov99d16942016-08-12 16:11:41 +02005948 rbd_unregister_watch(rbd_dev);
Alex Elder332bb122013-04-27 09:59:30 -05005949err_out_format:
5950 rbd_dev->image_format = 0;
Alex Elder5655c4d2013-04-25 23:15:08 -05005951 kfree(rbd_dev->spec->image_id);
5952 rbd_dev->spec->image_id = NULL;
Alex Elder5655c4d2013-04-25 23:15:08 -05005953 return ret;
Alex Eldera30b71b2012-07-10 20:30:11 -05005954}
5955
Ilya Dryomov9b60e702013-12-13 15:28:57 +02005956static ssize_t do_rbd_add(struct bus_type *bus,
5957 const char *buf,
5958 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005959{
Alex Eldercb8627c2012-07-09 21:04:23 -05005960 struct rbd_device *rbd_dev = NULL;
Alex Elderdc79b112012-10-25 23:34:41 -05005961 struct ceph_options *ceph_opts = NULL;
Alex Elder4e9afeb2012-10-25 23:34:41 -05005962 struct rbd_options *rbd_opts = NULL;
Alex Elder859c31d2012-10-25 23:34:42 -05005963 struct rbd_spec *spec = NULL;
Alex Elder9d3997f2012-10-25 23:34:42 -05005964 struct rbd_client *rbdc;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02005965 int rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005966
5967 if (!try_module_get(THIS_MODULE))
5968 return -ENODEV;
5969
Alex Eldera725f65e2012-02-02 08:13:30 -06005970 /* parse add command */
Alex Elder859c31d2012-10-25 23:34:42 -05005971 rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
Alex Elderdc79b112012-10-25 23:34:41 -05005972 if (rc < 0)
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02005973 goto out;
Alex Eldera725f65e2012-02-02 08:13:30 -06005974
Alex Elder9d3997f2012-10-25 23:34:42 -05005975 rbdc = rbd_get_client(ceph_opts);
5976 if (IS_ERR(rbdc)) {
5977 rc = PTR_ERR(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05005978 goto err_out_args;
Alex Elder9d3997f2012-10-25 23:34:42 -05005979 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005980
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005981 /* pick the pool */
Ilya Dryomov30ba1f02014-05-13 11:19:27 +04005982 rc = rbd_add_get_pool_id(rbdc, spec->pool_name);
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005983 if (rc < 0) {
5984 if (rc == -ENOENT)
5985 pr_info("pool %s does not exist\n", spec->pool_name);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005986 goto err_out_client;
Ilya Dryomov1fe48022015-03-05 10:47:22 +03005987 }
Alex Elderc0cd10db2013-04-26 09:43:47 -05005988 spec->pool_id = (u64)rc;
Alex Elder859c31d2012-10-25 23:34:42 -05005989
Ilya Dryomovd1475432015-06-22 13:24:48 +03005990 rbd_dev = rbd_dev_create(rbdc, spec, rbd_opts);
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02005991 if (!rbd_dev) {
5992 rc = -ENOMEM;
Alex Elderbd4ba652012-10-25 23:34:42 -05005993 goto err_out_client;
Ilya Dryomovb51c83c2015-10-15 15:38:57 +02005994 }
Alex Elderc53d5892012-10-25 23:34:42 -05005995 rbdc = NULL; /* rbd_dev now owns this */
5996 spec = NULL; /* rbd_dev now owns this */
Ilya Dryomovd1475432015-06-22 13:24:48 +03005997 rbd_opts = NULL; /* rbd_dev now owns this */
Yehuda Sadeh602adf42010-08-12 16:11:25 -07005998
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02005999 rbd_dev->config_info = kstrdup(buf, GFP_KERNEL);
6000 if (!rbd_dev->config_info) {
6001 rc = -ENOMEM;
6002 goto err_out_rbd_dev;
6003 }
6004
Ilya Dryomov811c6682016-04-15 16:22:16 +02006005 down_write(&rbd_dev->header_rwsem);
Ilya Dryomov6d69bb532015-10-11 19:38:00 +02006006 rc = rbd_dev_image_probe(rbd_dev, 0);
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006007 if (rc < 0) {
6008 up_write(&rbd_dev->header_rwsem);
Alex Elderc53d5892012-10-25 23:34:42 -05006009 goto err_out_rbd_dev;
Mike Christie0d6d1e9c2016-08-18 18:38:45 +02006010 }
Alex Elder05fd6f62012-08-29 17:11:07 -05006011
Alex Elder7ce4eef2013-05-06 17:40:33 -05006012 /* If we are mapping a snapshot it must be marked read-only */
Alex Elder7ce4eef2013-05-06 17:40:33 -05006013 if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
Ilya Dryomov9568c932017-10-12 12:35:19 +02006014 rbd_dev->opts->read_only = true;
Alex Elder7ce4eef2013-05-06 17:40:33 -05006015
Alex Elderb536f692013-04-28 23:32:34 -05006016 rc = rbd_dev_device_setup(rbd_dev);
Ilya Dryomovfd22aef2017-04-13 12:17:37 +02006017 if (rc)
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006018 goto err_out_image_probe;
Alex Elderb536f692013-04-28 23:32:34 -05006019
Ilya Dryomove010dd02017-04-13 12:17:39 +02006020 if (rbd_dev->opts->exclusive) {
6021 rc = rbd_add_acquire_lock(rbd_dev);
6022 if (rc)
6023 goto err_out_device_setup;
Alex Elderb536f692013-04-28 23:32:34 -05006024 }
6025
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006026 /* Everything's ready. Announce the disk to the world. */
6027
6028 rc = device_add(&rbd_dev->dev);
6029 if (rc)
Ilya Dryomove010dd02017-04-13 12:17:39 +02006030 goto err_out_image_lock;
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006031
6032 add_disk(rbd_dev->disk);
6033 /* see rbd_init_disk() */
6034 blk_put_queue(rbd_dev->disk->queue);
6035
6036 spin_lock(&rbd_dev_list_lock);
6037 list_add_tail(&rbd_dev->node, &rbd_dev_list);
6038 spin_unlock(&rbd_dev_list_lock);
6039
6040 pr_info("%s: capacity %llu features 0x%llx\n", rbd_dev->disk->disk_name,
6041 (unsigned long long)get_capacity(rbd_dev->disk) << SECTOR_SHIFT,
6042 rbd_dev->header.features);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006043 rc = count;
6044out:
6045 module_put(THIS_MODULE);
6046 return rc;
Alex Elder3abef3b2013-05-13 20:35:37 -05006047
Ilya Dryomove010dd02017-04-13 12:17:39 +02006048err_out_image_lock:
6049 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006050err_out_device_setup:
6051 rbd_dev_device_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006052err_out_image_probe:
6053 rbd_dev_image_release(rbd_dev);
Alex Elderc53d5892012-10-25 23:34:42 -05006054err_out_rbd_dev:
6055 rbd_dev_destroy(rbd_dev);
Alex Elderbd4ba652012-10-25 23:34:42 -05006056err_out_client:
Alex Elder9d3997f2012-10-25 23:34:42 -05006057 rbd_put_client(rbdc);
Alex Elder0ddebc02012-10-25 23:34:41 -05006058err_out_args:
Alex Elder859c31d2012-10-25 23:34:42 -05006059 rbd_spec_put(spec);
Ilya Dryomovd1475432015-06-22 13:24:48 +03006060 kfree(rbd_opts);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006061 goto out;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006062}
6063
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006064static ssize_t rbd_add(struct bus_type *bus,
6065 const char *buf,
6066 size_t count)
6067{
6068 if (single_major)
6069 return -EINVAL;
6070
6071 return do_rbd_add(bus, buf, count);
6072}
6073
6074static ssize_t rbd_add_single_major(struct bus_type *bus,
6075 const char *buf,
6076 size_t count)
6077{
6078 return do_rbd_add(bus, buf, count);
6079}
6080
Alex Elder05a46af2013-04-26 15:44:36 -05006081static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
6082{
Alex Elderad945fc2013-04-26 15:44:36 -05006083 while (rbd_dev->parent) {
Alex Elder05a46af2013-04-26 15:44:36 -05006084 struct rbd_device *first = rbd_dev;
6085 struct rbd_device *second = first->parent;
6086 struct rbd_device *third;
6087
6088 /*
6089 * Follow to the parent with no grandparent and
6090 * remove it.
6091 */
6092 while (second && (third = second->parent)) {
6093 first = second;
6094 second = third;
6095 }
Alex Elderad945fc2013-04-26 15:44:36 -05006096 rbd_assert(second);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006097 rbd_dev_image_release(second);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006098 rbd_dev_destroy(second);
Alex Elderad945fc2013-04-26 15:44:36 -05006099 first->parent = NULL;
6100 first->parent_overlap = 0;
6101
6102 rbd_assert(first->parent_spec);
Alex Elder05a46af2013-04-26 15:44:36 -05006103 rbd_spec_put(first->parent_spec);
6104 first->parent_spec = NULL;
Alex Elder05a46af2013-04-26 15:44:36 -05006105 }
6106}
6107
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006108static ssize_t do_rbd_remove(struct bus_type *bus,
6109 const char *buf,
6110 size_t count)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006111{
6112 struct rbd_device *rbd_dev = NULL;
Alex Elder751cc0e2013-05-31 15:17:01 -05006113 struct list_head *tmp;
6114 int dev_id;
Mike Christie0276dca2016-08-18 18:38:45 +02006115 char opt_buf[6];
Alex Elder82a442d2013-05-31 17:40:44 -05006116 bool already = false;
Mike Christie0276dca2016-08-18 18:38:45 +02006117 bool force = false;
Alex Elder0d8189e2013-04-27 09:59:30 -05006118 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006119
Mike Christie0276dca2016-08-18 18:38:45 +02006120 dev_id = -1;
6121 opt_buf[0] = '\0';
6122 sscanf(buf, "%d %5s", &dev_id, opt_buf);
6123 if (dev_id < 0) {
6124 pr_err("dev_id out of range\n");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006125 return -EINVAL;
Mike Christie0276dca2016-08-18 18:38:45 +02006126 }
6127 if (opt_buf[0] != '\0') {
6128 if (!strcmp(opt_buf, "force")) {
6129 force = true;
6130 } else {
6131 pr_err("bad remove option at '%s'\n", opt_buf);
6132 return -EINVAL;
6133 }
6134 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006135
Alex Elder751cc0e2013-05-31 15:17:01 -05006136 ret = -ENOENT;
6137 spin_lock(&rbd_dev_list_lock);
6138 list_for_each(tmp, &rbd_dev_list) {
6139 rbd_dev = list_entry(tmp, struct rbd_device, node);
6140 if (rbd_dev->dev_id == dev_id) {
6141 ret = 0;
6142 break;
6143 }
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006144 }
Alex Elder751cc0e2013-05-31 15:17:01 -05006145 if (!ret) {
6146 spin_lock_irq(&rbd_dev->lock);
Mike Christie0276dca2016-08-18 18:38:45 +02006147 if (rbd_dev->open_count && !force)
Alex Elder751cc0e2013-05-31 15:17:01 -05006148 ret = -EBUSY;
6149 else
Alex Elder82a442d2013-05-31 17:40:44 -05006150 already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
6151 &rbd_dev->flags);
Alex Elder751cc0e2013-05-31 15:17:01 -05006152 spin_unlock_irq(&rbd_dev->lock);
6153 }
6154 spin_unlock(&rbd_dev_list_lock);
Alex Elder82a442d2013-05-31 17:40:44 -05006155 if (ret < 0 || already)
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006156 return ret;
Alex Elder751cc0e2013-05-31 15:17:01 -05006157
Mike Christie0276dca2016-08-18 18:38:45 +02006158 if (force) {
6159 /*
6160 * Prevent new IO from being queued and wait for existing
6161 * IO to complete/fail.
6162 */
6163 blk_mq_freeze_queue(rbd_dev->disk->queue);
6164 blk_set_queue_dying(rbd_dev->disk->queue);
6165 }
6166
Ilya Dryomov5769ed02017-04-13 12:17:38 +02006167 del_gendisk(rbd_dev->disk);
6168 spin_lock(&rbd_dev_list_lock);
6169 list_del_init(&rbd_dev->node);
6170 spin_unlock(&rbd_dev_list_lock);
6171 device_del(&rbd_dev->dev);
Ilya Dryomovfca27062013-12-16 18:02:40 +02006172
Ilya Dryomove010dd02017-04-13 12:17:39 +02006173 rbd_dev_image_unlock(rbd_dev);
Ilya Dryomovdd5ac322015-10-16 17:09:24 +02006174 rbd_dev_device_release(rbd_dev);
Alex Elder8ad42cd2013-04-28 23:32:34 -05006175 rbd_dev_image_release(rbd_dev);
Ilya Dryomov8b679ec2017-04-13 12:17:37 +02006176 rbd_dev_destroy(rbd_dev);
Alex Elder1ba0f1e2013-05-31 15:17:01 -05006177 return count;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006178}
6179
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006180static ssize_t rbd_remove(struct bus_type *bus,
6181 const char *buf,
6182 size_t count)
6183{
6184 if (single_major)
6185 return -EINVAL;
6186
6187 return do_rbd_remove(bus, buf, count);
6188}
6189
6190static ssize_t rbd_remove_single_major(struct bus_type *bus,
6191 const char *buf,
6192 size_t count)
6193{
6194 return do_rbd_remove(bus, buf, count);
6195}
6196
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006197/*
6198 * create control files in sysfs
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006199 * /sys/bus/rbd/...
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006200 */
6201static int rbd_sysfs_init(void)
6202{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006203 int ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006204
Alex Elderfed4c142012-02-07 12:03:36 -06006205 ret = device_register(&rbd_root_dev);
Alex Elder21079782012-01-24 10:08:36 -06006206 if (ret < 0)
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006207 return ret;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006208
Alex Elderfed4c142012-02-07 12:03:36 -06006209 ret = bus_register(&rbd_bus_type);
6210 if (ret < 0)
6211 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006212
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006213 return ret;
6214}
6215
6216static void rbd_sysfs_cleanup(void)
6217{
Yehuda Sadehdfc56062010-11-19 14:51:04 -08006218 bus_unregister(&rbd_bus_type);
Alex Elderfed4c142012-02-07 12:03:36 -06006219 device_unregister(&rbd_root_dev);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006220}
6221
Alex Elder1c2a9df2013-05-01 12:43:03 -05006222static int rbd_slab_init(void)
6223{
6224 rbd_assert(!rbd_img_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006225 rbd_img_request_cache = KMEM_CACHE(rbd_img_request, 0);
Alex Elder868311b2013-05-01 12:43:03 -05006226 if (!rbd_img_request_cache)
6227 return -ENOMEM;
6228
6229 rbd_assert(!rbd_obj_request_cache);
Geliang Tang03d94402016-03-13 15:17:32 +08006230 rbd_obj_request_cache = KMEM_CACHE(rbd_obj_request, 0);
Alex Elder78c2a442013-05-01 12:43:04 -05006231 if (!rbd_obj_request_cache)
6232 goto out_err;
6233
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006234 return 0;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006235
Ilya Dryomov6c696d82017-01-25 18:16:23 +01006236out_err:
Alex Elder868311b2013-05-01 12:43:03 -05006237 kmem_cache_destroy(rbd_img_request_cache);
6238 rbd_img_request_cache = NULL;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006239 return -ENOMEM;
6240}
6241
6242static void rbd_slab_exit(void)
6243{
Alex Elder868311b2013-05-01 12:43:03 -05006244 rbd_assert(rbd_obj_request_cache);
6245 kmem_cache_destroy(rbd_obj_request_cache);
6246 rbd_obj_request_cache = NULL;
6247
Alex Elder1c2a9df2013-05-01 12:43:03 -05006248 rbd_assert(rbd_img_request_cache);
6249 kmem_cache_destroy(rbd_img_request_cache);
6250 rbd_img_request_cache = NULL;
6251}
6252
Alex Eldercc344fa2013-02-19 12:25:56 -06006253static int __init rbd_init(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006254{
6255 int rc;
6256
Alex Elder1e32d342013-01-30 11:13:33 -06006257 if (!libceph_compatible(NULL)) {
6258 rbd_warn(NULL, "libceph incompatibility (quitting)");
Alex Elder1e32d342013-01-30 11:13:33 -06006259 return -EINVAL;
6260 }
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006261
Alex Elder1c2a9df2013-05-01 12:43:03 -05006262 rc = rbd_slab_init();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006263 if (rc)
6264 return rc;
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006265
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006266 /*
6267 * The number of active work items is limited by the number of
Ilya Dryomovf77303b2015-04-22 18:28:13 +03006268 * rbd devices * queue depth, so leave @max_active at default.
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006269 */
6270 rbd_wq = alloc_workqueue(RBD_DRV_NAME, WQ_MEM_RECLAIM, 0);
6271 if (!rbd_wq) {
6272 rc = -ENOMEM;
6273 goto err_out_slab;
6274 }
6275
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006276 if (single_major) {
6277 rbd_major = register_blkdev(0, RBD_DRV_NAME);
6278 if (rbd_major < 0) {
6279 rc = rbd_major;
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006280 goto err_out_wq;
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006281 }
6282 }
6283
Alex Elder1c2a9df2013-05-01 12:43:03 -05006284 rc = rbd_sysfs_init();
6285 if (rc)
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006286 goto err_out_blkdev;
Alex Elder1c2a9df2013-05-01 12:43:03 -05006287
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006288 if (single_major)
6289 pr_info("loaded (major %d)\n", rbd_major);
6290 else
6291 pr_info("loaded\n");
6292
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006293 return 0;
6294
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006295err_out_blkdev:
6296 if (single_major)
6297 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006298err_out_wq:
6299 destroy_workqueue(rbd_wq);
Ilya Dryomove1b4d962013-12-13 15:28:57 +02006300err_out_slab:
6301 rbd_slab_exit();
Alex Elder1c2a9df2013-05-01 12:43:03 -05006302 return rc;
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006303}
6304
Alex Eldercc344fa2013-02-19 12:25:56 -06006305static void __exit rbd_exit(void)
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006306{
Ilya Dryomovffe312c2014-05-20 15:46:04 +04006307 ida_destroy(&rbd_dev_id_ida);
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006308 rbd_sysfs_cleanup();
Ilya Dryomov9b60e702013-12-13 15:28:57 +02006309 if (single_major)
6310 unregister_blkdev(rbd_major, RBD_DRV_NAME);
Ilya Dryomovf5ee37b2014-10-09 17:06:01 +04006311 destroy_workqueue(rbd_wq);
Alex Elder1c2a9df2013-05-01 12:43:03 -05006312 rbd_slab_exit();
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006313}
6314
6315module_init(rbd_init);
6316module_exit(rbd_exit);
6317
Alex Elderd552c612013-05-31 20:13:09 -05006318MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006319MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
6320MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006321/* following authorship retained from original osdblk.c */
6322MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
6323
Ilya Dryomov90da2582013-12-13 15:28:56 +02006324MODULE_DESCRIPTION("RADOS Block Device (RBD) driver");
Yehuda Sadeh602adf42010-08-12 16:11:25 -07006325MODULE_LICENSE("GPL");